-
Notifications
You must be signed in to change notification settings - Fork 1
/
bc.py
191 lines (146 loc) · 5.95 KB
/
bc.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
import asyncio
import os
import requests
import re
import bs4
from urllib.parse import urljoin
import tqdm
import urllib.parse
NORMALIZE_NAMES = True
INCLUDE_TRACK_NUM_IN_NAMES = False
downloaded_images = []
def getStream(url, prev_url=None):
"""Extremely light, dumb helper to get a stream from a url
Args:
url (str): Remote URL
prev_url (str, optional): Previous url, for relative resolution
Returns:
Requests stream
"""
url = urllib.parse.urljoin(prev_url, url)
stream = requests.get(url, stream=True)
stream.raise_for_status()
return stream
def _saveChunked(path, response):
"""Save a binary stream to a path. Dumb."""
try:
with open(path, 'wb') as file:
for chunk in response:
file.write(chunk)
except Exception:
# Clean up partial file
os.unlink(path)
raise
def saveStreamAs(stream, dest_path, nc=False, verbose=False):
"""Save a URL to a path as file
Args:
stream (stream): Stream
dest_path (str): Local path
Returns:
bool: Success
"""
from os import path, stat
stream_length = float(stream.headers.get("Content-Length", -1))
if path.isfile(dest_path):
if nc:
return False
if stream_length == stat(dest_path).st_size:
if verbose:
print("Not overwriting same-size file at", dest_path)
return False
else:
if verbose:
print("File sizes do not match for output", dest_path, ":", stream_length, "!=", stat(dest_path).st_size)
_saveChunked(dest_path, stream)
return True
def guessExtension(response):
from _data import mime2ext
content_type = response.headers.get("Content-Type")
ext_match = mime2ext.get(content_type.split(";")[0], "")
return ext_match
def normalizeFileName(string, repl="-", directory=False):
if directory:
return re.sub(r"^\.|\.+$", "", normalizeFileName(string, repl=repl, directory=False))
else:
return re.sub(r"[\\\\/:*?\"<>|\t]|\ +$", repl, string)
def normalizeTrackName(track_name):
track_name = "-".join(re.split(' ', track_name))
track_name = track_name.replace('&', 'and')
track_name = re.sub(r'[^a-zA-Z0-9-]', '', track_name)
track_name = re.sub(r'-{2,}', '-', track_name)
track_name = re.sub(r'^-+|-+$', '', track_name).lower()
return track_name
def getArgs():
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("--artists", nargs="*", default=[], help="Usernames")
ap.add_argument("--albums", nargs="*", default=[], help="Album URLs")
ap.add_argument("--tracks", nargs="*", default=[], help="Track URLs")
return ap.parse_args()
async def getMetadataFromArtist(artist):
artist_page_url = f"https://{artist}.bandcamp.com/music"
artist_page = bs4.BeautifulSoup(requests.get(artist_page_url).text, features="html.parser")
griditems = artist_page.findAll("li", class_="music-grid-item")
for griditem in tqdm.tqdm(griditems, desc=artist, unit="album"):
album_url = urljoin(artist_page_url, griditem.find("a").get("href"))
if "/track/" in album_url:
await getMetadataFromTrack(album_url, artist=artist, album="singles")
else:
await getMetadataFromAlbum(album_url, artist=artist)
async def getMetadataFromAlbum(album, artist=None):
track_page = bs4.BeautifulSoup(requests.get(album).text, features="html.parser")
tracks = track_page.findAll(itemprop="tracks") + track_page.findAll(class_="track_row_view")
for track in tqdm.tqdm(tracks, desc=album.split("/")[-1], unit="track"):
track_no = track.find(class_="track-number-col").text
try:
track_url = urljoin(album, track.find(class_="title").find("a").get("href"))
await getMetadataFromTrack(track_url, track_no, artist=artist)
except AttributeError:
print("ERROR!", artist, track, track_no)
async def getMetadataFromTrack(track, track_no=None, album=None, artist=None):
track_page = bs4.BeautifulSoup(requests.get(track).text, features="html.parser")
if not album:
album = track_page.find("span", class_="fromAlbum").text
try:
track_name = track_page.find("h2", class_="trackTitle").text.strip()
if NORMALIZE_NAMES:
track_name = normalizeTrackName(track_name)
if not track_name:
track_name = f"{track_no}"
if not artist:
artist = track_page.find("h3", class_="albumTitle").findAll("span")[1].text.strip()
image_url = track_page.find("a", class_="popupImage").get("href")
image_url = re.sub(r'_10\.jpg$', '_0', image_url) # Bandcamp HQ
except AttributeError as e:
print(e)
print(track)
raise
out_dir = os.path.join(
normalizeFileName(artist),
normalizeFileName(album)
)
os.makedirs(out_dir, exist_ok=True)
if INCLUDE_TRACK_NUM_IN_NAMES and track_no:
out_plainname = f"{track_no} {normalizeFileName(track_name)}"
else:
out_plainname = f"{normalizeFileName(track_name)}"
print(image_url, "->", out_dir, "as", out_plainname)
stream = getStream(image_url)
hash_ = hash(stream.content)
if hash_ not in downloaded_images:
saveStreamAs(
stream,
os.path.join(out_dir, out_plainname[:243] + guessExtension(stream)),
nc=True
)
downloaded_images.append(hash_)
return
if __name__ == "__main__":
args = getArgs()
albums = args.albums
for artist in args.artists:
asyncio.run(getMetadataFromArtist(artist))
for album in args.albums:
asyncio.run(getMetadataFromAlbum(album))
for track in args.tracks:
asyncio.run(getMetadataFromTrack(track))