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



Published: 2019-03-21 13:46:11 +0000
Categories: Python,

Language

Python

Description

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.

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

Requires

  • Python 3
  • URLLib3

Video Example

License

BSD-3-Clause

Keywords

python3, petition, count, country, distribution,

Latest Posts

Urldecode string (LUA)
FFMPEG Convert YUV444p to YUV420p (BASH)
Forcing FFMPEG to honour segment length in HLS Stream Creator (BASH)
Force CURL to place request to a specific IP (BASH)
Set a variable only if it is undefined (Javascript)
Ignore query string with Nginx caching proxy (NGinx)
Get breakdown of UK and non-UK votes for a Parliamentary Petition (Python)
Decompress Mozilla Firefox jsonlz4 bookmark backup files (Python)
Setting Git author details for a single repo (Git)
Checking Virtual Machine Resource Allocations with Libvirt (BASH)

Copyright © 2019 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and snippets.6zdgh5a5e6zpchdz.onion