This repository was archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_links.py
85 lines (63 loc) · 2.74 KB
/
generate_links.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
# This file is part of ns_utilities
# Copyright 2017 Khronion <khronion@gmail.com>
#
# ns_utilities is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ns_utilities is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ns_utilities. If not, see <http://www.gnu.org/licenses/>.
import urllib.request
import xml.etree.cElementTree as et
import time
import math
# Configuration
region = "Target Region Here"
user_agent = "Nation Name <Email Address>"
rate = 40 / 30 # Do not exceed 50 / 30
# End Configuaration
def fix(s):
return s.lower().replace(" ", "_")
region_query = "https://www.nationstates.net/cgi-bin/api.cgi?region={}" \
"&q=delegateauth+nations+officers+founderauth+delegate+delegateauth+delegatevotes+founder"\
.format(fix(region))
nation_query = "https://www.nationstates.net/cgi-bin/api.cgi?nation={}&q=endorsements"
# get list of nations
print("Getting data for region " + region)
region_api_call = urllib.request.Request(url=region_query, headers={'User-Agent': user_agent})
time.sleep(rate)
nation_list = et.fromstring((urllib.request.urlopen(region_api_call).read().decode())).find("NATIONS").text.split(":")
# generate unique ID for every nation
id = {}
i = 0
with open(fix(region) + '.nodes.csv', 'w') as out:
out.write('id,Label\n')
for nation in nation_list:
id[nation] = i
out.write(str(i) + "," + nation + '\n')
i += 1
# process nations and generate CSV of all links
with open(fix(region) + ".edges.csv", 'w') as out:
print("Processing endorsements for " + region)
out.write("Source,Target\n")
progress = 0
for nation in nation_list:
# query API
nation_api_call = urllib.request.Request(url=nation_query.format(nation), headers={'User-Agent': user_agent})
time.sleep(rate)
try:
endorsement_list = et.fromstring((urllib.request.urlopen(nation_api_call).read().decode()))\
.find("ENDORSEMENTS").text.split(",")
for endorsement in endorsement_list:
print("\t" + endorsement + " endorsing " + nation)
out.write(str(id[endorsement]) + ',' + str(id[nation]) + '\n')
except AttributeError:
pass # nation has no endorsements
progress += 1
print(str(math.floor(progress / len(nation_list)*100)) + "% complete")