-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodel.xml
More file actions
72 lines (67 loc) · 2.76 KB
/
model.xml
File metadata and controls
72 lines (67 loc) · 2.76 KB
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
<project name="python_connector" threads="1" pubsub="auto" heartbeat-interval="1" luaroot="@ESP_PROJECT_OUTPUT@/luaroot">
<description><![CDATA[This project contains a Source window with a Python connector that calls a REST API to obtain Star Wars data and publishes that data into the project.]]></description>
<metadata>
<meta id="layout">{"cq1":{"Source":{"x":-435,"y":-400}}}</meta>
<meta id="studioUploadedBy">anonymousUser</meta>
<meta id="studioUploaded">1774432525395</meta>
<meta id="studioModifiedBy">anonymousUser</meta>
<meta id="studioModified">1774434468698</meta>
<meta id="studioTags">Example</meta>
</metadata>
<contqueries>
<contquery name="cq1">
<windows>
<window-source index="pi_HASH" name="Source">
<description><![CDATA[This project contains a Source window with a Python connector that calls a REST API to obtain Star Wars data and publishes that data into the project.]]></description>
<schema>
<fields>
<field name="name" type="string" key="true"/>
<field name="description" type="string"/>
</fields>
</schema>
<connectors>
<connector class="python" name="pub">
<properties>
<property name="type"><![CDATA[pub]]></property>
<property name="interval"><![CDATA[3 seconds]]></property>
<property name="code"><![CDATA[import esp
import json
import urllib.request
BASE_URL='https://starwars-databank-server.onrender.com' # Base URL
STARWARS_SPECIES_URL = f"{BASE_URL}/api/v1/species" # Star Wars Species
next_page = f"{STARWARS_SPECIES_URL}?page=1"
def publish():
global next_page
esp.logMessage(logcontext="my_connector",
message=f'Making API call to {next_page}',
level="info")
response = urllib.request.urlopen(next_page)
response = response.read().decode('utf-8')
response = json.loads(response)
events = []
# loop through species results
for result in response['data']:
keep_keys = ['name', 'description']
event = dict((k, result[k]) for k in keep_keys if k in result)
events.append(event)
done = False
if response['info']['next'] is None:
done = True
esp.logMessage(logcontext="my_connector",
message='Connector finished',
level="info")
elif type(response['info']['next']) == str:
next_page = f"{BASE_URL}" + response['info']['next']
return {
"events": events,
"done": done
}
]]></property>
</properties>
</connector>
</connectors>
</window-source>
</windows>
</contquery>
</contqueries>
</project>