[booblyrics] able to search by artist or songtitle
This commit is contained in:
parent
74e6e30b55
commit
0628802b72
6 changed files with 66 additions and 18 deletions
|
|
@ -42,5 +42,5 @@ class SeeklyricsBackend(BaseBackend, ICapLyrics):
|
||||||
def get_lyrics(self, id):
|
def get_lyrics(self, id):
|
||||||
return self.browser.get_lyrics(id)
|
return self.browser.get_lyrics(id)
|
||||||
|
|
||||||
def iter_lyrics(self, pattern):
|
def iter_lyrics(self, criteria, pattern):
|
||||||
return self.browser.iter_lyrics(quote_plus(pattern.encode('iso-8859-1')))
|
return self.browser.iter_lyrics(criteria,quote_plus(pattern.encode('iso-8859-1')))
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
from weboob.tools.browser import BaseBrowser
|
from weboob.tools.browser import BaseBrowser
|
||||||
|
|
||||||
from .pages import ResultsPage, SonglyricsPage
|
from .pages import SongResultsPage, SonglyricsPage, ArtistResultsPage, ArtistSongsPage
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['SeeklyricsBrowser']
|
__all__ = ['SeeklyricsBrowser']
|
||||||
|
|
@ -32,13 +32,19 @@ class SeeklyricsBrowser(BaseBrowser):
|
||||||
ENCODING = 'iso-8859-1'
|
ENCODING = 'iso-8859-1'
|
||||||
USER_AGENT = BaseBrowser.USER_AGENTS['wget']
|
USER_AGENT = BaseBrowser.USER_AGENTS['wget']
|
||||||
PAGES = {
|
PAGES = {
|
||||||
'http://www.seeklyrics.com/search.php.*': ResultsPage,
|
'http://www.seeklyrics.com/search.php.*t=1': SongResultsPage,
|
||||||
'http://www.seeklyrics.com/lyrics/.*': SonglyricsPage,
|
'http://www.seeklyrics.com/search.php.*t=2': ArtistResultsPage,
|
||||||
|
'http://www.seeklyrics.com/lyrics/.*html': SonglyricsPage,
|
||||||
|
'http://www.seeklyrics.com/lyrics/.*/': ArtistSongsPage,
|
||||||
}
|
}
|
||||||
|
|
||||||
def iter_lyrics(self, pattern):
|
def iter_lyrics(self, criteria, pattern):
|
||||||
self.location('http://www.seeklyrics.com/search.php?q=%s&t=1' % pattern.encode('utf-8'))
|
if criteria == 'artist':
|
||||||
assert self.is_on_page(ResultsPage)
|
type = 2
|
||||||
|
else:
|
||||||
|
type = 1
|
||||||
|
self.location('http://www.seeklyrics.com/search.php?q=%s&t=%s' % (pattern,type))
|
||||||
|
assert self.is_on_page(ArtistResultsPage) or self.is_on_page(SongResultsPage)
|
||||||
return self.page.iter_lyrics()
|
return self.page.iter_lyrics()
|
||||||
|
|
||||||
def get_lyrics(self, id):
|
def get_lyrics(self, id):
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,34 @@ from weboob.capabilities.base import NotAvailable, NotLoaded
|
||||||
from weboob.tools.browser import BasePage
|
from weboob.tools.browser import BasePage
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['ResultsPage','SonglyricsPage']
|
__all__ = ['SongResultsPage','SonglyricsPage', 'ArtistResultsPage', 'ArtistSongsPage']
|
||||||
|
|
||||||
|
|
||||||
class ResultsPage(BasePage):
|
class ArtistResultsPage(BasePage):
|
||||||
|
def iter_lyrics(self):
|
||||||
|
for link in self.parser.select(self.document.getroot(),'table[title~=Results] a.tlink'):
|
||||||
|
artist = unicode(link.text_content())
|
||||||
|
self.browser.location('http://www.seeklyrics.com%s'%link.attrib.get('href',''))
|
||||||
|
assert self.browser.is_on_page(ArtistSongsPage)
|
||||||
|
for lyr in self.browser.page.iter_lyrics(artist):
|
||||||
|
yield lyr
|
||||||
|
|
||||||
|
|
||||||
|
class ArtistSongsPage(BasePage):
|
||||||
|
def iter_lyrics(self,artist):
|
||||||
|
for th in self.parser.select(self.document.getroot(),'th.text'):
|
||||||
|
txt = th.text_content()
|
||||||
|
if txt.startswith('Top') and txt.endswith('Lyrics'):
|
||||||
|
for link in self.parser.select(th.getparent().getparent(),'a.tlink'):
|
||||||
|
title = unicode(link.attrib.get('title','').replace(' Lyrics',''))
|
||||||
|
id = link.attrib.get('href','').replace('/lyrics/','').replace('.html','')
|
||||||
|
songlyrics = SongLyrics(id, title)
|
||||||
|
songlyrics.artist = artist
|
||||||
|
songlyrics.content = NotLoaded
|
||||||
|
yield songlyrics
|
||||||
|
|
||||||
|
|
||||||
|
class SongResultsPage(BasePage):
|
||||||
def iter_lyrics(self):
|
def iter_lyrics(self):
|
||||||
first = True
|
first = True
|
||||||
for tr in self.parser.select(self.document.getroot(),'table[title~=Results] tr'):
|
for tr in self.parser.select(self.document.getroot(),'table[title~=Results] tr'):
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,8 @@ from weboob.capabilities.base import NotLoaded
|
||||||
class SeeklyricsTest(BackendTest):
|
class SeeklyricsTest(BackendTest):
|
||||||
BACKEND = 'seeklyrics'
|
BACKEND = 'seeklyrics'
|
||||||
|
|
||||||
def test_search_n_get(self):
|
def test_search_song_n_get(self):
|
||||||
l_lyrics = list(self.backend.iter_lyrics('Complainte'))
|
l_lyrics = list(self.backend.iter_lyrics('song','Complainte'))
|
||||||
for songlyrics in l_lyrics:
|
for songlyrics in l_lyrics:
|
||||||
assert songlyrics.id
|
assert songlyrics.id
|
||||||
assert songlyrics.title
|
assert songlyrics.title
|
||||||
|
|
@ -36,3 +36,11 @@ class SeeklyricsTest(BackendTest):
|
||||||
assert full_lyr.artist
|
assert full_lyr.artist
|
||||||
assert full_lyr.content is not NotLoaded
|
assert full_lyr.content is not NotLoaded
|
||||||
|
|
||||||
|
|
||||||
|
def test_search_artist(self):
|
||||||
|
l_lyrics = list(self.backend.iter_lyrics('artist','boris vian'))
|
||||||
|
for songlyrics in l_lyrics:
|
||||||
|
assert songlyrics.id
|
||||||
|
assert songlyrics.title
|
||||||
|
assert songlyrics.artist
|
||||||
|
assert songlyrics.content is NotLoaded
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ class Booblyrics(ReplApplication):
|
||||||
COMMANDS_FORMATTERS = {'search': 'lyrics_list',
|
COMMANDS_FORMATTERS = {'search': 'lyrics_list',
|
||||||
'get': 'lyrics_get',
|
'get': 'lyrics_get',
|
||||||
}
|
}
|
||||||
|
SEARCH_CRITERIAS = ['artist','song']
|
||||||
|
|
||||||
def complete_get(self, text, line, *ignored):
|
def complete_get(self, text, line, *ignored):
|
||||||
args = line.split(' ')
|
args = line.split(' ')
|
||||||
|
|
@ -99,17 +100,23 @@ class Booblyrics(ReplApplication):
|
||||||
self.format(songlyrics)
|
self.format(songlyrics)
|
||||||
self.flush()
|
self.flush()
|
||||||
|
|
||||||
def do_search(self, pattern):
|
def complete_search(self, text, line, *ignored):
|
||||||
"""
|
args = line.split(' ')
|
||||||
search [PATTERN]
|
if len(args) == 2:
|
||||||
|
return self.SEARCH_CRITERIAS
|
||||||
|
|
||||||
Search lyrics.
|
def do_search(self, line):
|
||||||
"""
|
"""
|
||||||
|
search [artist | song] [PATTERN]
|
||||||
|
|
||||||
|
Search lyrics by artist name or by song title.
|
||||||
|
"""
|
||||||
|
criteria, pattern = self.parse_command_args(line, 2, 1)
|
||||||
self.change_path([u'search'])
|
self.change_path([u'search'])
|
||||||
if not pattern:
|
if not pattern:
|
||||||
pattern = None
|
pattern = None
|
||||||
|
|
||||||
self.start_format(pattern=pattern)
|
self.start_format(pattern=pattern)
|
||||||
for backend, songlyrics in self.do('iter_lyrics', pattern=pattern):
|
for backend, songlyrics in self.do('iter_lyrics', criteria, pattern):
|
||||||
self.cached_format(songlyrics)
|
self.cached_format(songlyrics)
|
||||||
self.flush()
|
self.flush()
|
||||||
|
|
|
||||||
|
|
@ -41,10 +41,13 @@ class ICapLyrics(IBaseCap):
|
||||||
"""
|
"""
|
||||||
Lyrics websites.
|
Lyrics websites.
|
||||||
"""
|
"""
|
||||||
def iter_lyrics(self, pattern):
|
def iter_lyrics(self, criteria, pattern):
|
||||||
"""
|
"""
|
||||||
Search lyrics and iterate on results.
|
Search lyrics by artist or by song
|
||||||
|
and iterate on results.
|
||||||
|
|
||||||
|
:param criteria: 'artist' or 'song'
|
||||||
|
:type criteria: str
|
||||||
:param pattern: pattern to search
|
:param pattern: pattern to search
|
||||||
:type pattern: str
|
:type pattern: str
|
||||||
:rtype: iter[:class:`SongLyrics`]
|
:rtype: iter[:class:`SongLyrics`]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue