forked from youtify/youtify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.py
78 lines (66 loc) · 2.31 KB
/
migrations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import urlfetch
from model import FollowRelation
from model import YoutifyUser
from model import Activity
from model import Playlist
from model import ExternalUser
from string import Template
from django.utils import simplejson
TEMPLATE = """
<html>
<body>
</body>
Progress: $progress
<script type="text/javascript">
setTimeout(function() { location.href = '?page=$next'; }, 100);
</script>
</html>
"""
COMPLETE = """
<html>
<body>
<h1 style="color:green">DONE, $count iterations</h1>
</body>
</html>
"""
flattr_thing_cache = {}
def import_followings(external_user_id):
url = "http://api.soundcloud.com/users/%s/followings.json?client_id=%s" % (external_user_id, '206f38d9623048d6de0ef3a89fea1c4d')
result = urlfetch.fetch(url)
result = simplejson.loads(result.content)
for user in result:
if ExternalUser.all().filter('type =', 'soundcloud').filter('external_user_id =', str(user['id'])).get() is None:
external_user_model = ExternalUser(type='soundcloud', external_user_id=str(user['id']))
external_user_model.username = user['permalink']
external_user_model.avatar_url = user['avatar_url']
external_user_model.save()
class MigrationStepHandler(webapp.RequestHandler):
def get(self):
global flattr_thing_cache
page = int(self.request.get('page', '0'))
page_size = 30
count = 0
#### START MIGRATION CODE ####
for m in ExternalUser.all().filter('type =', 'soundcloud').fetch(page_size, page_size * page):
count += 1
import_followings(m.external_user_id)
#### END MIGRATION CODE ####
self.response.headers['Content-Type'] = 'text/html'
if (count < page_size):
self.response.out.write(Template(COMPLETE).substitute({
'count': count,
}))
else:
self.response.out.write(Template(TEMPLATE).substitute({
'progress': page_size * page,
'next': page + 1,
}))
def main():
application = webapp.WSGIApplication([
('/admin/migrations/import_followings', MigrationStepHandler),
], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()