-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackedin.py
119 lines (106 loc) · 3.06 KB
/
stackedin.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
import urllib
import urllib2
import json
import httplib
import base64
from urlparse import urlparse
username = None
repo = None
def Main():
create_url = create_repo()
encoding = gen_html()
if create_file(create_url, encoding):
print 'Your Stackoverflow flair for Linkedin is: ' + '\033[1m' + 'http://' + username + '.github.io' + '\033[0m'
else:
print 'Failed creating, deleting repository.....'
if delete_repo():
print 'Repository deleted'
else:
print 'Failed deleting ' + repo + ', please delete it manually'
def create_repo():
res_code = 400
create_url = None
while res_code != 201:
global username
username = raw_input('Github username: ')
token = raw_input('Access Token: ')
url = 'https://api.github.com/user/repos?access_token=' + token
global repo
repo = username + ".github.io"
values = {"name":repo}
data = json.dumps(values)
req = urllib2.Request(url,data)
response = None
print 'Creating repository.....'
try: response = urllib2.urlopen(req)
except urllib2.URLError as e:
if e.reason == 'Unauthorized':
print 'Invalid token, try again'
if e.reason == 'Unprocessable Entity':
print 'Invalid Github username, try again'
continue
res_code = response.getcode()
if res_code == 201:
print 'Repository ' + repo + ' created'
create_url = 'https://api.github.com/repos/' + username + '/' + repo + '/contents/index.html?access_token=' + token
return create_url
def gen_html():
valid_url = False
flair = None
profile = None
while not valid_url:
stack_id = raw_input('Stackoverflow id: ')
flair = 'http://stackoverflow.com/users/flair/' + stack_id + '.png'
profile = 'http://stackoverflow.com/users/' + stack_id
print 'Validating Stackoverflow ID.....'
if checkUrl(flair) and checkUrl(profile):
valid_url = True
html = ('<!DOCTYPE html>\n'
'<html>\n'
'<head>\n'
'<meta content="0; ' + profile + '" http-equiv="refresh" />\n'
'</head>\n'
'<body>\n'
'<p>\n'
'<a href="' + profile + '">\n'
'<img alt="Nothing" border="0" src="' + flair + '" >\n'
'</a>\n'
'</p>\n'
'</body>\n'
'</html>\n')
return base64.b64encode(html)
def checkUrl(url):
p = urlparse(url)
conn = httplib.HTTPConnection(p.netloc)
conn.request('HEAD', p.path)
resp = conn.getresponse()
return resp.status < 400
def create_file(create_url, encoding):
values = {"message":"Stackoverflow & Linkedin","content":encoding}
data = json.dumps(values)
req = urllib2.Request(create_url,data)
req.get_method = lambda: 'PUT'
response = None
print 'Committing HTML file.....'
try: response = urllib2.urlopen(req)
except urllib2.URLError as e:
print e
return False
res_code = response.getcode()
if res_code == 201:
return True
return False
def delete_repo():
delete_url = 'https://api.github.com/repos/' + username + '/' + repo
req = urllib2.Request(delete_url)
req.get_method = lambda: 'DELETE'
response = None
try: response = urllib2.urlopen(req)
except urllib2.URLError as e:
return False
res_code = response.getcode()
if res_code == 204:
return True
return False
if __name__ == "__main__":
Main()