-
Notifications
You must be signed in to change notification settings - Fork 8
/
commons_captions_copy.py
156 lines (133 loc) · 4.91 KB
/
commons_captions_copy.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copy descriptions to captions
# Mike Peel 27-Jan-2019 v1
from __future__ import unicode_literals
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
import urllib
import pprint
import csv
from database_login import *
database = False
manual = True
maxnum = 1000000
usetemplate = 0
usecategory = 1
wikidata_site = pywikibot.Site("wikidata", "wikidata")
repo = wikidata_site.data_repository() # this is a DataSite object
commons = pywikibot.Site('commons', 'commons')
def get_login_token(site):
params = { 'action' :'query',
'meta' : 'tokens',
'type' : 'login',
'format' : 'json'}
request = pywikibot.data.api.Request(site=site, parameters=params)
return request.submit()
def do_login(site,token):
loginmanager = pywikibot.data.api.LoginManager(password=commons_testbot_pass, sysop=False, site=site, user=commons_testbot_username)
return loginmanager.login()
# params = { 'action' :'login',
# 'lgname' : commons_testbot_username,
# 'lgpassword' : commons_testbot_pass,
# 'format' : 'json',
# 'lgtoken':token}
# request = pywikibot.data.api.Request(site=site, parameters=params)
# return request.submit()
def get_token(site):
params = { 'action' :'query',
'meta' : 'tokens',
'format' : 'json'}
request = pywikibot.data.api.Request(site=site, parameters=params)
return request.submit()
def get_userinfo(site):
params = { 'action' :'query',
'meta' : 'userinfo',
'format' : 'json'}
request = pywikibot.data.api.Request(site=site, parameters=params)
return request.submit()
def get_mid(site, itemtitle):
params = { 'action' :'query',
'prop' : 'info',
'format' : 'json',
'titles' : itemtitle}
request = pywikibot.data.api.Request(site=site, parameters=params)
return request.submit()
def get_caption(site, itemtitle):
params = { 'action' :'wbgetentities',
'format' : 'json',
'ids': itemtitle}
request = pywikibot.data.api.Request(site=site, parameters=params)
return request.submit()
def set_caption(site, itemtitle, caption,language='en'):
# loginmanager = pywikibot.data.api.LoginManager(password=commons_testbot_pass, sysop=False, site=site, user=commons_testbot_username)
# print loginmanager.login()
# print loginmanager.get_login_token()
# print loginmanager.getCookie()
# test = pywikibot.login.LoginManager(password=commons_testbot_pass,site=site, user=commons_testbot_username)
# print test.botAllowed()
# test.login()
# prettyPrint(test)
# caption = 'Sala Sao Paulo, Brazil'
print 'test'
site.login()
print site.logged_in()
print site.getuserinfo()
# site.TokenWallet.load_tokens()
# caption = caption.replace(' ','%20')
print caption
params = { 'action' :'wbsetlabel',
'format' : 'json',
# 'lgname' : commons_testbot_username,
'token': site.tokens['edit'],#loginmanager.get_login_token(),
'id': itemtitle,
'language' : language,
'value': caption}
print params
# print pywikibot.data.api.encode_url(params)
request = pywikibot.data.api.Request(site=site, parameters=params)
return request.submit()
def prettyPrint(variable):
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(variable)
def check_and_set_caption(target):
# Test using the Wikidata sandbox
# captions = get_caption(repo, 'Q4115189')
# prettyPrint(captions)
# test = set_caption(repo, 'Q4115189', 'This is a test',language='en')
# exit()
# token = get_login_token(commons)
# prettyPrint(token)
# print token['query']['tokens']['logintoken']
# login = do_login(commons, token['query']['tokens']['logintoken'])
# prettyPrint(login)
# tokens = get_token(commons)
# prettyPrint(tokens)
# edittoken = tokens['query']['tokens']['csrftoken']
# print edittoken
# userinfo = get_userinfo(commons)
# prettyPrint(userinfo)
# return 1
print target.title()
filetext = target.get()
if '{{en|1=' in filetext:
caption = filetext.split('{{en|1=')[1].split('}}')[0]
print caption
# Get the media ID
mid_temp = get_mid(commons, target.title())
prettyPrint(mid_temp)
for val in mid_temp['query']['pages']:
# print val
mid = val
print mid
captions = get_caption(commons, 'M'+str(mid))
prettyPrint(captions)
test = set_caption(commons, 'M'+str(mid), caption,language='en')
return 0
# That's the end of the function definitions, now run them.
file = pywikibot.Page(commons, 'File:Sala São Paulo 2018 07.jpg')
check_and_set_caption(file)
# EOF