Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
cupsPrinters/createEntryAndAddAttachment.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
150 lines (130 sloc)
4.78 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import datetime | |
import time | |
import requests | |
import sys | |
import json | |
def statusCheck(status): | |
if status < 0: | |
print 'Stopping this program due to an previous error.' | |
sys.exit() | |
def getToken(server, hostname, email, password): | |
myApi = server + '/api/v2/auth/login' | |
myHeaders = {'Content-Type':'application/json; charset=UTF-8'} | |
myData = {"user":email, "password":password} | |
if hostname == 'labfolder': | |
myVerify = True | |
else: | |
myVerify = False | |
myRequest = requests.post(myApi, headers=myHeaders, data=json.dumps(myData), verify=myVerify) | |
myJson = myRequest.json() | |
try: | |
return myJson.get('token') | |
except: | |
whoAmI = sys._getframe().f_code.co_name | |
print 'ERROR @ ' + whoAmI + ': The request returned something invalid.' | |
print myJson | |
return -1 | |
def getProjectID(server, hostname, token, limit, projectname): | |
whoAmI = sys._getframe().f_code.co_name | |
### limit : maximum number of projects to return | |
myApi = server + '/api/v2/projects?limit=' + str(limit) | |
myHeaders = {'Authorization':'Token '+str(token), 'Content-Type':'application/json; charset=UTF-8'} | |
if hostname == 'labfolder': | |
myVerify = True | |
else: | |
myVerify = False | |
myRequest = requests.get(myApi, headers=myHeaders, verify=myVerify) | |
myJson = myRequest.json() | |
try: | |
myJson[0] | |
except: | |
print 'ERROR @ ' + whoAmI + ': The request returned something invalid.' | |
print myJson | |
return -1 | |
g = 0 | |
while True: | |
try: | |
myProject = myJson[g] | |
if myProject.get('title') == projectname: | |
return myProject.get('id') | |
except IndexError: | |
break | |
g += 1 | |
print 'ERROR @ ' + whoAmI + ': Could not find the project "' + projectname + '". Establish a new project before running this script.' | |
return -1 | |
def addNewEntryToProject(server, hostname, token, projectID, title, tags): | |
myApi = server + '/api/v2/entries' | |
myHeaders = {'Authorization': 'Token ' + str(token), 'Content-Type': 'application/json; charset=UTF-8'} | |
if tags: | |
myData = {'project_id': str(projectID), 'title': title, 'tags': tags} | |
else: | |
myData = {'project_id': str(projectID), 'title': title} | |
if hostname == 'labfolder': | |
myVerify = True | |
else: | |
myVerify = False | |
myRequest = requests.post(myApi, headers=myHeaders, data=json.dumps(myData), verify=myVerify) | |
myJson = myRequest.json() | |
try: | |
entryID = myJson.get('id') | |
return entryID | |
except: | |
whoAmI = sys._getframe().f_code.co_name | |
print 'ERROR @ ' + whoAmI + ': The request returned something invalid.' | |
print myJson | |
return -1 | |
def addAttachment(server, hostname, token, entryID, filename): | |
fname = filename.split("/")[-1] | |
myApi = server + '/api/v2/elements/file?entry_id=' + entryID + '&file_name=' + fname | |
fileExtension = filename.split(".")[-1] | |
contentType = 'image/' + fileExtension | |
myHeaders = {'Authorization': 'Token ' + str(token), | |
#'User-Agent': 'PythonSDK; ' + user_agent , | |
'Content-Type': contentType} | |
myData = open(filename, 'rb').read() | |
if hostname == 'labfolder': | |
myVerify = True | |
else: | |
myVerify = False | |
myRequest = requests.post(myApi, headers=myHeaders, data=myData, verify=myVerify) | |
myJson = myRequest.json() | |
try: | |
attachmentID = myJson.get('id') | |
return attachmentID | |
except: | |
whoAmI = sys._getframe().f_code.co_name | |
print 'ERROR @ ' + whoAmI + ': The request returned something invalid.' | |
print myJson | |
return -1 | |
#python $pythonScript "$SERVER" "HOSTNAME" "$USER" "$PASSWORD" "$PROJECTNAME" ${TSTRING}.${EXT} ${AUTHOR} ${LPHOST} lplisten.log | |
server = sys.argv[1] | |
hostName = sys.argv[2] | |
email = sys.argv[3] | |
password = sys.argv[4] | |
projectName = sys.argv[5] | |
fileName = sys.argv[6] | |
personWhoPrinted = sys.argv[7] | |
printHost = sys.argv[8] | |
log = sys.argv[9] | |
try: | |
old_stdout = sys.stdout | |
log_file = open(log,"a") | |
sys.stdout = log_file | |
token = getToken(server, hostName, email, password) | |
#print(' Token: ' + token) | |
limit = 1000 | |
projectID = getProjectID(server, hostName, token, limit, projectName) | |
print(' Project ID: {}'.format(projectID)) | |
title = "Sent by " + personWhoPrinted + " from " + printHost | |
tags = ['Printing'] | |
entryID = addNewEntryToProject(server, hostName, token, projectID, title, tags) | |
print(' Entry ID: {}'.format(entryID)) | |
attachmentID = addAttachment(server, hostName, token, entryID, fileName) | |
print(' Attachment ID: {}'.format(attachmentID)) | |
except IOError: | |
print('ERROR: Could not open or write to logfile') | |
finally: | |
sys.stdout = old_stdout | |
log_file.close() | |