From 0970689df9ca779e6a044b7c6143cece7852f457 Mon Sep 17 00:00:00 2001 From: Aaron Cohen Date: Wed, 12 Sep 2012 01:43:53 -0700 Subject: [PATCH] IT'S ALIIIIIVE!!! --- headphones/helpers.py | 7 ++++++- headphones/searcher.py | 28 +++++++++++++++++----------- lib/whatapi.py | 18 +++++++++++++++--- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/headphones/helpers.py b/headphones/helpers.py index 7f5cbab9..211bf637 100644 --- a/headphones/helpers.py +++ b/headphones/helpers.py @@ -123,7 +123,12 @@ def bytes_to_mb(bytes): mb = int(bytes)/1048576 size = '%.1f MB' % mb return size - + +def mb_to_bytes(mb_str): + result = re.search('^(\d+(?:\.\d+)?)\s?(?:mb)?', mb_str, flags=re.I) + if result: + return int(float(result.group(1))*1048576) + def replace_all(text, dic): for i, j in dic.iteritems(): text = text.replace(i, j) diff --git a/headphones/searcher.py b/headphones/searcher.py index d54d6d76..757bc4dc 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -809,6 +809,7 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False): if headphones.WHATCD: provider = "What.cd" + providerurl = "http://what.cd/" bitrate = None if headphones.PREFERRED_QUALITY == 3 or losslessOnly: @@ -838,40 +839,45 @@ def searchTorrent(albumid=None, new=False, losslessOnly=False): if artist_id: # will be None if artist not found logger.info(u"What.cd artist ID: %s" % artist_id) artist_releases = artist.getArtistReleases() - logger.info(u"Found %d releases on what.cd for %s" % (len(artist_releases), artistterm)) + logger.info(u"Found %d releases on %s for %s" % (len(artist_releases), provider, artistterm)) #Returns a list with all artist's releases in form of dictionary {releasetype, year, name, id} else: artist_releases = [] - possible_matches = [ release for release in artist_releases if albumterm in release['name'] ] + logger.info(u"Loading information about available torrents (this may take a while)") + release_torrent_groups = [ whatcd.getTorrentGroup(release['id']) for release in artist_releases if albumterm in release['name'] ] + all_children = [] + for group in release_torrent_groups: + all_children += group.torrentinfo['torrent']['childrenids'] # cap at 10 matches, 1 per second to reduce hits on API...don't wanna get in trouble. # Might want to turn up number of matches later. - max_torrent_info_reads = 10 +# max_torrent_info_reads = 10 info_read_rate = 1 match_torrents = [] - for i, release in enumerate(possible_matches[:max_torrent_info_reads]): + for i, child_id in enumerate(all_children): if i > 0: time.sleep(info_read_rate) - match_torrents.append(whatcd.getTorrent(release['id'])) + match_torrents.append(whatcd.getTorrent(child_id)) # filter on format, size, and num seeders match_torrents = [ torrent for torrent in match_torrents if re.search(format_regex, torrent.getTorrentDetails(), flags=re.I) - and torrent.getTorrentSize() <= maxsize - and torrent.getTorrentSeeders() >= minimumseeders ] + and helpers.mb_to_bytes(torrent.getTorrentSize()) <= maxsize + and int(torrent.getTorrentSeeders()) >= minimumseeders ] # sort by times d/l'd - if not len(possible_matches): + if not len(match_torrents): logger.info(u"No results found from %s for %s after filtering" % (provider, term)) elif len(match_torrents) > 1: - match_torrents.sort(match_torrents, key=whatapi.Torrent.getTorrentSeeders) + logger.info(u"Found %d matching releases from %s for %s after filtering" % (len(match_torrents), provider, artistterm)) + match_torrents.sort(key=lambda x: x.getTorrentSeeders) for torrent in match_torrents: resultlist.append((torrent.getTorrentFolderName(), - torrent.getTorrentSize(), - torrent.getTorrentDownloadURL(), + helpers.mb_to_bytes(torrent.getTorrentSize()), + providerurl + torrent.getTorrentDownloadURL(), provider)) if headphones.ISOHUNT: diff --git a/lib/whatapi.py b/lib/whatapi.py index b6f1b956..66c0d009 100755 --- a/lib/whatapi.py +++ b/lib/whatapi.py @@ -785,6 +785,15 @@ class Torrent(WhatBase): if self.torrentinfo: return self.torrentinfo['torrent']['parentid'] + def getTorrentChildren(self): + """ + Returns list of children if is a torrent group, else returns own id in list + """ + if self.isParent: + return self.torrentinfo['torrent']['childrenids'] + else: + return [self.id] + def getTorrentDownloadURL(self): """ Returns relative url to download the torrent @@ -2503,11 +2512,9 @@ class Parser(object): self.debugMessage("not found href to retrieve user id") else: userInfo["id"] = regid.search(hrefid).group(0) - print "User id: %s" % userInfo["id"] #retrieve user logged id hrefauth = ul.findAll('li')[2].find("a")["href"] - print hrefauth regauth = re.compile('=[0-9a-zA-Z]+') if regid.search(hrefid) is None: self.debugMessage("not found href to retrieve user id") @@ -2606,6 +2613,11 @@ class Parser(object): soup = BeautifulSoup(str(dom)) if isparent: torrentInfo['torrent']['parentid'] = id + torrentInfo['torrent']['childrenids'] = [] + for torrent in soup.findAll('tr', {'class':re.compile(r'\bgroupid_%s.+edition_\d.+group_torrent' % id)}): + child_id = re.search('\d+$', torrent['id']).group(0) + if child_id: + torrentInfo['torrent']['childrenids'].append(child_id) else: groupidurl = soup.findAll('div', {'class':'linkbox'})[0].find('a')['href'] torrentInfo['torrent']['editioninfo'] = soup.findAll('td', {'class':'edition_info'})[0].find('strong').contents[-1] @@ -2694,7 +2706,7 @@ class Parser(object): tagsartist = [] similarartists = [] soup = BeautifulSoup(str(dom)) - for releasetype in soup.fetch('table', {'class':'torrent_table'}): + for releasetype in soup.fetch('table', {'class':'torrent_table grouped release_table'}): releasetypenames = releasetype.findAll('strong') releasetypename = releasetype.findAll('strong')[0].string for release in releasetypenames[1:-1]: