-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube-sync
executable file
·300 lines (229 loc) · 8.68 KB
/
youtube-sync
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
#||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Author: Shane 'SajeOne' Brown
# Date: 13/03/2016
# Description: Syncs a youtube playlist with a local folder
#||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import json
from pprint import pprint
import urllib.request as REQ
import urllib.error
import subprocess as sp
import os
from os.path import isfile, join, expanduser
import sys
import argparse
import re
import string
# LOCAL
from youtubesync.ytplaylist import YTPlaylist
# Get Directory of config.json
def getConfigDir():
xdgConfigRaw = os.getenv("XDG_CONFIG_HOME")
xdgConfig = str(xdgConfigRaw)
if not xdgConfigRaw:
if verbose:
print("Warning: XDG_CONFIG_HOME not defined, attempting config read from local directory")
# If config home not defined, default to free desktop spec
home_path = os.getenv("HOME")
xdgConfig = os.path.join(str(home_path), ".config")
if not os.path.exists(xdgConfig + "/youtubeSync"):
try:
os.makedirs(xdgConfig + "/youtubeSync")
except OSError:
print("Could not write to CONFIG directory. Not owned by you?")
return False
xdgConfig += "/youtubeSync"
return xdgConfig
# Load the config file into memory
def loadConfigFile(path):
try:
with open(path, "r") as configFile:
data = configFile.read()
jsonObj = json.loads(data)
return jsonObj
except (FileNotFoundError, IOError) as e:
return False
# Write default config in the case one doesn't exist
def writeDefaultConfig():
xdgConfig = getConfigDir()
musicDir = expanduser("~") + "/Music"
configList = {'playlistID': 'PUT_PLAYLIST_ID_HERE', 'googleAPIKey': 'PUT_KEY_HERE', 'destination': musicDir}
jsonSaveFile = json.dumps(configList, indent=4)
with open(xdgConfig + "/config.json", "w") as configFile:
print(jsonSaveFile, file=configFile)
print("Created default config at " + xdgConfig)
# Download youtube video at URL specified and convert to mp3 in path folder
def downloadVideo(url, path):
try:
proc = sp.Popen(['yt-dlp', '--extract-audio', '--audio-format', 'mp3', '--output', path + "/%(title)s.%(ext)s", url], stdout=sp.PIPE)
except FileNotFoundError as ex:
raise ex
result = proc.communicate()[0]
status = proc.returncode
if status == 0:
return True
return False
# Download Google API Json for playlist
def downloadPage(url):
try:
response = REQ.urlopen(url)
data = response.read()
except urllib.error.HTTPError as e:
data = e.read()
text = data.decode('utf-8')
return text
# Decode the JSON to python DICT
def decodeJson(encodedJson):
data = json.loads(encodedJson)
return data
# convert list to form without any special characters or whitespace
def convertListFileSystemNeutral(convList):
for index, value in enumerate(convList):
convList[index] = re.sub('[^a-zA-Z\d]+', '', os.path.splitext(convList[index])[0])
return convList
# convert string to form without any special characters or whitespace
def convertStringFileSystemNeutral(song):
song = re.sub('[^a-zA-Z\d]+', '', song)
return song
def getListDiff(list1, list2):
tempList = []
for item1 in list1:
item1Trunc = os.path.splitext(item1)[0]
item1Trunc = convertStringFileSystemNeutral(item1Trunc)
for item2 in list2:
item2 = convertStringFileSystemNeutral(item2)
if item1Trunc == item2:
tempList.append(item1)
return list(set(list1) - set(tempList))
# Get list of songs from directory
def getSongList(path):
songs = [f for f in os.listdir(path) if isfile(join(path, f))]
return songs
# Handle json errors from Google API response
def errorHandleJson(jsonResp):
reason = None
if "error" in jsonResp:
reason = jsonResp['error']['errors'][0]['reason']
if reason == "keyInvalid":
reason = "Invalid API Key"
elif reason == "playlistNotFound":
reason = "Could not find YouTube playlist"
return reason
# Parse program arguments
def parseArguments():
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--id3tag', help="Automatically sets up ID3 tags based on '-' delimiter. Requires optional dependency 'python-mutagen'", action="store_true")
parser.add_argument('-v', '--verbose', help="Includes skipped songs(Already downloaded and in active playlist)", action="store_true")
parser.add_argument('-s', '--simulate', help="Simulates without actually downloading, good for speed testing", action="store_true")
parser.add_argument('-c', '--config', help="Alternate config file location")
parser.add_argument('--noapi', help="uses alternative method to youtube data api to grab meta data", action="store_true")
args = parser.parse_args()
return args
# Parses the raw result from yt api into title:id dicts
def objResultParser(obj):
videos = list()
for item in obj['items']:
cur_video = dict()
cur_video['title'] = item['snippet']['title']
cur_video['id'] = item['snippet']['resourceId']['videoId']
videos.append(cur_video)
return videos
# Grab playlist meta using youtube data api
def getPlaylistDataAPI(url):
## Download page and decode data
video_bundle = list()
cur_url = url
while True:
page = downloadPage(cur_url)
jsonData = None
try:
jsonData = decodeJson(page)
except json.decoder.JSONDecodeError:
print("Error: Could not decode JSON response, ensure config.json is setup properly")
sys.exit(1)
response = errorHandleJson(jsonData)
if response is not None:
print("Error: " + response)
sys.exit(1)
cur_videos = objResultParser(jsonData)
video_bundle = video_bundle + cur_videos
if not 'nextPageToken' in jsonData.keys():
break
cur_url = url + "&pageToken=" + jsonData['nextPageToken']
return video_bundle
# BEGIN EXECUTION
## Handle Arguments
args = parseArguments()
tagFiles = False
verbose = False
simulate = False
if args.id3tag:
tagFiles = True
if args.verbose:
verbose = True
if args.simulate:
simulate = True
## Handle Configuration
xdgConfig = getConfigDir() + "/config.json"
if not xdgConfig:
sys.exit(1)
if args.config == None or not isfile(args.config):
config = loadConfigFile(xdgConfig)
else:
config = loadConfigFile(args.config)
if not config:
writeDefaultConfig()
print("No config, wrote default. Ensure to edit appropriately")
sys.exit(1)
if args.noapi:
jsonData = YTPlaylist.fetchPlaylist(config['playlistID'])
if jsonData != None and jsonData != False:
print("Alternative Data Fetch Success")
else:
url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=" + config['playlistID'] + "&key=" + config['googleAPIKey'] + "&maxResults=50"
jsonData = getPlaylistDataAPI(url)
songs = getSongList(config['destination'])
neutralSongs = convertListFileSystemNeutral(songs)
curSongList = []
## Loop through items and compare repos
for item in jsonData:
curSongList.append(item['title'])
neutralSnippet = convertStringFileSystemNeutral(item['title'])
if not neutralSnippet in neutralSongs:
print("Downloading " + item['title'] + "..")
try:
if not simulate and not downloadVideo("https://youtube.com/watch?v=" + item['id'], config['destination']):
print("\n--DOWNLOAD FAILED--\n")
except FileNotFoundError:
print("youtube-dl not found, aborting..")
sys.exit(1)
else:
if verbose:
print("SKIPPING: ", end="")
print(item['title'].encode('utf-8'))
## Delete songs removed from remote
songs = getSongList(config['destination'])
songsForDeletion = getListDiff(songs, curSongList)
for item in songsForDeletion:
try:
if not args.simulate:
os.remove(config['destination'] + "/" + item)
print("Removed " + item)
except FileNotFoundError:
print("Could not remove " + item)
## If -t flag add ID3 tags to files
if tagFiles:
if os.path.isfile("/usr/bin/mp3tags"):
proc = sp.Popen(['mp3tags', '-p', config['destination']], stdout=sp.PIPE, stderr=sp.PIPE)
elif os.path.isfile(os.path.dirname(os.path.realpath(__file__)) + "/mp3tags.py"):
proc = sp.Popen(['python', 'mp3tags.py', '-p', config['destination']], stdout=sp.PIPE, stderr=sp.PIPE)
else:
print("Could not find mp3tags in /usr/bin or mp3tags.py in youtube-sync directory. Aborting tagging..")
sys.exit(1)
result = proc.communicate()[1]
result = str(result).replace('\\n', '\n').replace("\\\'", '\'')
if not proc.returncode:
print("ID3 Tagged Files")
else:
print("\nFailed to tag files\n")