-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
312 lines (277 loc) · 11.4 KB
/
gui.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
try:
import json
except:
import simplejson as json
from operator import itemgetter, attrgetter
from random import randint
from tkFileDialog import askdirectory
from tkFileDialog import askopenfilename
from Tkinter import *
from tkSimpleDialog import askstring
import base64
import copy
import fnmatch
import httplib
import md5
import mimetypes
import os
import shutil
import sqlite3
import string
import sys
import time
import tkFont
import tkSimpleDialog
import urllib
import urllib2
DASE_HOST = 'daseupload.laits.utexas.edu'
DASE_BASE = '/'
PROTOCOL = 'https'
def rfc3339():
""" Format a date the way Atom likes it (RFC3339)
"""
return time.strftime('%Y-%m-%dT%H:%M:%S%z')
class ScrolledText(Frame):
""" from O'Reilly Programming Python 3rd ed.
"""
def __init__(self, parent=None, text='', file=None):
Frame.__init__(self, parent)
self.pack(expand=YES, fill=BOTH) # make me expandable
self.makewidgets()
self.settext(text, file)
def makewidgets(self):
sbar = Scrollbar(self)
text = Text(self, relief=SUNKEN,height=18)
sbar.config(command=text.yview) # xlink sbar and text
text.config(yscrollcommand=sbar.set) # move one moves other
sbar.pack(side=RIGHT, fill=Y) # pack first=clip last
text.pack(side=LEFT, expand=YES, fill=BOTH) # text clipped first
self.text = text
def addtext(self,text):
self.text.insert(END,text)
self.text.insert(END,"\n")
self.update_idletasks()
self.text.focus() # save user a click
self.text.yview_pickplace("end")
def settext(self, text='', file=None):
if file:
text = open(file, 'r').read()
self.text.delete('1.0', END) # delete current text
self.text.insert('1.0', text) # add at line 1, col 0
self.text.mark_set(INSERT, '1.0') # set insert cursor
self.text.focus() # save user a click
def gettext(self): # returns a string
return self.text.get('1.0', END+'-1c') # first through last
class Application():
def __init__(self, master):
self.eid = ''
self.root = master
frame = Frame(master)
frame.pack(fill=BOTH,padx=2,pady=2)
self.f1 = tkFont.Font(family="arial", size = "14", weight = "bold")
self.titleLabel = Label(frame, width=66, padx = '3', pady = '3', font = self.f1,anchor=W)
self.titleLabel.pack()
self.report = ScrolledText(frame)
menu = Menu(frame)
self.menu = menu
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="Login/Exit", menu=filemenu)
filemenu.add_command(label="Login", command=self.login_user)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
self.clear_button = Button(frame, text="Clear",command=self.clear)
self.clear_button.pack(side=RIGHT)
self.upload_button = Button(frame, text="Upload Recent Citations",command=self.upload)
self.upload_button.pack(side=RIGHT)
#self.login_button = Button(frame, text="Login",command=self.login_user)
#self.login_button.pack(side=LEFT)
self.write("Please login\n")
self.frame = frame
self.collection = ''
self.user = ''
def logout_user(self):
pass
def upload(self):
if not self.eid:
self.login_user()
ZOTERO_DIR = '../Library/Application Support/Firefox'
#ZOTERO_DIR = '../.mozilla'
already_uploaded = {}
try:
f = open('checklist.json','r')
already_uploaded = json.load(f)
f.close()
except:
pass
zpath = self.find_zotero_db(ZOTERO_DIR)
if not zpath:
self.write("No Zotero found. Is ZOTERO_DIR set correctly?")
sys.exit()
db_file = self.make_zotero_db(zpath)
for item in self.get_items(db_file):
self.write(item['item_type']+' '+item['key'])
self.write(item['title'])
if item['key'] in already_uploaded:
self.write("already uploaded")
else:
already_uploaded[item['key']] = item['title']
self.write(self.post_item(json.dumps(item)))
f = open('checklist.json','w')
json.dump(already_uploaded,f)
f.close()
def login_user(self):
eid = askstring('enter your EID','EID')
self.eid = eid
self.write(eid+' is logged in')
self.logout_button = Button(self.frame, text="Logout "+eid,command=self.logout_user)
self.logout_button.pack(side=LEFT)
def write(self,text,delete_text=False):
if delete_text:
self.report.settext(text)
else:
self.report.addtext(text)
def clear(self):
self.report.settext('')
def checkMd5(self,coll,md5):
h = self.getHTTP()
h.request("GET",DASE_BASE.rstrip('/')+'/collection/'+coll+'/items/by/md5/'+md5+'.txt')
r = h.getresponse()
if 200 == r.status:
self.write(r.read())
return True
else:
return False
def abort_upload(self):
self.root.destroy()
#FROM get_data.py
def make_zotero_db(self,current_path):
new_path = ''.join(['zotero',str(int(time.time())),'.db'])
shutil.copyfile(current_path,new_path)
return new_path
def find_zotero_db(self,dir):
for dirname, dirnames, filenames in os.walk(dir):
for filename in filenames:
if ('zotero.sqlite' == filename):
return os.path.join(dirname, filename)
def get_items(self,db_file):
all = []
conn = sqlite3.connect(db_file)
items_c = conn.cursor()
sql = "SELECT key,itemID,itemTypeID FROM items ORDER BY dateAdded DESC"
items_c.execute(sql)
for item_row in items_c:
item_meta = {}
item_meta['key'] = []
# item_meta['notes'] = []
item_meta['tags'] = []
key = itemID = itemTypeID = ''
(key,itemID,itemTypeID) = item_row
item_meta['key'].append(key)
item_creators_c = conn.cursor()
sql = "SELECT creatorID,creatorTypeID, orderIndex FROM itemCreators WHERE itemID = ?"
item_creators_c.execute(sql,(itemID,))
item_meta['creator'] = []
for creator_row in item_creators_c:
creatorID = creatorTypeID = creatorDataID = orderIndex = ''
(creatorID,creatorTypeID,orderIndex) = creator_row
if 'creator'+str(orderIndex) not in item_meta:
item_meta['creator'+str(orderIndex)] = []
creators_c = conn.cursor()
sql = "SELECT creatorDataID FROM creators WHERE creatorID = ?"
creators_c.execute(sql,(creatorID,))
(creatorDataID,) = creators_c.fetchone()
creatordata_c = conn.cursor()
sql = "SELECT firstName,lastName FROM creatorData WHERE creatorDataID = ?"
creatordata_c.execute(sql,(creatorDataID,))
for creator_data in creatordata_c:
firstName = lastName = ''
(firstName,lastName) = creator_data
item_meta['creator'].append(lastName+' '+firstName)
item_meta['creator'+str(orderIndex)].append(lastName+' '+firstName)
item_collections_c = conn.cursor()
sql = "SELECT collectionID FROM collectionItems WHERE itemID = ?"
item_collections_c.execute(sql,(itemID,))
item_meta['folder'] = []
for collection_row in item_collections_c:
collectionID = ''
(collectionID,) = collection_row
collections_c = conn.cursor()
sql = "SELECT collectionName FROM collections WHERE collectionID = ?"
collections_c.execute(sql,(collectionID,))
(collectionName,) = collections_c.fetchone()
item_meta['folder'].append(collectionName)
item_data_c = conn.cursor()
sql = "SELECT fieldID,valueID FROM itemData WHERE itemID = ?"
item_data_c.execute(sql,(itemID,))
for data_row in item_data_c:
fieldID = valueID = value = ''
(fieldID,valueID) = data_row
field_c = conn.cursor()
sql = "SELECT fieldName FROM fields WHERE fieldID = ?"
field_c.execute(sql,(fieldID,))
(fieldName,) = field_c.fetchone()
if fieldName not in item_meta:
item_meta[fieldName] = []
value_c = conn.cursor()
sql = "SELECT value FROM itemDataValues WHERE valueID = ?"
value_c.execute(sql,(valueID,))
(value,) = value_c.fetchone()
try:
item_meta[fieldName].append(str(value))
except:
item_meta[fieldName].append(value.encode('utf8'))
# item_notes_c = conn.cursor()
# sql = "SELECT note,title FROM itemNotes WHERE itemID = ?"
# item_notes_c.execute(sql,(itemID,))
# for notes_row in item_notes_c:
# note = title = ''
# (note,title) = notes_row
# item_meta['notes'].append('['+title+'] '+note)
item_meta['itemType'] = []
item_type_c = conn.cursor()
sql = "SELECT typeName FROM itemTypes WHERE itemTypeID = ?"
item_type_c.execute(sql,(itemTypeID,))
typeName = ''
(typeName,) = item_type_c.fetchone()
item_tags_c = conn.cursor()
sql = "SELECT tagID FROM itemTags WHERE itemID = ?"
item_tags_c.execute(sql,(itemID,))
for tag_row in item_tags_c:
tagID = ''
(tagID,) = tag_row
tag_c = conn.cursor()
sql = "SELECT name FROM tags WHERE tagID = ?"
tag_c.execute(sql,(tagID,))
(name,) = tag_c.fetchone()
item_meta['tags'].append(name)
item = {}
item['item_type'] = typeName
item['metadata'] = item_meta
item['key'] = key
if 'title' in item['metadata']:
item['title'] = item['metadata']['title'][0]
else:
item['title'] = key
all.append(item)
return all
def post_item(self,item_json):
DASE_HOST = 'daseupload.laits.utexas.edu'
h = httplib.HTTPSConnection(DASE_HOST,443)
auth = 'Basic ' + string.strip(base64.encodestring('pkeane:pubsub'))
body = item_json
headers = {
"Content-Type":'application/json',
"Content-Length":str(len(body)),
"Authorization":auth
};
h.request("POST",'/collection/ut_publications?auth=service',body,headers)
r = h.getresponse()
return r.status
if __name__ == "__main__":
root = Tk()
root.title('Zotero Poster')
root.geometry('+25+25')
app = Application(root)
root.mainloop()