From 65407b42dcf82f9a14c0cce5fa9e30d79198c3a3 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Thu, 18 Mar 2010 22:11:48 +0000 Subject: [PATCH] Added further work on reddit API and cronjobs --- reddit/api.py | 2 +- reddit/cron.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/reddit/api.py b/reddit/api.py index 5eb9146..d35037b 100644 --- a/reddit/api.py +++ b/reddit/api.py @@ -54,7 +54,7 @@ class Inbox(): def _inbox_data(self): if not hasattr(self, '__inbox_cache'): - inbox = json.load(self.opener.open(self.REDDIT_API_INBOX))['data'] + inbox = json.load(self._opener.open(self.REDDIT_API_INBOX))['data'] self.__inbox_cache = [] for msg in inbox['children']: diff --git a/reddit/cron.py b/reddit/cron.py index f86a213..f078117 100644 --- a/reddit/cron.py +++ b/reddit/cron.py @@ -3,6 +3,7 @@ import logging from django_cron import cronScheduler, Job from reddit.models import RedditAccount +from reddit.api import Inbox class UpdateAPIs(Job): """ @@ -24,4 +25,40 @@ class UpdateAPIs(Job): acc.save() time.sleep(2) + +class APIKeyParser: + dictitems = {} + + def __init__(self, key): + rows = key.split('\n') + for row in rows: + key = row.split(":")[0].replace(" ", "_").lower().strip() + value = row.split(":")[1].strip() + + self.dictitems[key] = value + + def __getattr__(self, key): + return self.dictitems[key] + + def __str__(self): + return "%s:%s" % (self.user_id, self.api_key) + +class ProcessInbox(Job): + """ + Grabs all Reddit Mail and processes any new applications + """ + + def job(self): + inbox = Inbox(settings.REDDIT_USER, settings.REDDIT_PASSWORD) + + for msg in inbox: + if not msg.was_comment and msg.new: + try: + key = APIKeyParser(msg.body) + except: + pass + else: + print key.username + + cronScheduler.register(UpdateAPIs)