1
1
import logging
2
+ #from cqlengine import BatchQuery
3
+ from cqlengine .query import DoesNotExist
4
+ from pyhackers .config import config
2
5
from pyhackers .model .user import User , SocialUser
3
- from pyhackers .model .cassandra .hierachy import (
4
- User as CsUser , Post as CsPost , UserPost as CsUserPost , UserFollower as CsUserFollower ,
5
- UserTimeLine )
6
+ from pyhackers .model .cassandra .hierachy import (GithubProject , GithubUserList , GithubUser ,
7
+ User as CsUser , Post as CsPost , UserPost as CsUserPost , UserFollower as CsUserFollower ,
8
+ UserTimeLine )
9
+ from github import Github
6
10
7
11
8
12
class RegistrationGithubWorker ():
@@ -11,22 +15,148 @@ class RegistrationGithubWorker():
11
15
following users/followers
12
16
"""
13
17
14
- def __init__ (self , user_id , social_account_id ):
18
+ def __init__ (self , user_id , social_account_id , config ):
15
19
self .user_id = user_id
16
20
self .social_account_id = social_account_id
21
+ self .client_id = config .get ("github" , 'client_id' )
22
+ self .client_secret = config .get ("github" , 'client_secret' )
17
23
self .access_token = None
24
+ self .g = None
25
+ self .github_user = None
26
+ self .github_user_detail = None
27
+ self .users_discovered = set ()
18
28
19
29
def get_user_details_from_db (self ):
20
30
user = User .query .get (self .user_id )
21
31
social_account = SocialUser .query .get (self .social_account_id )
22
32
self .access_token = social_account .access_token
23
33
34
+
35
+ def init_github (self ):
36
+ self .g = Github (self .access_token ,
37
+ client_id = self .client_id ,
38
+ client_secret = self .client_secret ,
39
+ per_page = 100 )
40
+
41
+ self .github_user = self .g .get_user ()
42
+ self .github_user_detail = GithubUserList .create (user = self .github_user .login )
43
+
44
+ def get_starred_projects (self ):
45
+ starred = self .github_user .get_starred ()
46
+ projects = []
47
+ #with BatchQuery() as b:
48
+ for s in starred :
49
+ projects .append (s .full_name )
50
+ self .users_discovered .add (s .owner .login )
51
+
52
+ GithubProject .create (
53
+ id = s .id ,
54
+ name = s .name ,
55
+ full_name = s .full_name ,
56
+ watchers_count = s .watchers ,
57
+ description = s .description ,
58
+ homepage = s .homepage ,
59
+ fork = s .fork ,
60
+ forks_count = s .forks ,
61
+ language = s .language ,
62
+ master_branch = s .master_branch ,
63
+ network_count = 0 ,
64
+ open_issues = s .open_issues ,
65
+ url = s .url ,
66
+ is_py = s .language in ['python' , 'Python' ],
67
+ owner = s .owner .id ,
68
+ hide = False ,
69
+ )
70
+
71
+ #print s.full_name, s.watchers
72
+
73
+
74
+ self .github_user_detail .starred = projects
75
+ self .github_user_detail .save ()
76
+
77
+ def get_following_users (self ):
78
+ following = self .github_user .get_following ()
79
+ following_users = []
80
+
81
+ for f in following :
82
+ self .users_discovered .add (f .login )
83
+ following_users .append (f .login )
84
+ print f
85
+
86
+ self .github_user_detail .following = following_users
87
+ self .github_user_detail .save ()
88
+
89
+ def get_follower_users (self ):
90
+ followers = self .github_user .get_followers ()
91
+ follower_users = []
92
+
93
+ for f in followers :
94
+ self .users_discovered .add (f .login )
95
+ follower_users .append (f .login )
96
+
97
+ self .github_user_detail .followers = follower_users
98
+ self .github_user_detail .save ()
99
+
100
+ def save_discovered_users (self ):
101
+
102
+ found_ids = GithubUser .objects .filter (nick__in = list (self .users_discovered ))
103
+ found_id_list = []
104
+
105
+ for user in found_ids :
106
+ found_id_list .append (user .nick )
107
+
108
+ missing_ids = list (set (self .users_discovered ) - set (found_id_list ))
109
+
110
+ logging .warn (found_id_list )
111
+ logging .warn (self .users_discovered )
112
+
113
+ logging .warn ("[{}] users are found" .format (len (self .users_discovered )))
114
+ logging .warn ("[{}] users are missing" .format (len (missing_ids )))
115
+
116
+ #return
117
+
118
+ for nick in missing_ids :
119
+
120
+ user = self .g .get_user (nick )
121
+
122
+ logging .warn ("Creating user [{}]" .format (nick ))
123
+
124
+
125
+ GithubUser (nick = user .login ,
126
+ id = user .id ,
127
+ email = user .email ,
128
+ followers = user .followers ,
129
+ following = user .following ,
130
+ image = user .avatar_url ,
131
+ blog = user .blog ,
132
+ bio = user .bio ,
133
+ company = user .company ,
134
+ location = user .location ,
135
+ name = user .name ,
136
+ url = user .url ,
137
+ utype = user .type ,
138
+ public_repos = user .public_repos ,
139
+ public_gists = user .public_gists ,).save ()
140
+ #ghuser.save()
141
+ logging .warn ("User[{}]created" .format (nick ))
142
+
143
+
24
144
def run (self ):
25
145
self .get_user_details_from_db ()
146
+ self .init_github ()
147
+ self .get_starred_projects ()
148
+ self .get_following_users ()
149
+ self .get_follower_users ()
150
+ self .save_discovered_users ()
26
151
pass
27
152
28
153
29
154
def new_github_registration (user_id , social_account_id ):
30
155
logging .warn ("[TASK][new_github_registration]: [UserId:{}] [SAcc:{}]" .format (user_id , social_account_id ))
31
156
32
- RegistrationGithubWorker (user_id ,social_account_id ).run ()
157
+ RegistrationGithubWorker (user_id , social_account_id , config ).run ()
158
+
159
+
160
+ if __name__ == "__main__" :
161
+ #new_github_registration(12,5)
162
+ new_github_registration (14 , 13 )
0 commit comments