Skip to content

Commit 10bdc8d

Browse files
committed
Close #100 - Set useragent in header
1 parent 4d3a34b commit 10bdc8d

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

modules/search.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cherrypy
22
import htpc
33
from htpc.proxy import get_image
4-
from urllib2 import urlopen, quote
4+
import urllib2
55
from json import loads
66
import logging
77

@@ -23,15 +23,6 @@ def __init__(self):
2323
def index(self, query='', **kwargs):
2424
return htpc.LOOKUP.get_template('search.html').render(query=query, scriptname='search')
2525

26-
"""
27-
NOT IMPLEMENTET
28-
@cherrypy.expose()
29-
@cherrypy.tools.json_out()
30-
def ping(self, newznab_host, newznab_apikey, **kwargs):
31-
self.logger.debug("Pinging newznab-host")
32-
return 1
33-
"""
34-
3526
@cherrypy.expose()
3627
def thumb(self, url, h=None, w=None, o=100):
3728
if url.startswith('rageid'):
@@ -54,7 +45,7 @@ def getcategories(self, **kwargs):
5445
def search(self, q='', cat='', **kwargs):
5546
if cat:
5647
cat = '&cat=' + cat
57-
result = self.fetch('search&q=' + quote(q) + cat + '&extended=1')
48+
result = self.fetch('search&q=' + urllib2.quote(q) + cat + '&extended=1')
5849
try:
5950
return result['channel']['item']
6051
except:
@@ -66,11 +57,15 @@ def fetch(self, cmd):
6657
host = settings.get('newznab_host', '').replace('http://', '').replace('https://', '')
6758
ssl = 's' if settings.get('newznab_ssl', 0) else ''
6859
apikey = settings.get('newznab_apikey', '')
69-
7060
url = 'http' + ssl + '://' + host + '/api?o=json&apikey=' + apikey + '&t=' + cmd
71-
7261
self.logger.debug("Fetching information from: " + url)
73-
return loads(urlopen(url, timeout=10).read())
62+
request = urllib2.Request(url)
63+
request.add_header('User-agent', 'HTPC Manager')
64+
try:
65+
resource = urllib2.urlopen(request)
66+
return loads(resource.read())
67+
except urllib2.HTTPError, err:
68+
self.logger.error("HTTP Error Code Received: " + str(err.code))
7469
except:
7570
self.logger.error("Unable to fetch information from: " + url)
7671
return

0 commit comments

Comments
 (0)