forked from tshrinivasan/voice-recorder-for-tawictionary
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmediawiki-uploader.py
100 lines (64 loc) · 2.68 KB
/
mediawiki-uploader.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
import wikitools
import poster
import pyexiv2
import os
import shutil
#wiki_url = "MediaWiki API url here"
wiki_url = 'http://localhost/mediawiki/api.php'
#wiki_url = 'http://commons.wikimedia.org/w/api.php'
wiki_username = "shrini"
wiki_password = "shrini123"
category = "English pronunciation"
try:
wiki = wikitools.wiki.Wiki(wiki_url)
except:
print "Can not connect with wiki. Check the URL"
try:
wiki.login(username=wiki_username,password=wiki_password)
except:
print "Invalid Username or Password"
path = './'
listing = os.listdir(path)
def filetype(file):
return file.split(".")[-1]
def filename(file):
return file.split(".")[-2]
def get_file_details(image):
try:
metadata = pyexiv2.ImageMetadata(image)
metadata.read()
file_name=metadata['Iptc.Application2.Headline'].raw_value[0].strip()
caption=metadata['Iptc.Application2.Caption'].raw_value[0].strip()
file_meta = {'name':file_name,'caption':caption}
return file_meta
except:
print "No tag is set for the image " + image
exit
def move_file(file):
source = file
destination = "./uploaded/"+file
if os.path.isdir("uploaded"):
shutil.move(source,destination)
else:
os.mkdir("uploaded")
shutil.move(source,destination)
print "Moving the file " + file + " to the folder 'uploaded' "
def upload_file(file):
file_name = filename(file)
caption = "Audio pronunciation of the term '" + file_name + " 'in Indian English. "
file_object=open(file,"r")
picture=wikitools.wikifile.File(wiki=wiki, title=file_name)
picture.upload(fileobj=file_object,comment=caption, ignorewarnings=True)
print "Uploaded the File " + file_name
page_name = file_name.replace(" ","_")
page_url = "File:" + page_name + filetype(file)
page = wikitools.Page(wiki, page_url, followRedir=True)
# wikidata = "=={{int:filedesc}}=={{Information|description={{en|1= " + caption + "}}{{TamilWiki Media Contest}}|source={{own}}|author=[[User:" + wiki_username + "|" + wiki_username + "]]}}=={{int:license-header}}=={{self|cc-by-sa-3.0}}[[Category:" + category + "]] [[Category:Uploaded with MediawikiUploader]]"
wikidata = " {{Information |Description= " + caption + "|Source= Self |Author= [[User: " + wiki_username + "|" + wiki_username + " ]] |Permission=Dual licensed per GFDL Version 1.2 or later / Creative Commons Attribution ShareAlike license versions 2.5, 2.0, and 1.0 as noted.}} == {{int:license}} == {{self|GFDL|Cc-by-sa-3.0-migrated|Cc-by-sa-2.5,2.0,1.0}} [[Category:English pronunciation|" + file_name + "]] "
page.edit(text=wikidata)
move_file(file)
for photo in listing:
# print photo
#` print filetype(photo)
if filetype(photo) in ['JPG','jpg','GIF','gif','png','PNG','ogg','OGG']:
upload_file(photo)