forked from kossiitkgp/kwoc-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_images.py
150 lines (140 loc) · 4.82 KB
/
update_images.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
import sys
from imp import reload
reload(sys)
sys.setdefaultencoding('utf8')
#above three lines are IMPORTANT
import psycopg2
import requests
import os
import traceback
try:
import urlparse
except Exception as e:
from urllib import parse as urlparse
if "LOCAL_CHECK" in os.environ:
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
def updateProjectImage():
global conn, cursor
if "LOCAL_CHECK" not in os.environ:
msg = "Database Connection cannot be set since you are running website locally"
print (msg)
query="SELECT * FROM project"
try:
cursor.execute(query)
# projectsData=list()
for index,row in enumerate(cursor.fetchall()) :
if not row[7] : #checking if a valid image url is already present
handle=row[0][:]
imgURL=getimageURL(handle.split("/")[0])
if imgURL :
updateQuery = "UPDATE project SET image='%s' WHERE project_handle='%s'" % (imgURL,row[0])
else :
updateQuery = "UPDATE project SET image='%s' WHERE project_handle='%s'" % ("http://i.imgur.com/nx6cwcv.png",row[0])
try : #updating image URL in the database
cursor.execute(updateQuery)
conn.commit()
except :
conn.rollback()
error_msg = "Unable to update image url {} for {}.\nFollowing error occured{}".format(imgURL,row[0],
traceback.format_exc())
print (error_msg)
except:
conn.rollback()
error_msg = "Unable to get all projects\n\n {}".format(
traceback.format_exc())
print (error_msg)
def getimageURL(githubUsername) : # getting the image url from github
baseQuery="https://api.github.com/search/users?access_token={}&q=".format(os.environ["DEFCON_GITHUB_AUTH_TOKEN"])
try :
query = baseQuery+githubUsername
response=requests.get(baseQuery+githubUsername).json()
if response["total_count"] == 1 : # checking if a unique user is found
return response["items"][0]["avatar_url"]
# return unicode(response["items"][0]["avatar_url"] , "utf-8")
else :
print ("Got more than one result for {}".format(githubUsername))
return False
except :
print ("Unable to retrive image url for {}\nGot following error :{}".format(githubUsername,traceback.format_exc()))
# slack_notification("Unable to retrive image url for {}\nGot following error :{}".format(githubUsername,traceback.format_exc()))
return False
def updateForkNo():
global conn, cursor
if "LOCAL_CHECK" not in os.environ:
msg = "Database Connection cannot be set since you are running website locally"
print (msg)
query="SELECT * FROM project"
try:
cursor.execute(query)
for index,row in enumerate(cursor.fetchall()) :
if row[1] == "df" and row[2] == "df" :
continue
forkno = getforks(row[0])
updateQuery = "UPDATE project SET forkno='%s' WHERE project_handle='%s'" % (str(forkno),row[0])
try : #updating image URL in the database
cursor.execute(updateQuery)
conn.commit()
except :
conn.rollback()
error_msg = "Unable to update fork nofor {}.\nFollowing error occured{}".format(row[0],
traceback.format_exc())
print (error_msg)
except:
conn.rollback()
error_msg = "Unable to get all projects\n\n {}".format(
traceback.format_exc())
print (error_msg)
def getforks(projectHandle):
baseQuery="https://api.github.com/repos/{}?access_token={}".format(projectHandle,os.environ["DEFCON_GITHUB_AUTH_TOKEN"])
try :
response = requests.get(baseQuery).json()
forkNo = response["forks"]
return forkNo
except :
return "None"
def updatewatcherNo():
global conn, cursor
if "LOCAL_CHECK" not in os.environ:
msg = "Database Connection cannot be set since you are running website locally"
print (msg)
query="SELECT * FROM project"
try:
cursor.execute(query)
for index,row in enumerate(cursor.fetchall()) :
if row[1] == "df" and row[2] == "df" :
continue
watcherno = getwatchers(row[0])
updateQuery = "UPDATE project SET watcherno='%s' WHERE project_handle='%s'" % (str(watcherno),row[0])
try : #updating image URL in the database
cursor.execute(updateQuery)
conn.commit()
except :
conn.rollback()
error_msg = "Unable to update watcher no for {}.\nFollowing error occured{}".format(row[0],
traceback.format_exc())
print (error_msg)
except:
conn.rollback()
error_msg = "Unable to get all projects\n\n {}".format(
traceback.format_exc())
print (error_msg)
def getwatchers(projectHandle):
baseQuery="https://api.github.com/repos/{}?access_token={}".format(projectHandle,os.environ["DEFCON_GITHUB_AUTH_TOKEN"])
try :
response = requests.get(baseQuery).json()
watcherNo = response["watchers"]
return watcherNo
except :
return "-"
if __name__ == "__main__" :
updatewatcherNo()