Skip to content

Instantly share code, notes, and snippets.

@w1ndy
Last active August 29, 2015 14:23
Show Gist options
  • Save w1ndy/b8bbc75d3e9bc30cd011 to your computer and use it in GitHub Desktop.
Save w1ndy/b8bbc75d3e9bc30cd011 to your computer and use it in GitHub Desktop.
import requests
import re
import math
import traceback
from bs4 import BeautifulSoup
UID = '' # Put your UID here
member_auth = '' # Put your "member_auth" cookie here
ItemsPerPage = 25
XiamiUrl = 'http://www.xiami.com/space/lib-song/u/%s/page/%d'
UserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36'
def getProfilePages():
ItemCountHint = '\u7b2c\\d+\u9875, \u5171(\\d+)\u6761'
print('getting', XiamiUrl % (UID, 1))
s = requests.Session()
s.headers['user-agent'] = UserAgent
s.cookies['member_auth'] = member_auth
r = s.get(XiamiUrl % (UID, 1), allow_redirects=False)
if r.status_code != 200:
raise Exception('error: server returned code %d' % r.status_code)
c = re.findall(ItemCountHint, r.text, re.U | re.S)
if not c:
raise Exception('error: invalid page', XiamiUrl % (UID, 1))
count = int(math.ceil(int(c[0]) / ItemsPerPage))
yield r.text
for i in range(2, count + 1):
print('getting', XiamiUrl % (UID, i))
r = s.get(XiamiUrl % (UID, i))
if r.status_code != 200:
raise Exception('error: server returned code %d' % r.status_code)
if not re.findall(ItemCountHint, r.text, re.U | re.S):
raise Exception('error: invalid page', XiamiUrl % (UID, i))
yield r.text
def newTrackTag(soup, name, artist):
tag_file = soup.new_tag('File')
tag_type = soup.new_tag('MediaFileType')
tag_type.string = '0'
tag_name = soup.new_tag('FileName')
tag_name.string = '%s - %s.mp3' % (artist, name)
tag_path = soup.new_tag('FilePath')
tag_path.string = '.'
tag_file.append(tag_type)
tag_file.append(tag_name)
tag_file.append(tag_path)
return tag_file
def main():
output = BeautifulSoup('<?xml version="1.0" encoding="utf-8"?>')
root = output.new_tag('List', ListName='Imported')
try:
for page in getProfilePages():
soup = BeautifulSoup(page)
tracks = soup.find_all('td', class_='song_name')
for t in tracks:
track_name = t.a.text
track_artist = t.find(class_='artist_name').text
root.append(newTrackTag(output, track_name, track_artist))
except:
traceback.print_exc()
else:
output.append(root)
open('output.kgl', 'w').write(output.prettify())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment