-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
103 lines (76 loc) · 2.48 KB
/
utils.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
#! /usr/bin/env python3.7
# Imports
from os.path import expanduser
from os.path import join as pjoin, isfile
from Crypto.Hash import SHA512
from re import compile as compile_regex
# Classes
class Song:
def __init__(self, title, artist = None, album = None):
if title:
self.title = str(title).title()
else: raise RuntimeError("You must specify a title for the song.")
self.artist = str(artist).title() if artist else None
self.album = str(album).title() if album else None
def __str__(self):
string = self.title
if self.album: string += ", " + self.album
if self.artist: string += " - " + self.artist
return string
def _filepath(self, fold = True): # return the preferred filename
string = ""
if fold:
string = pjoin(string, self.artist if self.artist else "Unknown Artist")
string = pjoin(string, self.album if self.album else "Unknown Album")
string = pjoin(string, self.title)
else:
if self.artist: string += self.artist + " - "
string += self.title
return string + "." + OUTPUT_FORMAT
# Settings
MUSIC_DOWNLOAD_DIRECTORY = expanduser("~/Music/Downloaded/")
DOWNLOADED_VIDEOS_HASHES_FILEPATH = MUSIC_DOWNLOAD_DIRECTORY + ".hashes.lst"
IGNORE_VIDEOS_HASHES_FILEPATH = MUSIC_DOWNLOAD_DIRECTORY + ".ignore.lst"
OUTPUT_FORMAT = "mp3"
# Costants
VIDEO_URL = "https://www.youtube.com/watch?v=" # video's url first part
SONGS_REGEX = r"(?P<title>[\w\ \']+)\ \-\ (?P<artist>[\w\ \.\']+)" # this is the input format for the songs
# Functions
def add_hash(url, hashes_file):
"""
Add the hash to the downloaded files hash list
"""
vid = url.split(VIDEO_URL)[1]
id_hash = SHA512.new( str(vid).encode() ).hexdigest().upper()
f = open(hashes_file, 'a')
f.write( f"{id_hash}\n" )
f.close()
def match_hash(vid, hashes_file):
"""
Match the video id in the provided file
"""
id_hash = SHA512.new( str(vid).encode() ).hexdigest().upper()
if not isfile(hashes_file):
print( f"Hashes file {hashes_file} not found." )
return False
hf = open(hashes_file, 'r')
for hline in hf.readlines():
if hline.strip() == id_hash: return True
return False
def load_songs_from_file(filename):
"""
Load a list of songs from a file
"""
sf = open(filename, 'r')
lines = sf.readlines()
sf.close()
sgs = list()
for s in lines:
if not s.strip(): continue # skip empty lines
matched_desc = compile_regex(SONGS_REGEX).match(s)
if matched_desc:
song = Song( *matched_desc.groups() )
sgs.append(song)
else:
print( f"This line is not well formatted: {s}" )
return sgs