summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-08-05 15:28:19 +0200
committerBernhard Posselt <nukeawhale@gmail.com>2013-08-05 15:28:19 +0200
commitdeba6e21d11b945a6d5362e7ce4ff89070ab98a0 (patch)
tree37cb672d2b724c8c9c16b46bcad7f6e33cfd81e5
parent9e922dddf3cd161bd1357c21f6834fd4b7323eef (diff)
still problems
-rw-r--r--bin/updater.py43
1 files 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 <http://www.gnu.org/licenses/>.
"""
-# 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()