Skip to content

Commit

Permalink
1. added youtube functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag2506 committed Mar 25, 2024
1 parent a30abb2 commit 1fc14c1
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 3 deletions.
17 changes: 17 additions & 0 deletions handleChats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from helpers.youtubeFunctionalities.client import getClient
from helpers.youtubeFunctionalities.stream import getStreamChatId
from appUtils import log
import os
from dotenv import load_dotenv
load_dotenv()

streamId = os.environ.get("STREAM_ID", "")
nextPageToken = ""

youtube = getClient()

chatId = getStreamChatId(youtube, streamId)
if chatId != "":
print(chatId)
else:
log.info("Received empty chat ID")
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions helpers/youtubeFunctionalities/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from appUtils import log

def getLiveChats(client, liveChatId: str, pageToken: str = "", part: str = "snippet"):
# client.liveBroadcasts().list(part=part, ).execute()
return
16 changes: 16 additions & 0 deletions helpers/youtubeFunctionalities/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from helpers.youtubeFunctionalities.credentials import write as writeCred, read as readCred
from appUtils import log
import googleapiclient.discovery as discovery
from googleapiclient.discovery import Resource

API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'

def getClient() -> Resource:
try:
credentials = readCred()
youtube = discovery.build(API_SERVICE_NAME, API_VERSION, credentials= credentials)
writeCred(creds= credentials) # in case credentials were refreshed
return youtube
except Exception as e:
log.error("Error in building client: {}".format(e))
16 changes: 16 additions & 0 deletions helpers/youtubeFunctionalities/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from appUtils import log, writeJson, readJson, configuration, json
from google.oauth2.credentials import Credentials

def write(creds: Credentials):
try:
writeJson(configuration["GCP"]["OAuthToken"], json.loads(creds.to_json()))
except Exception as e:
log.error("Error in writing credentials: {}".format(e))

def read() -> Credentials:
try:
tokenJson = readJson(configuration["GCP"]["OAuthToken"])
credentials = Credentials.from_authorized_user_info(tokenJson)
return credentials
except Exception as e:
log.error("Error in reading credentials: {}".format(e))
Empty file.
16 changes: 16 additions & 0 deletions helpers/youtubeFunctionalities/stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from appUtils import log
from googleapiclient.discovery import Resource

def getStreamChatId(client: Resource, streamId: str, part: str = "liveStreamingDetails"):
response = ""
try:
ytResponse = client.videos().list(
part=part,
id= streamId
).execute()
items = ytResponse.get("items", [])
if len(items) > 0:
response = items[0].get("liveStreamingDetails", {}).get("activeLiveChatId", "")
except Exception as e:
log.error("Error in getting stream ID: {}".format(e))
return response
Empty file.
1 change: 1 addition & 0 deletions sample-env.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STREAM_ID=""
7 changes: 4 additions & 3 deletions services/auth/routeDetails.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Blueprint, redirect, request, session
from appUtils import configuration, writeJson, json
from flask import Blueprint, redirect, request
from appUtils import configuration
from helpers.youtubeFunctionalities.credentials import write as writeCred
from google_auth_oauthlib.flow import Flow

blueprint = Blueprint('authRoutes', __name__, template_folder= 'templates')
Expand All @@ -19,5 +20,5 @@ def login():
def callback():
flow.fetch_token(authorization_response= request.url)
credentials = flow.credentials
writeJson(configuration["GCP"]["OAuthToken"], json.loads(credentials.to_json()))
writeCred(creds= credentials)
return redirect('/home')

0 comments on commit 1fc14c1

Please sign in to comment.