forked from subhojit-mukherjee/pythoncodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackOverFlow.py
More file actions
162 lines (132 loc) · 5.69 KB
/
StackOverFlow.py
File metadata and controls
162 lines (132 loc) · 5.69 KB
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
151
152
153
154
155
156
157
158
159
160
161
import requests
import json,ast
from pprint import pprint
from sets import Set
from pytz import timezone
from datetime import datetime,timedelta
import calendar
subject,numberofscholar=raw_input().split()
numberofscholar=int(numberofscholar)
#Getting the top answers with given tag
URL_TOP_ANSWER1 ='https://api.stackexchange.com/2.2/tags/'
URl_TOP_ANSWER2='/top-answerers/all_time'
#Getting top answers of a given user with same tag to determine if he/she has given reply of the same tag ever
URL_TAG_USER_ANSWERS1='https://api.stackexchange.com/2.2/users/'
URL_TAG_USER_ANSWERS2='/top-answers'
#Getting the badge details of a user to check if he has any badge with 'Teacher'
URL_USER_BADGE1='https://api.stackexchange.com/2.2/users/'
URL_USER_BADGE2='/badges'
#Getting the reputation change details of last one month
URL_USER_REPUTATION1='https://api.stackexchange.com/2.2/users/'
URL_USER_REPUTATION2='/reputation'
#Getting the user detail
URL_USER_DETAIL1='https://api.stackexchange.com/2.2/users/'
#Getting the date in UTC Strin as supported by stackexchange api
today=str(calendar.timegm(datetime.now().timetuple()))
oneMonthBefore=str(calendar.timegm((datetime.now()- timedelta(days=30)).timetuple()))
#parameters for rest api
url_user_param={
'site' : 'stackoverflow',
'order' : 'desc',
'sort' : 'reputation'
}
url_replutation_param={
'site' : 'stackoverflow',
'fromdate' : oneMonthBefore,
'todate' : today
}
url_user_badge={
'site' : 'stackoverflow',
'order' : 'desc',
'sort' : 'rank'
}
url_params_topAnswer = {
'site' : 'stackoverflow'
}
url_params_tagUsers_answers={
'site' : 'stackoverflow',
'order' : 'desc',
'sort' :'activity'
}
url_params_tags_answers={
'site' : 'stackoverflow',
'order' : 'desc',
'sort' :'popular'
}
page = 1
not_done = True
user_list = []
# replies are paginated so loop thru until none left
while not_done and numberofscholar>0:
# url_params['page'] = page
# get next page of users
#Getting the top answers of given tag
api_response = requests.get(URL_TOP_ANSWER1+subject+URl_TOP_ANSWER2,params=url_params_topAnswer)
json_data = api_response.json()
#rawData= api_response.json() # pull the list of users out of the json answer
#data = json.load(api_response.text)
#Storing the user ID and url of their profile
ll=json_data['items']
for item_elem in ll:
user_list.append({item_elem['user']['user_id']:item_elem['user']['link']})
#user_list[item_elem['user']['user_id']]=item_elem['user']['link']
# show progress each time thru loop
for item_elem in user_list:
userkey=list(item_elem.keys())[0]
userUrl=item_elem[userkey]
#Getting the response for answers of given tag by the user in whole life
api_response_more_than_one_answer = requests.get(URL_TAG_USER_ANSWERS1+str(userkey)+'/tags/'+subject+URL_TAG_USER_ANSWERS2,params=url_params_tagUsers_answers)
#pprint(api_response_more_than_one_answer.json())
json_data_more=api_response_more_than_one_answer.json()
listTemp=json_data_more['items']
#Gdtting the badge details and storing the names in a set and checking if it contains Teacher or not
api_response_badge=requests.get(URL_USER_BADGE1+str(userkey)+URL_USER_BADGE2,params=url_user_badge)
json_data_badge=api_response_badge.json()
ll=json_data_badge['items']
badgeSet=Set()
for item_elem1 in ll:
badgeSet.add(item_elem1['name'])
#getting the reputations and same way checking for reputation change of 200 or more
api_respons_reputation=requests.get(URL_USER_REPUTATION1+str(userkey)+URL_USER_REPUTATION2,params=url_replutation_param)
json_data_reputation=api_respons_reputation.json()
reputationSet=Set()
ll=json_data_reputation['items']
for item_elem2 in ll:
if('reputation_change' in item_elem2):
reputationSet.add(item_elem2["reputation_change"])
#Checking all the conditions
if (len(listTemp)>2) and 'Teacher' in badgeSet:
fl=0
for repItem in reputationSet:
if(int(repItem)>=200):
fl=1
break
if fl==1:
api_response_user=requests.get(URL_USER_DETAIL1+str(userkey),params=url_user_param)
json_data_user=api_response_user.json()
json_data_user=json_data_user['items']
location=""
displayName=""
for t in json_data_user:
if('location' in t):
location=t['location']
if('display_name' in t):
nameUser=t['display_name']
#location=json_data_user['items'][15]
#nameUser=json_data_user['items'][19]
print "userkey:",userkey,"....name:",nameUser,"....top_answer....location:",location,'....url:',userUrl
#Continuing the process until it reaches required scholar
numberofscholar=numberofscholar-1
#print userkey,":",":",badgeSet,":",reputationSet
#print user_list
#note only so man queries allowed per day
# prepare for next iteration if needed
page += 1
not_done = json_data['has_more']
'''
Input:
cryptography 2
Output:
userkey: 3474 ....name: erickson ....top_answer....location: United States ....url: http://stackoverflow.com/users/3474/erickson
userkey: 148870 ....name: Amber ....top_answer....location: California ....url: http://stackoverflow.com/users/148870/amber
'''