-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.py
168 lines (151 loc) · 6.14 KB
/
upload.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sys #for command-line arguments
import urllib.request #for http
import http.cookiejar as cookielib #for cookies and session
from urllib import parse #for urlencode
import getpass #password reading from commandline
#import urllib
#------ SETTINGS -------#
LOGIN_URL = "http://igem.org/Login"
#DO NOT end base url with "/"
BASE_URL = "http://2014.igem.org/wiki/index.php?title=Team:Aalto-Helsinki"
AUTO_PAGES = ["index","Business","Research","Team","Journal","Modeling","Cooperation","Outreach"]
#-----------------------#
# Wrangler class - parser object which parses HTML
from html.parser import HTMLParser
class Wrangler(HTMLParser):
#The tags, a.k.a. id's are stored here. For example wpEditToken
ids = {}
#Method which handles every start tag eg. <input>
def handle_starttag(self, tag, attrs):
if (tag == 'input'):
for i in attrs:
if (i[0] == 'name'):
if (i[1] == 'wpAutoSummary' or i[1] == 'wpEditToken' or i[1] == 'wpSave' or i[1] == 'wpSection' or i[1] == 'wpStarttime' or i[1] == 'wpEdittime' or i[1] == 'oldid'):
name = i[1]
value = [c for c in attrs if c[0]=='value'][0][1]
self.ids[name]=value
#http://stackoverflow.com/questions/189555/how-to-use-python-to-login-to-a-webpage-and-retrieve-cookies-for-later-usage
#def main(argv=sys.argv):
def upload(page, file, headerfooter = True):
global opener
#-------- get edit id --------#
try:
if (page == "index"):
resp = opener.open(BASE_URL+"&action=edit")
else:
resp = opener.open(BASE_URL+"/"+page+"&action=edit")
content = resp.read()
parser = Wrangler()
parser.feed(content.decode('utf8'))
data = parser.ids #stores wpEditToken and wp AutoSummary
#except NameError:
except:
print("Error:", sys.exc_info()[0])
return 3
#---- read requested file ----#
try:
with open (file, "r", encoding="utf8") as myfile:
file_data=myfile.read()
except FileNotFoundError:
#print("File {:s} not found".format(file))
return 2
#---- read header & footer ---#
if (headerfooter == True):
try:
with open ("include/header.html", "r") as myfile:
header_data=myfile.read()
except FileNotFoundError:
print("no include/header.html found. Not including")
header_data = ""
try:
with open ("include/footer.html", "r") as myfile:
footer_data=myfile.read()
except FileNotFoundError:
print("no include/footer.html found. Not including")
footer_data = ""
file_data = header_data+file_data+footer_data
#------- post new edit -------#
try:
data['wpTextbox1'] = file_data
#data['wpSave'] = 'Save page'
encoded_data = parse.urlencode(data)
if (page == "index"):
resp = opener.open(BASE_URL+"&action=submit", encoded_data.encode('utf8'))
else:
resp = opener.open(BASE_URL+"/"+page+"&action=submit", encoded_data.encode('utf8'))
#print (resp.read())
#headers = {"Content-type": "multipart/form-data;",
except:
print("Error:", sys.exc_info()[0])
return 3
#print('No errors encountered. Although i cannot verify the success :)')
return 0
def login( username, password):
global opener
#---------- log in------------#
try:
login_data = {'id':'0','new_user_center':'','new_user_right':'','hidden_new_user':'','return_to':'http://2014.igem.org/wiki/index.php?title=Team:Aalto-Helsinki/testpreview&action=edit','username':username,'password':password,'Login':'Log+in','search_text':''}
cookie = cookielib.CookieJar()
opener = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(cookie))
encoded_data = parse.urlencode(login_data)
resp = opener.open(LOGIN_URL, encoded_data.encode('utf8'))
#check if login failed
response = resp.read()
if (response.find(b"That username") != -1):
return 2
except urllib.error.URLError:
print("Login server not found. Perhaps the URL is wrong in the code?")
return 1
except:
print("Error:", sys.exc_info()[0])
return 1
return 0
def main(argv=sys.argv):
#----- arguments -------------#
try:
if (argv[1] != '-auto'):
page = argv[1]
file = argv[2]
except IndexError:
print("Usage: upload.py wikipage filename\n\nwikipage\tThe subpage in the wiki.\n\t\teg. in igem.org/wiki/index.php?title=Team:teamname/members\n\t\twikipage=members\n\nfile\t\tfilename in current directory")
return
#------- read input ----------#
try:
print("-- iGEM wiki quickify --\ncmd + d to abort.")
username = input("Username: ")
username = username.encode("utf8")
password = getpass.getpass('Password: ')
#input("Password: ")
except EOFError:
print("Aborting...")
return 1
print("Logging in")
login_result = login(username, password)
if (login_result == 2):
print("Invalid username/password")
return 1
elif (login_result != 0):
print("Server error when logging in")
return 1
#------- automation ---------#
if (argv[1] == '-auto'):
print("Auto updating pages: ",",".join(AUTO_PAGES))
for p in AUTO_PAGES:
r=upload(p,p+".html", True)
if (r == 2):
print("{:s}.html\t\tFile not found".format(p))
elif (r== 1):
print("{:s}.html\t\tServer error".format(p))
elif (r== 3):
print("{:s}.html\t\tUnknown error".format(p))
else:
print("{:s}.html\t\tUploaded".format(p))
else:
print("Uploading contents of \"{:s}\" to \"{:s}\"".format(file, page))
r = upload( page, file)
if (r == 2):
print("{:s}.html\t\tFile not found".format(file))
elif (r != 0):
print("Error occured")
print("Done")
main()