Skip to content

Instantly share code, notes, and snippets.

@DominicBM
Created March 17, 2015 04:05
Show Gist options
  • Save DominicBM/bbb5686650e945ee6520 to your computer and use it in GitHub Desktop.
Save DominicBM/bbb5686650e945ee6520 to your computer and use it in GitHub Desktop.
import ttk, json, requests, PIL, os
from Tkinter import *
from ttk import Frame, Style
from PIL import Image, ImageTk
def topic(*args):
topics = topictext.get()
image = json.loads(requests.get('https://uat.research.archives.gov/api/v1/?resultTypes=object&rows=200&description.item.scopeAndContentNote=' + topics).text)
total = image['opaResponse']['results']['total'] - 1
if total > 199 :
total = 199
naid = image['opaResponse']['results']['result'][total]['naId']
imageurl = image['opaResponse']['results']['result'][int(total)]['objects']['object']['file']['@url']
filename = image['opaResponse']['results']['result'][int(total)]['objects']['object']['file']['@name']
r = requests.get(imageurl, stream=True)
with open(filename, "wb") as image :
image.write(r.content)
tkimage = Image.open(filename)
tkim = ImageTk.PhotoImage(tkimage)
ttk.Label(imageframe, text=naid).pack(side=TOP)
ttk.Label(imageframe, image=tkim).pack(side=TOP)
os.remove(filename)
def userpass(*args):
user = usertext.get()
password = passwordtext.get()
login = json.loads(requests.post('https://uat.research.archives.gov/api/v1/login', data={'user': user, 'password': password}).text)
header = ({'Authorization': login['opaResponse']['credentials']})
if int(login['opaResponse']['header']['@status']) == 200:
ttk.Label(userframe, text=user, foreground="#0000ff", font=('bold'), cursor="pointinghand").pack(side=RIGHT)
ttk.Label(userframe, text='Now logged in as: ').pack(side=RIGHT)
else:
ttk.Label(userframe, text=login['opaResponse']['header']['@status']).pack(side=RIGHT)
topics = topictext.get()
image = json.loads(requests.get('https://uat.research.archives.gov/api/v1/?resultTypes=object&rows=200&description.item.scopeAndContentNote=' + topics).text)
total = image['opaResponse']['results']['total'] - 1
if total > 199 :
total = 199
naid = image['opaResponse']['results']['result'][total]['naId']
imageurl = image['opaResponse']['results']['result'][int(total)]['objects']['object']['file']['@url']
# ttk.Label(imageframe, text=imageurl).pack(side=TOP)
ttk.Label(imageframe, text=naid).pack(side=TOP)
tagdata = {'text': tagtext.get()}
q = requests.post('https://uat.research.archives.gov/api/v1/id/' + str(naid) + '/tags', headers=header, data=tagdata)
try :
print '\n---\nError received:\n ' + json.loads(q.text)['opaResponse']['error']['@code'] + ' (' + json.loads(q.text)['opaResponse']['header']['@status'] + '): \"' + json.loads(q.text)['opaResponse']['error']['description'] + '\"\n---\n'
except KeyError :
tag_number = int(json.loads(q.text)['opaResponse']['tags']['@total']) - 1
while tag_number > -1:
print '\n---\nSuccess!\n Tag \"' + json.loads(q.text)['opaResponse']['tags']['tag'][tag_number]['@text'] + '\" added at ' + json.loads(q.text)['opaResponse']['tags']['tag'][tag_number]['@created'] + '. Check here: https://uat.research.archives.gov/id/' + naid + '\n---\n'
tag_number = tag_number - 1
root = Tk()
root.title("Tagging game")
root.attributes('-fullscreen', True)
style = Style()
userframe = ttk.Frame(root, relief=RAISED, borderwidth=3)
userframe.pack(fill=BOTH)
topicframe = ttk.Frame(root, relief=RAISED, borderwidth=0)
topicframe.pack(fill=BOTH)
imageframe = ttk.Frame(root, relief=RAISED, borderwidth=0)
imageframe.pack(fill=BOTH, expand=1)
tagframe = ttk.Frame(root, relief=RAISED, borderwidth=0)
tagframe.pack(fill=BOTH)
tagtext = StringVar()
topictext = StringVar()
usertext = StringVar()
passwordtext = StringVar()
user_entry = ttk.Entry(userframe, width=7, textvariable=usertext)
password_entry = ttk.Entry(userframe, width=7, textvariable=passwordtext, show='*')
ttk.Label(userframe, text="Username:").pack(side=LEFT)
user_entry.pack(side=LEFT)
ttk.Label(userframe, text="Password:").pack(side=LEFT)
password_entry.pack(side=LEFT)
#ttk.Button(userframe, text="Login", command=userpass).pack(side=LEFT)
topic_entry = ttk.Entry(topicframe, width=40, textvariable=topictext)
ttk.Label(topicframe, text="Enter one or more topics (separated by a comma):").pack(side=LEFT)
topic_entry.pack(side=LEFT)
ttk.Button(topicframe, text="Start", command=topic).pack(side=LEFT)
tag_entry = ttk.Entry(tagframe, width=40, textvariable=tagtext)
ttk.Label(tagframe, text="Enter one or more tags (separated by a comma):").pack(side=LEFT)
tag_entry.pack(side=LEFT)
ttk.Button(tagframe, text="Tag", command=userpass).pack(side=LEFT)
root.bind('<Return>', userpass)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment