-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
203 lines (162 loc) · 5.55 KB
/
test.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
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
import time
def RateLimited(maxPerSecond):
minInterval = 1.0 / float(maxPerSecond)
def decorate(func):
lastTimeCalled = [0.0]
def rateLimitedFunction(*args,**kargs):
elapsed = time.clock() - lastTimeCalled[0]
leftToWait = minInterval - elapsed
if leftToWait>0:
time.sleep(leftToWait)
ret = func(*args,**kargs)
lastTimeCalled[0] = time.clock()
return ret
return rateLimitedFunction
return decorate
@RateLimited(2) # 2 per second at most
def PrintNumber(num):
print(num)
""" rate = 5.0 # unit: messages
per = 8.0 # unit: seconds
allowance = rate # unit: messages
last_check = now() # floating-point, e.g. usec accuracy. Unit: seconds
when (message_received):
current = now()
time_passed = current - last_check
last_check = current
allowance += time_passed * (rate / per)
if (allowance > rate):
allowance = rate # throttle
if (allowance < 1.0):
discard_message()
else:
forward_message()
allowance -= 1.0
if __name__ == "__main__":
print("This should print 1,2,3... at about 2 per second.")
for i in range(1,100):
PrintNumber(i)
"""
"""
from mutagen.easyid3 import EasyID3
audio = EasyID3('C:/Users/Joe/Desktop/Oliver Tree/Ugly is Beautiful (2020)/01. Me, Myself & I.mp3')
print(audio)
title = audio['title'][0]
artist = (audio['artist'] or audio['albumartist'])[0]
print('Extracted: {} - {}'.format(artist, title)) """
file = 'C:\\Users\\Joe\\Documents\\Projects\\Python\\lyricscrapper\\tests\\songs\\01 Hit Parade.flac'
from mutagen.easyid3 import EasyID3
from mutagen.apev2 import APEv2
from mutagen.id3 import ID3, SYLT, USLT, Encoding
from mutagen.oggvorbis import OggVorbis
from mutagen import File
from mutagen.flac import FLAC
""" audio = EasyID3(file)
print(audio) """
#ogg = OggVorbis(file)
#print(ogg.get('TITLE'))
m = File(file)
if isinstance(m, FLAC):
print('this is a flac file')
print(m)
for tag in ('TPE1', 'TPE2', u'©ART', 'Author', 'Artist', 'artist', 'ARTIST', 'TRACK ARTIST', 'TRACKARTIST', 'TrackArtist', 'Track Artist'):
try:
artist = m[tag][0]
break
except KeyError:
pass
except ValueError:
pass
for tag in ('lyrics:description', 'USLT:description', 'LYRICS', 'Lyrics', '©lyr', 'WM/Lyrics'):
try:
m[tag] = 'Test'
break
except KeyError:
pass
except ValueError:
pass
m.save()
exit()
# Update webdriver code
chrome_url = 'https://chromedriver.storage.googleapis.com/index.html' # Needs JS, out of question
import requests
from bs4 import BeautifulSoup
import wget
import os
import zipfile
import shutil
from selenium import webdriver
import stat
def handleError(func, path, exc_info):
print('Handling Error for file ' , path)
print(exc_info)
os.chmod(path, stat.S_IWUSR)
# Check if file access issue
if not os.access(path, os.W_OK):
# Try to change the permision of file
os.chmod(path, stat.S_IWUSR)
# call the calling function again
func(path)
def cleanup():
# Clear any temp directories created. Should be temp-XX.X.XXXX.XX
for dirpath, folders, files in os.walk('./', topdown=True):
for folder in folders:
if folder.startswith('temp-'):
shutil.rmtree(folder, onerror=handleError)
cleanup()
response = requests.get('https://chromedriver.chromium.org/downloads')
soup = BeautifulSoup(response.text, 'html.parser')
versions = []
direct_download_url = 'https://chromedriver.storage.googleapis.com/%s/'
platform = 'win' # win32, mac64, linux64
opts = webdriver.ChromeOptions()
opts.add_argument('--no-sandbox')
opts.add_argument("--disable-gpu")
opts.add_argument('log-level=3')
opts.add_argument("--window-size=1920,1200")
opts.add_argument("--ignore-certificate-errors")
opts.add_argument("--headless")
anchors = soup.find_all('a', {'style': 'background-color:transparent'})
for anchor in anchors:
if anchor.text.startswith('ChromeDriver '):
versions.append(anchor.text.replace('ChromeDriver ', ''))
valid_version = ''
file_ext = ''
for version in versions:
print('Checking against {}'.format(version))
if platform == 'win':
file_ext = '.exe'
url = (direct_download_url % version) + 'chromedriver_win32.zip'
elif platform == 'mac':
url = (direct_download_url % version) + 'chromedriver_mac64.zip'
else:
url = (direct_download_url % version) + 'chromedriver_linux64.zip'
# Create temp directory
temp_dir = os.path.abspath('./temp-%s/' % version)
print('temp dir: ', temp_dir)
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
filename = wget.download(url, out=temp_dir)
with zipfile.ZipFile(filename, 'r') as zip_ref:
zip_ref.extractall(temp_dir)
full_filename = os.path.join(temp_dir, 'chromedriver' + file_ext)
print(full_filename)
driver = None
try:
driver = webdriver.Chrome(full_filename, chrome_options=opts)
driver.get('https://github.com/')
driver.quit()
except:
print('\tFailed')
if driver is not None:
driver.quit()
continue
valid_version = version
break
print('Correct version: {}'.format(valid_version))
if len(valid_version) > 0:
# Copy from folder, to root directory, cleanup
temp_dir = os.path.abspath('./temp-%s/' % valid_version)
full_filename = os.path.join(temp_dir, 'chromedriver' + file_ext)
shutil.copyfile(full_filename, os.path.join('./', 'chromedriver' + file_ext))
cleanup()