[booblyrics] able to search by artist or songtitle

This commit is contained in:
Julien Veyssier 2013-03-08 13:36:39 +01:00
commit 0628802b72
6 changed files with 66 additions and 18 deletions

View file

@ -42,5 +42,5 @@ class SeeklyricsBackend(BaseBackend, ICapLyrics):
def get_lyrics(self, id):
return self.browser.get_lyrics(id)
def iter_lyrics(self, pattern):
return self.browser.iter_lyrics(quote_plus(pattern.encode('iso-8859-1')))
def iter_lyrics(self, criteria, pattern):
return self.browser.iter_lyrics(criteria,quote_plus(pattern.encode('iso-8859-1')))

View file

@ -20,7 +20,7 @@
from weboob.tools.browser import BaseBrowser
from .pages import ResultsPage, SonglyricsPage
from .pages import SongResultsPage, SonglyricsPage, ArtistResultsPage, ArtistSongsPage
__all__ = ['SeeklyricsBrowser']
@ -32,13 +32,19 @@ class SeeklyricsBrowser(BaseBrowser):
ENCODING = 'iso-8859-1'
USER_AGENT = BaseBrowser.USER_AGENTS['wget']
PAGES = {
'http://www.seeklyrics.com/search.php.*': ResultsPage,
'http://www.seeklyrics.com/lyrics/.*': SonglyricsPage,
'http://www.seeklyrics.com/search.php.*t=1': SongResultsPage,
'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):
self.location('http://www.seeklyrics.com/search.php?q=%s&t=1' % pattern.encode('utf-8'))
assert self.is_on_page(ResultsPage)
def iter_lyrics(self, criteria, pattern):
if criteria == 'artist':
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()
def get_lyrics(self, id):

View file

@ -23,10 +23,34 @@ from weboob.capabilities.base import NotAvailable, NotLoaded
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):
first = True
for tr in self.parser.select(self.document.getroot(),'table[title~=Results] tr'):

View file

@ -23,8 +23,8 @@ from weboob.capabilities.base import NotLoaded
class SeeklyricsTest(BackendTest):
BACKEND = 'seeklyrics'
def test_search_n_get(self):
l_lyrics = list(self.backend.iter_lyrics('Complainte'))
def test_search_song_n_get(self):
l_lyrics = list(self.backend.iter_lyrics('song','Complainte'))
for songlyrics in l_lyrics:
assert songlyrics.id
assert songlyrics.title
@ -36,3 +36,11 @@ class SeeklyricsTest(BackendTest):
assert full_lyr.artist
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