-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.ce1002
executable file
·138 lines (117 loc) · 3.83 KB
/
upload.ce1002
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
#!/usr/bin/python2
#coding=utf-8
import sys
import urllib2
import urllib
import cookielib
import re
from os.path import expanduser
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
#設定值
homedir = expanduser('~')
user = passwd = filedir = None
config_file = open(homedir+'/.au_ce1002/config', 'r').readlines()
for line in config_file:
match = re.search(ur'^(user|password|filedir)=(.*)', line)
if match is not None:
if match.group(1) == 'user':
user = match.group(2)
elif match.group(1) == 'password':
passwd = match.group(2)
if user is None or passwd is None:
if user is None:
print 'user is None'
if passwd is None:
print 'password is None'
exit()
filename = homedir+"/s"+user+".zip"
#處理參數
want=str(sys.argv[1])
alpha = want[0]
number = want[1:]
#需要用到的4個網址
auth_url = 'http://lms.minelab.tw'
list_url = 'http://lms.minelab.tw/chooseCourse/'
view_url = 'http://lms.minelab.tw/viewCourse/'
upload_url='http://lms.minelab.tw'
#初始化opener
opener = register_openers()
cookieJar=cookielib.CookieJar()
opener.add_handler(urllib2.HTTPCookieProcessor(cookieJar))
#登入
data = {}
data['account'] = user
data['password']= passwd
post_data=urllib.urlencode(data)
req = urllib2.Request(auth_url, post_data)
result = opener.open(req)
#取得課程資訊
req = urllib2.Request(list_url)
result = opener.open(req)
data={}
data['course[name]']="CE1002-B"
#抓取form
match = re.search(ur'id="new_course"(?:.|\n)*form>', str(result.read()))
#抓取input
match = re.findall(ur'input[^>]*" />', str(match.group()))
#抓取3個必須的name value
for i in match:
want = re.search(ur'name="([^"]*)".*value="([^"]*)"',str(i))
name = want.group(1)
value = want.group(2)
data[name]=value
post_data=urllib.urlencode(data)
req = urllib2.Request(view_url, post_data)
result = opener.open(req)
#取得作業的連結
match = re.search(ur'<a href="(/tasks/\d*)">' + alpha + '[^<>]*#' + number + '</a>', str(result.read()))
upload_url += match.group(1)
req = urllib2.Request(upload_url)
result = str(opener.open(req).read())
#自動評分
if(alpha == 'E'):
token = re.search(ur'<meta content="([^">]*)" name="csrf-token"', result).group(1)
match = re.findall(ur'<a href="([^"]*stars=' + stars + ')"[^>]*>' + stars + '</a>', result)
for i in match:
i = auth_url + i
req = urllib2.Request(i, "")
req.add_header('X-CSRF-Token', token)
req.add_header('Accept', 'text/javascript')
opener.open(req)
#取得上傳資訊
#抓取form
data={}
match = re.search(ur"action=\"/tasks/\w*/uploadFile\"(?:.|\n)*?form>", result)
if match is not None:
#抓取input
match = re.findall(ur"input(?:.|\n)*?(?:/>|/input>)", str(match.group()))
#抓取5個必須的name value
for i in match:
want = re.search(ur"name=\"(.*?)\".*?value=\"(.*?)\"",str(i))
if want is not None:
name = want.group(1)
value = want.group(2)
data[name]=value
else:
result = opener.open(upload_url+"/editFile")
#抓取form
match = re.search(ur"action=\"/tasks/\w*/uploadFile\"(?:.|\n)*?form>", str(result.read()))
if match is not None:
#抓取input
match = re.findall(ur"input(?:.|\n)*?(?:/>|/input>)", str(match.group()))
#抓取5個必須的name value
for i in match:
want = re.search(ur"name=\"(.*?)\".*?value=\"(.*?)\"",str(i))
if want is not None:
name = want.group(1)
value = want.group(2)
data[name]=value
else:
print "上傳網址不存在"
exit()
upload_url += "/uploadFile"
data['file'] = open(filename, "rb")
datagen, headers = multipart_encode(data)
request = urllib2.Request(upload_url, datagen, headers)
result = opener.open(request)