MythTV  master
lyricsScraper.py
Go to the documentation of this file.
1 #-*- coding: UTF-8 -*-
2 '''
3 Scraper for http://music.163.com/
4 
5 osdlyrics
6 '''
7 
8 import os
9 import requests
10 import re
11 import random
12 import difflib
13 from lib.utils import *
14 
15 __title__ = "Music163"
16 __priority__ = '120'
17 __lrc__ = True
18 
19 headers = {}
20 headers['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']
culrcscrapers.music163.lyricsScraper.LyricsFetcher.get_lyrics_from_list
def get_lyrics_from_list(self, link)
Definition: lyricsScraper.py:62
utils
culrcscrapers.music163.lyricsScraper.LyricsFetcher.__init__
def __init__(self, *args, **kwargs)
Definition: lyricsScraper.py:24
culrcscrapers.music163.lyricsScraper.LyricsFetcher.LYRIC_URL
LYRIC_URL
Definition: lyricsScraper.py:28
culrcscrapers.music163.lyricsScraper.LyricsFetcher.settings
settings
Definition: lyricsScraper.py:26
culrcscrapers.music163.lyricsScraper.LyricsFetcher.SEARCH_URL
SEARCH_URL
Definition: lyricsScraper.py:27
culrcscrapers.music163.lyricsScraper.LyricsFetcher
Definition: lyricsScraper.py:23
xbmc.log
None log(str msg, int level=LOGDEBUG)
Definition: xbmc.py:9
culrcscrapers.music163.lyricsScraper.LyricsFetcher.DEBUG
DEBUG
Definition: lyricsScraper.py:25
culrcscrapers.music163.lyricsScraper.LyricsFetcher.get_lyrics
def get_lyrics(self, song)
Definition: lyricsScraper.py:30