Added further work on reddit API and cronjobs

This commit is contained in:
2010-03-18 22:11:48 +00:00
parent b49f8edb9a
commit 65407b42dc
2 changed files with 38 additions and 1 deletions

View File

@@ -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']:

View File

@@ -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)