brenns10 / tswift

MetroLyrics API for Python
BSD 3-Clause "New" or "Revised" License
52 stars 15 forks source link

AttributeError in tswift.py #10

Closed josephbwagner closed 5 years ago

josephbwagner commented 5 years ago

Using this code:

from tswift import Artist
import random

tswift = Artist('Taylor Swift')
song = random.choice(tswift.songs)
print(song.format())

I get the following traceback:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-2341cd82ac4a> in <module>()
      3 
      4 tswift = Artist('Taylor Swift')
----> 5 song = random.choice(tswift.songs)
      6 print(song.format())

~/anaconda3/envs/python3/lib/python3.6/site-packages/tswift.py in songs(self)
    148     def songs(self):
    149         if self._songs is None:
--> 150             self.load()
    151         return self._songs
    152 

~/anaconda3/envs/python3/lib/python3.6/site-packages/tswift.py in load(self, verbose)
    139                 song_link = row.xpath(r'./td/a[contains(@class,"title")]')
    140                 assert len(song_link) == 1
--> 141                 self._songs.append(Song(url=song_link[0].attrib['href']))
    142 
    143             total_pages = len(tree.xpath(songlist_pagination_xp))

~/anaconda3/envs/python3/lib/python3.6/site-packages/tswift.py in __init__(self, title, artist, url)
     45         if url is not None:
     46             self._url = url
---> 47             self.title, self.artist = re.match(SONG_RE, url).groups()
     48         elif title is not None and artist is not None:
     49             self.title = title

AttributeError: 'NoneType' object has no attribute 'groups'

I don't know the cause of the error, but I suspect the MetroLyrics API changed? I've been using your your package for almost a year (thank you its awesome!) and I've never had any issues with it in the past.

brenns10 commented 5 years ago

Hi @beverast, glad you've found the package useful! It looks like Metrolyrics decided to switch all their URLs to HTTPS, and my regex wasn't general enough to accept those. I fixed the issue, verified the example code runs correctly, and released v0.6.0 on PyPI with the fix!