-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (50 loc) · 1.65 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import youtube as yt
import database as db
import random
from bs4 import BeautifulSoup
def random_playlist(con):
'''Randomly select a channel uploads playlist'''
uploads_ids = db.get_uploads_ids(con)
selection = random.choice(uploads_ids)
return selection
def random_video(con, video_id):
'''
- Selects a video id from the uploads playlist.
- Checks to see if that video id has been played before and skips it if it has
'''
youtube = yt.YoutubeClass()
past_videos = db.get_video_ids(con)
video_response = youtube.get_video_ids(video_id)
n = 0
while True:
id = video_response['items'][n]['contentDetails']['videoId']
if id in past_videos:
n += 1
else:
break
return id
def update_html(video_id):
url = f"https://www.youtube.com/embed/{video_id}"
with open("index.html") as fp:
soup = BeautifulSoup(fp, 'html.parser')
tag = soup.iframe
tag['src'] = url
with open("index.html", "w") as file:
file.write(str(soup))
return print('HTML has been updated with new video!')
#hN3CyODxl-c
#update_html('hN3CyODxl-c')
# Need to now create html production file that will get updated with
# the new video id. Will use beautifulsoup to do so
# with open("index.html") as fp:
# soup = BeautifulSoup(fp, 'html.parser')
# con = db.init_db('StemVideo.db')
# db.insert_video_id(con, 'hN3CyODxl-c')
def main():
con = db.init_db('StemVideo.db')
playlist = random_playlist(con)
video = random_video(con, playlist)
db_id = db.insert_video_id(con, video)
update_html(video)
if __name__ == '__main__':
main()