Переглянути джерело

Add support for retrieving steam accounts

Closes #2
master
Chris Smith 14 роки тому
джерело
коміт
b84e5daa25
1 змінених файлів з 35 додано та 4 видалено
  1. 35
    4
      src/helloworld.py

+ 35
- 4
src/helloworld.py Переглянути файл

1
 import os
1
 import os
2
 from Scraper import Scraper
2
 from Scraper import Scraper
3
 from google.appengine.ext.webapp import template
3
 from google.appengine.ext.webapp import template
4
-
5
 from google.appengine.ext import webapp
4
 from google.appengine.ext import webapp
6
 from google.appengine.api import users
5
 from google.appengine.api import users
7
 from google.appengine.ext.webapp.util import run_wsgi_app
6
 from google.appengine.ext.webapp.util import run_wsgi_app
83
 
82
 
84
     def post(self):
83
     def post(self):
85
         account = db.get(db.Key(self.request.get('key')))
84
         account = db.get(db.Key(self.request.get('key')))
86
-        res = []
87
 
85
 
88
         if account.source.name == 'Spore':
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
     @staticmethod
98
     @staticmethod
94
     def merge_achievements(account, achievements):
99
     def merge_achievements(account, achievements):
121
 
126
 
122
         return res
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
 application = webapp.WSGIApplication([('/', MainPage),
155
 application = webapp.WSGIApplication([('/', MainPage),
125
                                       ('/admin/addsource', AddSourcePage),
156
                                       ('/admin/addsource', AddSourcePage),
126
                                       ('/worker/update', UpdatePage),
157
                                       ('/worker/update', UpdatePage),

Завантаження…
Відмінити
Зберегти