MythTV master
lyricsScraper.py
Go to the documentation of this file.
1#-*- coding: UTF-8 -*-
2'''
3Scraper for http://music.163.com/
4
5osdlyrics
6'''
7
8import os
9import requests
10import re
11import random
12import difflib
13from lib.utils import *
14
15__title__ = "Music163"
16__priority__ = '120'
17__lrc__ = True
18
19headers = {}
20headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0'
21
22
24 def __init__(self, *args, **kwargs):
25 self.DEBUG = kwargs['debug']
26 self.settings = kwargs['settings']
27 self.SEARCH_URL = 'http://music.163.com/api/search/get'
28 self.LYRIC_URL = 'http://music.163.com/api/song/lyric'
29
30 def get_lyrics(self, song):
31 log("%s: searching lyrics for %s - %s" % (__title__, song.artist, song.title), debug=self.DEBUG)
32 lyrics = Lyrics(settings=self.settings)
33 lyrics.song = song
34 lyrics.source = __title__
35 lyrics.lrc = __lrc__
36 artist = song.artist.replace(' ', '+')
37 title = song.title.replace(' ', '+')
38 search = '?s=%s+%s&type=1' % (artist, title)
39 try:
40 url = self.SEARCH_URL + search
41 response = requests.get(url, headers=headers, timeout=10)
42 result = response.json()
43 except:
44 return None
45 links = []
46 if 'result' in result and 'songs' in result['result']:
47 for item in result['result']['songs']:
48 artists = "+&+".join([a["name"] for a in item["artists"]])
49 if (difflib.SequenceMatcher(None, artist.lower(), artists.lower()).ratio() > 0.6) and (difflib.SequenceMatcher(None, title.lower(), item['name'].lower()).ratio() > 0.8):
50 links.append((artists + ' - ' + item['name'], self.LYRIC_URL + '?id=' + str(item['id']) + '&lv=-1&kv=-1&tv=-1', artists, item['name']))
51 if len(links) == 0:
52 return None
53 elif len(links) > 1:
54 lyrics.list = links
55 for link in links:
56 lyr = self.get_lyrics_from_list(link)
57 if lyr and lyr.startswith('['):
58 lyrics.lyrics = lyr
59 return lyrics
60 return None
61
62 def get_lyrics_from_list(self, link):
63 title,url,artist,song = link
64 try:
65 log('%s: search url: %s' % (__title__, url), debug=self.DEBUG)
66 response = requests.get(url, headers=headers, timeout=10)
67 result = response.json()
68 except:
69 return None
70 if 'lrc' in result:
71 return result['lrc']['lyric']
None log(str msg, int level=LOGDEBUG)
Definition: xbmc.py:9