Index: ahttp.py ================================================================== --- ahttp.py +++ ahttp.py @@ -10,22 +10,23 @@ # And a function to add trailings slashes on http URLs. # # -from compat2and3 import urllib2, urlencode, urlparse, cookielib, StringIO, xrange, PY3 -from gzip import GzipFile from config import conf, __print__, dbg import requests import copy -#-- chains to progress meter and status bar in main window +#-- hooks to progress meter and status bar in main window feedback = None -# sets either text or percentage, so may take two parameters +# Sets either text or percentage of main windows' status bar. +# +# Can either take a float parameter (e.g. 0.99 for % indicator) +# or text message. Alternatively two parameters to update both. def progress_feedback(*args): # use reset values if none given if not args: args = ["", 1.0] @@ -36,11 +37,11 @@ except: pass -# default HTTP headers for AJAX/POST request +# default HTTP headers for requests default_headers = { "User-Agent": "streamtuner2/2.1 (X11; U; Linux AMD64; en; rv:1.5.0.1) like WinAmp/2.1 but not like Googlebot/2.1", #"Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6", "Accept": "*/*;q=0.5, audio/*, url/*", "Accept-Language": "en-US,en,de,es,fr,it,*;q=0.1", "Accept-Encoding": "gzip,deflate", @@ -51,12 +52,15 @@ "Cache-Control": "no-cache", } -#-- GET -def get(url, params={}, referer="", post=0, ajax=0, binary=0): +#-- Retrieve data via HTTP +# +# Well, it says "get", but it actually does POST and AJAXish GET requests too. +# +def get(url, params={}, referer="", post=0, ajax=0, binary=0, feedback=None): __print__( dbg.HTTP, "GET", url) # statusbar info progress_feedback(url, 0.1) @@ -67,14 +71,12 @@ if referer: headers["Referer"] = (referer if referer else url) # read if post: - __print__("POST") r = requests.post(url, params=params, headers=headers) else: - __print__("GET") r = requests.get(url, params=params, headers=headers) # result progress_feedback(0.9) content = (r.content if binary else r.text) @@ -85,17 +87,11 @@ return content -# simulate ajax calls -def ajax(url, params, referer="", binary=0): - get(url, params, referer, binary, ajax=1) - - - -#-- fix invalid URLs +#-- Append missing trailing slash to URLs def fix_url(url): if url is None: url = "" if len(url): # remove whitespace Index: channels/musicgoal.py ================================================================== --- channels/musicgoal.py +++ channels/musicgoal.py @@ -68,11 +68,11 @@ url = self.api_radio % cat.lower().replace(" ","").replace("&","") else: return [] # retrieve API data - data = http.ajax(url, None) + data = http.get(url, params=None, ajax=1) data = json.loads(data) # tranform datasets if grp == "podcast": return [{ Index: channels/myoggradio.py ================================================================== --- channels/myoggradio.py +++ channels/myoggradio.py @@ -155,23 +155,23 @@ } # just push data in, like the form does if form: self.login() - http.ajax(self.api + "c_neu.jsp", submit) + http.get(self.api + "c_neu.jsp", params=submit, ajax=1, post=1) # use JSON interface else: - http.ajax(self.api + "commonadd.json?" + urllib.urlencode(submit)) + http.get(self.api + "commonadd.json", params=submit, ajax=1) # authenticate against MyOggRadio def login(self): login = self.user_pw() if login: data = dict(zip(["benutzer", "passwort"], login)) - http.ajax(self.api + "c_login.jsp", data) + http.get(self.api + "c_login.jsp", params=data, ajax=1) # let's hope the JSESSIONID cookie is kept # returns login (user,pw) def user_pw(self): Index: channels/xiph.py ================================================================== --- channels/xiph.py +++ channels/xiph.py @@ -119,14 +119,15 @@ # there is actually just a single category to download, # all else are virtual if (cat == "all"): #-- get data - yp = http.get(self.base_url + self.yp, 1<<22, feedback=self.parent.status) + yp = http.get(self.base_url + self.yp) #-- extract l = [] + __print__( dbg.DATA, "xml.dom.minidom parses yp.xml" ) for entry in xml.dom.minidom.parseString(yp).getElementsByTagName("entry"): bitrate = self.bitrate(self.x(entry, "bitrate")) if conf.xiph_min_bitrate and bitrate and bitrate >= int(conf.xiph_min_bitrate): l.append({ "title": str(self.x(entry, "server_name")), Index: favicon.py ================================================================== --- favicon.py +++ favicon.py @@ -86,11 +86,11 @@ title = rx_t.search(row["title"]) if title: title = title.group(0).replace(" ", "%20") # do a google search - html = ahttp.ajax("http://www.google.de/search?hl=de&q="+title, None) + html = ahttp.get("http://www.google.de/search?hl=de&q="+title, params={}, ajax=1) # find first URL hit url = rx_u.search(html) if url: row["homepage"] = ahttp.fix_url(url.group(1))