From 0644f182013b04aae89b10f22a7859282b2a3f46 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sun, 30 Jun 2013 20:13:27 +0200 Subject: [PATCH] Fix bugs, set correct headers, etc. --- main.py | 1 + shared.py | 10 ++++++++-- sources/binsearch.py | 13 +++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 49e5f3d..f9f78e9 100644 --- a/main.py +++ b/main.py @@ -137,6 +137,7 @@ for release in releases: except Exception, e: errors += 1 sys.stderr.write("Downloading NZB for %s failed: %s\n" % (release_name, repr(e))) + continue sys.stdout.write("Downloaded NZB for %s.\n" % release_name) downloaded += 1 diff --git a/shared.py b/shared.py index 3daa11e..4bbce69 100644 --- a/shared.py +++ b/shared.py @@ -11,11 +11,15 @@ user_agents = [ "Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0", "Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1", + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.63 Safari/537.36" ] class NotFoundException(Exception): pass +class DownloadException(Exception): + pass + # Very nasty monkeypatching ahead! socket.real_create_connection = socket.create_connection @@ -28,7 +32,7 @@ class ModifiedSession(requests.Session): self.bound_ip = "" requests.Session.__init__(self, *args, **kwargs) - self.headers['user-agent'] = random.choice(user_agents) + self.headers['User-Agent'] = random.choice(user_agents) def patch_socket(self): socket.create_connection = get_patched_func(self.bound_ip) @@ -44,7 +48,7 @@ class ModifiedSession(requests.Session): def post(self, *args, **kwargs): self.patch_socket() - response = requests.Session.get(self, *args, **kwargs) + response = requests.Session.post(self, *args, **kwargs) self.unpatch_socket() return response @@ -66,3 +70,5 @@ def download_file(request, target): f.write(chunk) f.close() + else: + raise DownloadException("Status code was %s" % request.status_code) diff --git a/sources/binsearch.py b/sources/binsearch.py index 61d1b38..932d720 100644 --- a/sources/binsearch.py +++ b/sources/binsearch.py @@ -21,7 +21,8 @@ class BinsearchSpider(object): "adv_col": "on", "adv_nfo": "on", "font": "", - "postdate": "" + "postdate": "", + "server": "" }, verify=False) search_results = [] @@ -45,7 +46,7 @@ class BinsearchSpider(object): match = re.search(']*type="checkbox"[^>]*name="([0-9]+)"[^>]*>', result) if match is not None: - search_results.append(BinsearchResult(name, title, match.group(1), self)) + search_results.append(BinsearchResult(name, title, match.group(1), self, response.url)) if len(search_results) == 0: raise NotFoundException("No results were found.") @@ -53,11 +54,12 @@ class BinsearchSpider(object): return search_results class BinsearchResult(object): - def __init__(self, name, title, id_, spider): + def __init__(self, name, title, id_, spider, searchurl): self.name = name self.title = title self.id_ = id_ self.spider = spider + self.searchurl = searchurl def show(self): print "%s -> %s (%s)" % (self.title, self.id_, self.name) @@ -66,6 +68,8 @@ class BinsearchResult(object): data_dict = {"action": "nzb"} data_dict[self.id_] = "on" + self.spider.session.headers['Referer'] = self.searchurl + response = self.spider.session.post("https://www.binsearch.info/fcgi/nzb.fcgi", params={ "q": self.name, "m": "", @@ -78,7 +82,8 @@ class BinsearchResult(object): "adv_col": "on", "adv_nfo": "on", "font": "", - "postdate": "" + "postdate": "", + "server": "" }, data=data_dict) download_file(response, target_path)