Get breakdown of UK and non-UK votes for a Parliamentary Petition (Python)

For various reasons today it's been interesting to pull out what proportion of signatures on a given Parliamentary petition (i.e. one on https://petition.parliament.uk) came from outside of Great Britain.

Petitions are open to British Citizens, including those living overseas, so it's expected that there will be some signatures from other countries, without there being any foul play, this python3 script just helps assess the proportion.

Details

Snippet

#!/usr/bin/env python3
# -*- coding: UTF-8
#
# Usage: petition_count.py [petition url]
#
#

import json
import urllib3
import sys

# get the URL from the command line
url="{}.json".format(sys.argv[1])

# Set up the urllib3 pool and fetch the JSON
http = urllib3.PoolManager()
response = http.request('GET',url)
p=json.loads(response.data.decode('utf8'))
response.release_conn()

total=p["data"]["attributes"]["signature_count"] 

# Iterate over signature countries and count anything not in GB
x=0
y={}
for country in p["data"]["attributes"]["signatures_by_country"]:
  if country["code"] not in ['GB','GI']:
        x=x+country["signature_count"]
        y[country["name"]] = country["signature_count"]

print("Total Signatures: {}".format(total))
print("Non-GB: {}".format(x))

# Print a breakdown of count by non GB country
for c,n in y.items():
   print('{}: {}'.format(c,n))

Usage Example

python3 petitions_count.py https://petition.parliament.uk/petitions/241584