-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_wiki.py
129 lines (99 loc) · 3.61 KB
/
oauth_wiki.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
from flask import current_app, session
from requests_oauthlib import OAuth1Session
from urllib.parse import urlencode
from flask_babel import gettext
project = "https://commons.wikimedia.org/w/api.php?"
def raw_request(params):
app = current_app
url = project + urlencode(params)
client_key = app.config['CONSUMER_KEY']
client_secret = app.config['CONSUMER_SECRET']
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=session['owner_key'],
resource_owner_secret=session['owner_secret'])
return oauth.get(url, timeout=4)
def raw_post_request(files, params):
app = current_app
url = project
client_key = app.config['CONSUMER_KEY']
client_secret = app.config['CONSUMER_SECRET']
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=session['owner_key'],
resource_owner_secret=session['owner_secret'])
return oauth.post(url, files=files, data=params, timeout=4)
def api_request(params):
return raw_request(params).json()
def userinfo_call():
params = {'action': 'query', 'meta': 'userinfo', 'format': 'json'}
return api_request(params)
def get_username():
if 'owner_key' not in session:
return # not authorized
if 'username' in session:
return session['username']
reply = userinfo_call()
if 'query' not in reply:
return
session['username'] = reply['query']['userinfo']['name']
return session['username']
def get_token():
params = {
'action': 'query',
'meta': 'tokens',
'format': 'json',
'formatversion': 2,
}
reply = api_request(params)
token = reply['query']['tokens']['csrftoken']
return token
def upload_file(file, filename, form, username):
text = build_text(form, username)
token = get_token()
params = {
"action": "upload",
"filename": form["title"]+get_file_ext(filename),
"format": "json",
"token": token,
"text": text,
"comment": "Uploaded with wikiusos"
}
media_file = {'file': (filename, file.read(), 'multipart/form-data')}
req = raw_post_request(media_file, params)
data = req.json()
return data
def build_text(form, username):
descr = gettext(u"Imagem contribuída através do aplicativo ''Wiki Museu do Ipiranga - Para que serve?''")
if "para_que_serve" in form and form["para_que_serve"]:
descr = descr + " Para que serve: \"" + form["para_que_serve"] + "\""
text = ("=={{int:filedesc}}==\n"
"{{Information\n"
"|description={{"+form["lang"]+"|1="+descr+"}}\n"
"|date="+form["date"]+"\n"
"|source={{own}}\n"
"|author=[[User:"+username+"|"+username+"]]\n"
"|other fields = {{Wikiusos/Information field|qid = "+form["qid"]+"}}\n"
"}}\n\n"
"=={{int:license-header}}==\n"
"{{Wikiusos}}\n"
"{{"+get_license(form["license"])+"}}\n\n"
"[[Category:Uploaded with wikiusos|"+form["qid"]+"]]"
)
return text
def get_license(license_):
if license_ == "ccbysa3":
return "Cc-by-sa-3.0"
elif license_ == "ccby4":
return "Cc-by-4.0"
elif license_ == "ccby3":
return "Cc-by-3.0"
elif license_ == "cc0":
return "Cc-zero"
else:
return "Cc-by-sa-4.0"
def get_file_ext(filename):
file_ext = filename.split(".")[-1]
if file_ext != filename:
return "." + file_ext
return ""