From deba6e21d11b945a6d5362e7ce4ff89070ab98a0 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Mon, 5 Aug 2013 15:28:19 +0200 Subject: still problems --- bin/updater.py | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/bin/updater.py b/bin/updater.py index 65236ae0c..75a233b43 100644 --- a/bin/updater.py +++ b/bin/updater.py @@ -22,7 +22,7 @@ License along with this library. If not, see . """ -# This script uses python 3 + import sys import time import json @@ -31,6 +31,7 @@ import threading import urllib.request import urllib.error + class UpdateThread(threading.Thread): lock = threading.Lock() @@ -40,23 +41,29 @@ class UpdateThread(threading.Thread): self.feeds = feeds self.update_url = update_url - def run(self): - with lock: + with UpdateThread.lock: if len(self.feeds) > 0: feed = self.feeds.pop() - # call the update method of one feed - data = urllib.parse.urlencode(feed) - urllib.request.urlopen(self.update_url, data) - self.run() else: self.exit() + # call the update method of one feed + data = json.dumps(feed) + headers = { + 'Content-type': 'application/json', + 'Accept': 'text/plain' + } + request = urllib.request.Request(self.update_url, data=data, + headers=headers, method='GET') + response = urllib.request.urlopen(request) + print(response.read()) + self.run() + class Updater: def __init__(self, base_url, thread_num, interval): - self.thread_num = thread_num self.interval = interval self.base_url = base_url @@ -75,12 +82,12 @@ class Updater: # run the cleanup request and get all the feeds to update urllib.request.urlopen(self.cleanup_url) feeds_response = urllib.request.urlopen(self.all_feeds_url) - feeds_json = str( feeds_response.read() ) - feeds = json.loads(feeds_json) + feeds_json = feeds_response.read().decode('utf-8') + feeds = json.loads(feeds_json)['feeds'] # start thread_num for feeds threads = [] - for i in range(0, self.thread_num): + for num in range(0, self.thread_num): thread = UpdateThread(feeds, self.update_url) thread.start() threads.append(thread) @@ -92,10 +99,10 @@ class Updater: time.sleep(self.interval) self.run() - - # TODO: also check for the other URLErrors - except (ValueError, urllib.error.HTTPError): - print('%s is either not valid or does not exist' % self.base_url) + except ValueError: + print('%s is not a valid URL' % self.base_url) + except urllib.error.HTTPError: + print('%s does not exist' % self.base_url) exit(1) @@ -125,9 +132,13 @@ def main(): # create the updater and run the threads updater = Updater(args.url, args.threads, args.interval) + updater.run() if __name__ == '__main__': - main() + if sys.version_info < (3, 3): + print('Python 3.3 is required to run this script') + else: + main() -- cgit v1.2.3