lyrics-fetcher/sources/com_bandcamp.py

49 lines
1.9 KiB
Python
Raw Permalink Normal View History

2021-11-17 22:44:26 +11:00
enabled=True
import requests as r
import utils as u
2021-11-17 22:44:26 +11:00
def bandget(band):
peg=r.get(f'https://{band}.bandcamp.com/').content.decode('utf-8') # Just assume success,
# I'll put error handling in later
# I'll also demand a real API later
albums=[]
needles=[('href="/album/','"','urlid'),
('img src="','"','coverurl'),
('class="title">\n ','\n','title')]
2022-01-03 18:02:22 +11:00
for needle in u.stringiter(peg,needles=needles):
2021-11-17 22:44:26 +11:00
albums.append(needle)
return albums
def albumget(band,album,mode=0):
# Returns track list or all tracks' lyrics, based on mode
peg=r.get(f'https://{band}.bandcamp.com/album/{album}').content.decode('utf-8')
tracks=[]
needles=[('rel="tracknum=','"','num'),
('a href="/track/','">','urlid'),
('span class="track-title">','</span>','title'),
('<span class="time secondaryText">\n \n ','\n','duration')]
# Despite the lyrics being in the pages, it's not actually safe to get them with this system because any track without lyrics will quietly delete all tracks after it, up to and including the next one with lyrics.
2022-01-03 18:02:22 +11:00
for needle in u.stringiter(peg,needles=needles):
2021-11-17 22:44:26 +11:00
tracks.append(needle)
return tracks
def index(band,album=''):
if album:
return albumget(band,album,0)
albums=bandget(band)
return {x:albumget(band,x,0) for x in map(lambda x:x['urlid'],albums)}
def lyrics(song,band='',album=''):
if not (band):
return "Bandcamp does not currently support song search (due to being a webscraper). You must specify the band that performed the song."
band=band.replace(' ',''); song=song.replace(' ','-')
peg=r.get(f'https://{band}.bandcamp.com/track/{song}')
if peg.status_code!=200: raise u.NotFound('song')
peg=peg.content.decode('utf-8')
2021-11-17 22:44:26 +11:00
needle=('<div class="tralbumData lyricsText">','</div>','lyrics')
try: lyrics=next(u.stringiter(peg,needle=needle))
except StopIteration: raise u.NotFound('lyrics')
2021-11-17 22:44:26 +11:00
lyrics=lyrics.replace('\r','').replace('\n','').replace('<br>','\n')
return lyrics