]> git.somenet.org - pub/jan/aic18.git/blob - camunda-overlay/camunda.py
try to receive pdf as base64
[pub/jan/aic18.git] / camunda-overlay / camunda.py
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 import json
6 import requests
7 import argparse
8 from pprint import pprint
9
10 CAMUNDA="http://localhost:8080/engine-rest/"
11
12 def get_current_deployments(key = 'sentiment-analysis'):
13     res = requests.get(CAMUNDA + "deployment")
14     #return [proc for proc in res.json() if proc['name'] == key]
15     return res.json()
16
17 def get_current_process_instances(key = 'sentiment-analysis'):
18     res = requests.get(CAMUNDA + 'process-instance')
19     if (key is None):
20         return res.json()
21     else:
22         return [instance for instance in res.json() if instance['definitionId'].startswith(key + ":")]
23
24 def cleanup_old_deployments(key='sentiment-analysis'):
25     print ("Cleaning up old deployments")
26     for deployment in get_current_deployments(key):
27         res = requests.delete(CAMUNDA + "deployment/" + deployment['id'] + "?cascade=true&skipCustomListeners=true")
28         if (res.status_code == 204):
29             print ("Cleaned up deployment {}".format(deployment['id']))
30         else:
31             print ("Error cleaning old deployment {}: Code: {}".format(deployment['id'], res.status_code))
32             try:
33                 pprint(res.json())
34             except:
35                 pprint(res.content)
36
37 def create_deployment(cleanup=False):
38     parameters = [
39             ("deployment-name", "sentiment-analysis"),
40             #("enable-duplicate-filtering", "true"),
41             #("deploy-changed-only", "true"),
42             ("file1", open("sentiment-analysis.bpmn")),
43             ("file2", open("forms/input-terms.html")),
44             ("file3", open("forms/download-pdf.html")),
45             ]
46     if cleanup:
47         cleanup_old_deployments()
48     res = requests.post(CAMUNDA + "deployment/create", files=parameters)
49     if (res.status_code == 200):
50         print ("Successfully deployed Sentiment Analysis")
51     else:
52         pprint ("Status Code: {}".format(res.status_code))
53         try:
54             pprint(res.json())
55         except:
56             pprint(res.content)
57
58 def submit_terms(terms):
59     termlist = [{'term': term} for term in terms]
60     # submit to camunda
61     params = {
62             'variables': {
63                 'terms': {
64                     'value': json.dumps(termlist),
65                     'type': "json",
66                     'valueInfo': {
67                         'serializationDataFormat': "application/json",
68                         }
69                     }
70                 }
71             }
72     res = requests.post(CAMUNDA + "process-definition/key/sentiment-analysis/start", json=params)
73     if (res.status_code == 200):
74         print ("Successfully started Sentiment Analysis")
75     else:
76         pprint ("Status Code: {}".format(res.status_code))
77         try:
78             pprint(res.json())
79         except:
80             try:
81                 import xml.dom.minidom
82                 content = res.content.decode('utf-8').replace('<!doctype html>', '')
83                 print(xml.dom.minidom.parseString(content).toprettyxml())
84             except:
85                 pprint(res.content)
86
87 def download_pdf():
88     instances = get_current_process_instances()
89     if len(instances) == 0:
90         print ("Error: no running instance found.")
91         return
92     instance = instances[0]['id']
93     res = requests.get(CAMUNDA + 'process-instance/' + instance + '/variables')
94     try:
95         pprint(res.json())
96     except:
97         pprint(res.content)
98
99 if __name__ == "__main__":
100     parser = argparse.ArgumentParser()
101     parser.add_argument('--no-deploy', dest='deploy', default=True, action='store_false', help="Do not run the deployment step")
102     parser.add_argument('--no-cleanup', dest='cleanup', default=True, action='store_false', help="Initial deploy does not need cleanup")
103     parser.add_argument('--autoclick', dest='autoclick', type=int, default=0, help="How many steps to autoclick")
104     args = parser.parse_args()
105
106     if args.deploy:
107         # initialize camunda process
108         create_deployment(cleanup=args.cleanup)
109
110     if args.autoclick >= 1:
111         # start clicking
112         submit_terms(["voting", "phonegate", "35c3"])
113
114     if args.autoclick >= 2:
115         download_pdf()