-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription-daemon.py
120 lines (89 loc) · 5.66 KB
/
subscription-daemon.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#its reccomended to have this run just once a day, at a time when most of your users wont be online, so a good example time would be 3am
#import statements
import flask, yt_dlp, sqlite3, json, time, os
from config import DATABASE_PATH
#make a connection to the database
DATABASE_CONNECTION = sqlite3.connect(DATABASE_PATH)
DATABASE_CURSOR = DATABASE_CONNECTION.cursor()
#select the data from the subscriptions table
DATABASE_CURSOR.execute('SELECT * FROM subscriptions ORDER BY subscription_id ASC')
databaseRows = DATABASE_CURSOR.fetchall()
#function to download videos (returns the path of the downloaded video) (borrowed from app.py)
def downloadVideo(videoURL, videoFormat, parentDownloadDir) -> str:
#the ytdl temporary file name (just make it a timestamp so that it doesnt overwrite anything)
tmpFileNameNumber = str(time.time())
#set up the youtube downloader object
youtubeDLObject = yt_dlp.YoutubeDL({'format':videoFormat,'outtmpl':'{}/{}.%(ext)s'.format(parentDownloadDir, tmpFileNameNumber),'default_search':'youtube'})
#download the metadata so that the video can be tagged for usage with streaming servers
youtubeVideoData = youtubeDLObject.extract_info(videoURL, download = False)
#get the data that is needed (this isnt a nessecary step, but it makes the code easier to work with)
youtubeVideoMetadataData = {
'ext':youtubeVideoData['ext'],
'title':youtubeVideoData['title'],
'uploader':youtubeVideoData['uploader'],
'id':youtubeVideoData['id'],
'playlist':youtubeVideoData['album'],
'playlist_index':youtubeVideoData['playlist_index'],
'upload_year':str(youtubeVideoData['upload_date'])[0:4],
'upload_month':str(youtubeVideoData['upload_date'])[4:6],
'upload_day':str(youtubeVideoData['upload_date'])[6:8]
}
#download the video
youtubeDLObject.download([videoURL])
#encode the media file with the data
os.system('ffmpeg -i "{}/{}.{}" -metadata title="{}" -metadata author="{}" -metadata artist="{}" -c copy "{}/{}_{}_{}_{}.{}" -nostdin -y'.format(
parentDownloadDir, #download directory
tmpFileNameNumber, #filename
youtubeVideoMetadataData['ext'], #extension
youtubeVideoMetadataData['title'], #metadata title
youtubeVideoMetadataData['uploader'], #metadata author (for video)
youtubeVideoMetadataData['uploader'], #metadata artist (for music)
parentDownloadDir, #download directory
youtubeVideoMetadataData['upload_year'], #upload year
youtubeVideoMetadataData['upload_month'], #upload month
youtubeVideoMetadataData['upload_day'], #upload day
youtubeVideoMetadataData['title'], #title
youtubeVideoMetadataData['ext'] #extension
))
#delete the original file
os.remove('{}/{}.{}'.format(parentDownloadDir, tmpFileNameNumber, youtubeVideoMetadataData['ext']))
#return the path of the video
return '{}/{}_{}_{}_{}.{}'.format(parentDownloadDir, youtubeVideoMetadataData['upload_year'], youtubeVideoMetadataData['upload_month'], youtubeVideoMetadataData['upload_day'], youtubeVideoMetadataData['title'], youtubeVideoMetadataData['ext'])
#iterate through the subscriptions
for subscription in databaseRows:
#big try catch, in case something is wrong with the subscription link
try:
#get the data about the subscription
SUBSCRIPTION_ID = subscription[0] #the id of the entry in the database
SUBSCRIPTION_URL = subscription[1] #the playlist/channel url that the subscription program monitors
SUBSCRIPTION_FORMAT = subscription[2] #the format that the videos will be downloaded in
SUBSCRIPTION_DIR = subscription[3] #the directory where the videos will be downloaded to
alreadyDownloadedVideoList = json.loads(subscription[4]) #the list of already downloaded videos from the playlist
#get the list of videos from the playlist/channel
youtubeDLObject = yt_dlp.YoutubeDL({'default_search':'youtube'})
playlistOrChannelData = youtubeDLObject.extract_info(SUBSCRIPTION_URL, download = False)
#iterate through the videos in the playlist
for entry in playlistOrChannelData['entries']:
#check that the video is already downloaded
if (entry['webpage_url'] not in alreadyDownloadedVideoList):
#try to download the video
try:
#download the video
downloadVideo(entry['webpage_url'], SUBSCRIPTION_FORMAT, SUBSCRIPTION_DIR)
#add the video url to the list of downloaded videos
alreadyDownloadedVideoList.append(entry['webpage_url'])
#the video wasnt able to be downloaded
except:
#print a warning
print('Skipping "{}" in the playlist/channel "{}": error while downloading.'.format(entry['webpage_url'], SUBSCRIPTION_URL))
#the video is already downloaded, just display a message that it already is here
else:
#print the message
print('Skipping "{}" in the playlist/channel "{}": already downloaded.'.format(entry['webpage_url'], SUBSCRIPTION_URL))
#update the downloaded video list
DATABASE_CONNECTION.execute('UPDATE subscriptions SET downloaded_video_list_json = ? WHERE subscription_id = ?', (json.dumps(alreadyDownloadedVideoList), SUBSCRIPTION_ID))
DATABASE_CONNECTION.commit()
#something went wrong, print an error
except:
#print an error and the information about the playlist
print('Error with the playlist: {}'.format(subscription))