Browse Source

Add support for retrieving steam accounts

Closes #2
master
Chris Smith 14 years ago
parent
commit
b84e5daa25
1 changed files with 35 additions and 4 deletions
  1. 35
    4
      src/helloworld.py

+ 35
- 4
src/helloworld.py View File

@@ -1,7 +1,6 @@
1 1
 import os
2 2
 from Scraper import Scraper
3 3
 from google.appengine.ext.webapp import template
4
-
5 4
 from google.appengine.ext import webapp
6 5
 from google.appengine.api import users
7 6
 from google.appengine.ext.webapp.util import run_wsgi_app
@@ -83,12 +82,18 @@ class UpdatePage(webapp.RequestHandler):
83 82
 
84 83
     def post(self):
85 84
         account = db.get(db.Key(self.request.get('key')))
86
-        res = []
87 85
 
88 86
         if account.source.name == 'Spore':
89
-            res = Scraper.scrape_spore(account.credentials)
87
+            UpdatePage.merge_achievements(account, Scraper.scrape_spore(account.credentials))
88
+        elif account.source.name == 'Steam':
89
+            UpdatePage.merge_sources(account, Scraper.scrape_steam(account.credentials))
90
+
91
+        self.redirect('/')
90 92
 
91
-        UpdatePage.merge_achievements(account, res)
93
+    @staticmethod
94
+    def merge_sources(account, sources):
95
+        for user_source in sources:
96
+            UpdatePage.get_or_create_source(user_source, account)
92 97
 
93 98
     @staticmethod
94 99
     def merge_achievements(account, achievements):
@@ -121,6 +126,32 @@ class UpdatePage(webapp.RequestHandler):
121 126
 
122 127
         return res
123 128
 
129
+    @staticmethod
130
+    def get_or_create_source(source_info, account):
131
+        source = AchievementSource.gql("WHERE name = :name", name=source_info['name'])
132
+
133
+        if source.count(1) == 0:
134
+            source = AchievementSource(name=source_info['name'],
135
+                                    url=source_info['url'])
136
+            source.put()
137
+        else:
138
+            source = source.get()
139
+
140
+        res = UserAccount.gql("WHERE source = :source AND user = :user AND "
141
+                            + "credentials = :creds", source = source,
142
+                                                      user = account.user,
143
+                                                      creds = account.credentials)
144
+
145
+        if res.count(1) == 0:
146
+            res = UserAccount(user = account.user,
147
+                              source = source,
148
+                              credentials = account.credentials)
149
+            res.put()
150
+        else:
151
+            res = res.get()
152
+
153
+        return res
154
+
124 155
 application = webapp.WSGIApplication([('/', MainPage),
125 156
                                       ('/admin/addsource', AddSourcePage),
126 157
                                       ('/worker/update', UpdatePage),

Loading…
Cancel
Save