Skip to content

Instantly share code, notes, and snippets.

@DominicBM
Last active August 29, 2015 14:20
Show Gist options
  • Save DominicBM/ebca9677d07c56819082 to your computer and use it in GitHub Desktop.
Save DominicBM/ebca9677d07c56819082 to your computer and use it in GitHub Desktop.
import requests, json, argparse, random, os, Tkinter
parser = argparse.ArgumentParser()
parser.add_argument('--naid', dest='naid', metavar='NAID',
action='store')
parser.add_argument('--tag', dest='tag', metavar='TAG',
action='store')
parser.add_argument('--user', dest='user', metavar='USER',
action='store')
parser.add_argument('--password', dest='password', metavar='PASSWORD',
action='store')
args = parser.parse_args()
naid = args.naid
tag = args.tag
user = args.user
password = args.password
session = requests.Session()
if (args.naid is None or args.tag is None or args.password is None or args.user is None):
print '\n***\n***\n\nWelcome to post.py!\n'
if args.user is None :
user = raw_input(' Enter your username: ')
if args.password is None :
password = raw_input(' Enter your password: ')
login = json.loads(session.post('https://catalog.archives.gov/api/v1/login', data={'user': user, 'password': password}).text)
header = {'Authorization': login['opaResponse']['credentials']}
print header
while 1 == 1:
if args.naid is None :
naid = raw_input(' Enter exactly one NAID: ')
if args.tag is None :
image = json.loads(requests.get('https://catalog.archives.gov/api/v1/?resultTypes=object&naIds=' + naid).text)
# os.system('display -resize 1000x800 -gravity NorthEast ' + image['opaResponse']['results']['result'][0]['objects']['object']['file']['@url'])
tag = raw_input(' Enter at least one tag, using commas to separate them: ')
data = {'text': tag}
q = session.post('https://catalog.archives.gov/api/v1/id/' + str(naid) + '/tags', headers=header, data=data)
print q.request.headers
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---'
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://catalog.archives.gov/id/' + naid + '\n---\n'
tag_number = tag_number - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment