Skip to content

Commit

Permalink
embed function
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzapanther committed Sep 1, 2023
1 parent a5133c0 commit f78d776
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ dist
public
.hugo_build.lock
resources/
.pdm-python
__pycache__
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ Hugo Theme: [Stack](https://github.com/CaiJimmy/hugo-theme-stack)
./build.sh ios
```

## Embed Code

Channel ID `UCmo8zL1ZhvT4vYNzhSAuAEw`

```html
<div id="stream-frame" style="width: 100%; height: 100%; color: white; background-color: black; display: flex; justify-content: center; align-items: center;">
<h2>Waiting for Stream to Start</h2>
</div>
<script>
function fetch_status() {
fetch('https://wildwoodag.herokuapp.com/stream-frame/status_1.json?channel=UCmo8zL1ZhvT4vYNzhSAuAEw&ts=' + Date.now())
.then(response => response.json())
.then((data) => {
if (data.status == 'live') {
document.querySelector("#stream-frame").innerHTML = data.embed;
} else{
setTimeout(fetch_status, 10000);
}
})
.catch(e => alert('Error fetching stream status; refresh page.'));
}
fetch_status();
</script>
```

## Palette

Expand Down
314 changes: 314 additions & 0 deletions pdm.lock

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions pyproject.toml
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"
22 changes: 22 additions & 0 deletions run_func.py
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()
77 changes: 77 additions & 0 deletions wwfunc/packages/wildweb/embedcheck/__main__.py
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': '*'
}
}
7 changes: 7 additions & 0 deletions wwfunc/packages/wildweb/embedcheck/build.sh
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
4 changes: 4 additions & 0 deletions wwfunc/packages/wildweb/embedcheck/requirements.txt
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
10 changes: 10 additions & 0 deletions wwfunc/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ packages:
environment: {}
annotations: {}
limits: {}
- name: embedcheck
binary: false
main: ""
runtime: python:3.11
web: true
webSecure: false
parameters: {}
environment: {}
annotations: {}
limits: {}

0 comments on commit f78d776

Please sign in to comment.