-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_script.py
69 lines (49 loc) · 1.79 KB
/
youtube_script.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
# import module
from googleapiclient.discovery import build
import json
import sys
import time
import logging
import requests
import os
from datetime import datetime
youtubeDLDomain = "https://" + os.environ['youtubeDLDomain'] + "/add"
youtubeAPIKey = os.environ['youtubeAPIKey']
playlistId = os.environ['playlistId']
alreadyDownloadedCount = os.environ['alreadyDownloaded']
def playlist_video_links(playlistId, VideosCount):
# creating youtube resource object
youtube = build('youtube','v3',developerKey=youtubeAPIKey)
# retrieve youtube video results
pl_request = youtube.playlistItems().list(
part="contentDetails",
playlistId=playlistId,
)
pl_response = pl_request.execute()
newCount = pl_response['pageInfo']['totalResults']
# is there a new item?
if newCount > VideosCount:
download_new_video(newCount, pl_response['items'][VideosCount]['contentDetails']['videoId'])
else:
logging.warning(str(datetime.now()) + "; allready up to date")
time.sleep(7200)
def download_new_video(playlistNumber, videoId):
link = 'https://www.youtube.com/watch?v=' + videoId
newCount = downloadedVideosCount + 1
with open("DownloadCount.txt", "a+") as file:
file.write("\n"+str(newCount))
post = requests.post(youtubeDLDomain, json={"url":link, "quality":"best", "format":"MP4"})
if post.status_code == 200:
logging.warning(str(datetime.now()) + "; SUCCESS; downloading video: " + link)
else:
logging.error(str(datetime.now()) + "; " + str(post.status_code) + ": Send to metube failed: " + link)
while 1:
downloadedVideosCount = 0
with open("DownloadCount.txt", "a+") as file:
file.seek(0)
line = file.readlines()
if len(line) == 0:
file.write(alreadyDownloadedCount)
else:
downloadedVideosCount = int(line[-1])
playlist_video_links(playlistId, downloadedVideosCount)