Skip to content

Instantly share code, notes, and snippets.

@pzread pzread/getcdinfo.py
Created Mar 7, 2016

Embed
What would you like to do?
import re
import requests
from bs4 import BeautifulSoup
def to_bom_utf8(name):
data = open(name, 'rb').read().decode('utf-8')
open(name, 'wb').write(data.encode('utf-8-sig'))
def parse_site(url):
resp = requests.get(url)
text = resp.text.replace('</br>', '')
soup = BeautifulSoup(text, 'html.parser')
tracklist = soup.select('div.tracklist')[0]
trackmap = dict()
for discidx, table in enumerate(tracklist.find_all('table')):
for tr in table.find_all('tr'):
parts = tr.find_all('td')
trackidx = int(parts[0].text.rstrip('\r\n ').lstrip('\r\n '))
name = parts[1].text.rstrip('\r\n ').lstrip('\r\n ')
artist = parts[2].text.rstrip('\r\n ').lstrip('\r\n ')
trackmap['%d-%d'%(discidx + 1, trackidx)] = {
'artist': artist,
'name': name,
}
return trackmap
def trans_cues(names, trackmap):
for discidx, name in enumerate(names):
cue = open(name, 'r')
outf = open(name + '.fix', 'w')
for line in cue:
line = line.rstrip('\r\n')
ind = re.findall('TRACK ([0-9]+) AUDIO', line)
if len(ind) != 1:
outf.write(line + '\r\n')
continue
outf.write(line + '\r\n')
trackidx = int(ind[0])
next(cue)
next(cue)
timestamp = next(cue).lstrip(' ').rstrip('\r\n')
data = trackmap['%d-%d'%(discidx + 1, trackidx)]
outf.write(' TITLE "%s"\r\n'%data['name'])
outf.write(' PERFORMER "%s"\r\n'%data['artist'])
outf.write(' ' * 4 + timestamp + '\r\n')
outf.close()
to_bom_utf8(name + '.fix')
def main():
trackmap = parse_site('https://diverse.direct/diverse-system/dvsp-0132/')
trans_cues([
'''Diverse System - AD House 4 disc1.cue''',
'''Diverse System - AD House 4 disc2.cue''',
], trackmap)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.