-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (92 loc) · 3.05 KB
/
main.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
# Parade Bulk Event Uploader
# Josh Spicer <[email protected]>
# Hits our GraphQL endpoint, uploading contents of given CSV.
from graphqlclient import GraphQLClient
import os
import sys
import json
############## VARIABLES ##############
debug = False
# Prod
ENDPOINT = 'https://aws-lambda.parade.events/graphql'
ORGANIZATION_ID = "5bdc6c705a0e30d0fd96cdde"
# Dev
#ENDPOINT = 'http://localhost:8080/graphql'
#ORGANIZATION_ID = '5d4f34804201f0645f257ea5'
########################################
if ENDPOINT == '' or ORGANIZATION_ID == '':
print('[-] Must set ENDPOINT and ORGANIZATION_ID variables')
exit(1)
# Set a valid Parade token as an environment variable for auth.
try:
token = os.environ['PARADE_JWT']
except:
print("[-] Must set environment variable: PARADE_JWT")
exit(1)
if len(sys.argv) != 2:
print("[-] Supply a properly formatted .csv file as the first argument.")
exit(1)
# Validates the event input, and submits if good.
def handleLine(line, idx):
try:
ss = line.split('|')
if len(ss) != 8:
print(
"[-] Invalid line length (length={}) on event index {}".format(len(ss), idx))
return
evt = {
"event": {},
"description": {}
}
evt['event']['organizationId'] = ORGANIZATION_ID
# Required Fields
evt['event']['title'] = ss[0]
evt['event']['startTime'] = ss[2]
evt['event']['location'] = ss[4]
# Optional
if ss[3] != '':
evt['event']['endTime'] = ss[3]
if ss[1] != '':
evt['description']['full'] = ss[1]
if ss[5] != '':
evt['event']['imageUrl'] = ss[5]
if ss[6] != '':
evt['event']['tagIds'] = ss[6]
submit(evt, idx)
except Exception as err:
print("[-] Error on event index {}. Error: {}".format(idx, err))
# Parses input file, calling handleLine() on each line.
def parseFile():
with open(sys.argv[1]) as f:
# Skip over the first line of the file,
# as that should be the header
header = f.readline()
# Enumerate each line
for idx, line in enumerate(f):
# DEBUG
if (debug):
print("{} : {} ".format(idx, line))
print("====================")
handleLine(line, idx)
# Submit an event
def submit(evt, idx):
client = GraphQLClient(ENDPOINT)
client.inject_token('Bearer {0}'.format(token))
if debug:
print(evt['event'])
print(evt['description'])
# Execute createEvent mutation
result = client.execute('''
mutation createEvent($event: EventInput!, $description: DescriptionInput!) {
createEvent(event: $event, description: $description) {
id
}
}
''', variables={"event": evt['event'], "description": evt['description']})
# Print result (error, or new event ID)
print("Idx: {0}. Result: {1}".format(idx, result))
# Kick off program
def init():
parseFile()
if __name__ == "__main__":
init()