-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5133c0
commit f78d776
Showing
9 changed files
with
481 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,3 +133,5 @@ dist | |
public | ||
.hugo_build.lock | ||
resources/ | ||
.pdm-python | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[project] | ||
name = "" | ||
version = "" | ||
description = "" | ||
authors = [ | ||
{name = "Paul Bailey", email = "[email protected]"}, | ||
] | ||
dependencies = [ | ||
"google-auth>=2.22.0", | ||
"google-auth-httplib2>=0.1.0", | ||
"google-api-python-client>=2.97.0", | ||
"google-auth-oauthlib>=1.0.0", | ||
] | ||
requires-python = ">=3.10" | ||
readme = "README.md" | ||
license = {text = "MIT"} | ||
|
||
[tool.pdm.scripts] | ||
_.env_file = ".env" | ||
func = "python run_func.py" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
import sys | ||
from importlib.machinery import SourceFileLoader | ||
|
||
def run(): | ||
fmain = SourceFileLoader("fmain", f'wwfunc/packages/wildweb/{sys.argv[1]}/__main__.py').load_module() | ||
|
||
if len(sys.argv) > 2: | ||
args = sys.argv[2:] | ||
event = {} | ||
for arg in args: | ||
key, value = arg.split(":") | ||
event[key] = value | ||
ret = fmain.main(event) | ||
|
||
else: | ||
ret = fmain.main(event) | ||
|
||
print(ret) | ||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import os | ||
import time | ||
import json | ||
|
||
import google.oauth2.credentials | ||
import google_auth_oauthlib.flow | ||
import googleapiclient.discovery | ||
|
||
OAUTH_TOKEN = json.loads(os.environ.get('OAUTH_TOKEN')) | ||
OAUTH_SECRET = json.loads(os.environ.get('OAUTH_SECRET')) | ||
|
||
|
||
class Channel: | ||
def __init__(self, channel_id): | ||
self.channel_id = channel_id | ||
|
||
@property | ||
def client(self): | ||
if not getattr(self, '_client', None): | ||
creds = google.oauth2.credentials.Credentials(**OAUTH_TOKEN) | ||
self._client = googleapiclient.discovery.build('youtube', 'v3', credentials=creds) | ||
|
||
return self._client | ||
|
||
def find_live(self): | ||
resp = self.client.search().list(eventType="live", channelId=self.channel_id, part="snippet", maxResults=1, type="video").execute() | ||
items = resp.get('items') | ||
if items: | ||
return items[0]['id']['videoId'] | ||
|
||
def get_video(self, vid): | ||
if not getattr(self, '_video', None): | ||
resp = self.client.videos().list(part='snippet,status,player', id=vid).execute() | ||
self._video = resp.get('items')[0] | ||
|
||
return self._video | ||
|
||
def allow_embed(self, vid): | ||
video = self.get_video(vid) | ||
|
||
if video['status']['embeddable']: | ||
pass | ||
|
||
else: | ||
self.client.videos().update(part='status', body={"id": vid, "status": {"embeddable": True}}).execute() | ||
time.sleep(0.5) | ||
del self._video | ||
|
||
|
||
def main(event): | ||
channel = event.get("channel") | ||
if not channel: | ||
return { | ||
"headers": { | ||
"Content-Type": "text/plain" | ||
}, | ||
"statusCode": 404, | ||
"body": "Channel Required" | ||
} | ||
|
||
channel = Channel(channel) | ||
vid = channel.find_live() | ||
embed = None | ||
status = 'offline' | ||
|
||
if vid: | ||
channel.allow_embed(vid) | ||
data = channel.get_video(vid) | ||
embed = data['player']['embedHtml'] | ||
status = 'live' | ||
|
||
return { | ||
'body': {'status': status, 'embed': embed}, | ||
'headers': { | ||
'Access-Control-Allow-Origin': '*' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
virtualenv --without-pip virtualenv | ||
|
||
pip install -r requirements.txt --target virtualenv/lib/python3.11/site-packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
google-auth>=2.6.0 | ||
google-auth-httplib2>=0.1.0 | ||
google-api-python-client>=2.39.0 | ||
google-auth-oauthlib>=0.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters