-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
66 lines (54 loc) · 2.04 KB
/
api.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
###############
#
# api.py is currently written for Python 2.x - a future update will port to Python 3.x
#
##########
from __future__ import print_function
import json
from urllib2 import urlopen
import requests
import time
import sys
# simple text file with system names, one per line
# Important note: The first line of the input file must contain a dash (see included file)
filepath = 'input.txt'
system_name = 'error'
# this provides an api link, and completes the api call with system names from the text file above
baseurl = 'https://www.edsm.net/api-v1/system?showId=1&showCoordinates=1&showInformation=1&showPrimaryStar=1&systemName='
f = open('output.csv', 'w')
with open(filepath) as fp:
count = 2
line = fp.readline()
while line :
try:
line = fp.readline()
stripline = format(line.strip())
url = baseurl + stripline
#print(format(line.strip()))
# get api call response and make it json
response = requests.get(url=url)
data = response.json()
#print(data)
# you could change these to any fields of interest in the EDSM API data.
system_name = (data['name'])
id64 = str(data['id64'])
type = (data['primaryStar']['type'])
spectral_class = type[0]
# Write out to standard output. Could be less lazy and write out to a .csv file.
# output write ID64 value.
output = str(count) + ',' + system_name + ',' + id64
# output write stellar class
#output = str(count) + ',' + system_name + ',' + type
print (output)
print (output, file=f)
count += 1
# sleep for 4 seconds between api calls so we don't break EDSM.
time.sleep(4)
except:
pass
output = str(count) + ',' + 'error' + ',' + '0';
print (output)
print (output, file=f)
#print ("error");
# make sure the line number is incremented so we match source .csv file line numbers
count += 1