Fix bugs, set correct headers, etc.

master
Sven Slootweg 11 years ago
parent eee7a0d253
commit 0644f18201

@ -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

@ -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)

@ -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('<input[^>]*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)

Loading…
Cancel
Save