lyrics-fetcher/sources/com_bandcamp.py

46 lines
1.8 KiB
Python
Raw Normal View History

2021-11-17 22:44:26 +11:00
enabled=True
import requests as r
2022-01-03 18:02:22 +11:00
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}').content.decode('utf-8')
needle=('<div class="tralbumData lyricsText">','</div>','lyrics')
2022-01-03 18:02:22 +11:00
lyrics=next(u.stringiter(peg,needle=needle))
2021-11-17 22:44:26 +11:00
lyrics=lyrics.replace('\r','').replace('\n','').replace('<br>','\n')
return lyrics