Table of Contents

Parlameter

Python - download

import sys; wdir = r"D:/vlado/data/parlament"; sys.path.append(wdir)
import os; os.chdir(wdir)

import csv
import json

import requests

def download_votes(
    url: str = "https://parladata-slovenija.lb.djnd.si/v3/votes/", votes: list = []
):
    """Recursively downloads all votes."""
    print(f"Downloading {url}")
    batch = requests.get(url).json()
    votes += batch["results"]
    if batch["next"]:
        download_votes(url=batch["next"], votes=votes)
    return votes

def download_ballots(vote_id: int):
    """Downloads ballots belonging to a particular vote (id)."""
    url = f"https://parladata-slovenija.lb.djnd.si/v3/cards/vote/single/?id={vote_id}"

    print(f"Downloading {url}")
    batch = requests.get(url).json()
    ballots = batch["results"]["members"]
    return ballots

def enrich_votes_with_ballots():
    """Merges votes with ballots, freshly downloads all."""
    votes = download_votes()
    for vote in votes:
        vote["ballots"] = download_ballots(vote["id"])
    return votes

with open("ballots.json", "w") as outfile:
    json.dump(enrich_votes_with_ballots(), outfile)       

Slow Split general data about each voting

votes = download_votes()        
 
with open("votes.json", "w") as outfile:
    json.dump(votes, outfile)

n = len(votes)

n = 2919. Add the ballots details to each voting. par partes

for i in range(400,800):
    vote = votes[i]
    vote["ballots"] = download_ballots(vote["id"])
    
with open("votes800.json", "w") as outfile:
    json.dump(votes, outfile)
{'id': 2921,
 'tags': [],
 'results': {'for': 53,
  'against': 6,
  'abstain': 24,
  'absent': 6,
  'did not vote': 0},
 'has_ballots': True,
 'created_at': '2022-02-23T14:39:46.860733',
 'updated_at': '2022-02-23T14:51:22.543578',
 'name': 'Zakon o interventnih ukrepih za podporo trgu dela - Glasovanje o zakonu v celoti',
 'timestamp': '2022-02-22T15:01:30',
 'needs_editing': False,
 'result': True,
 'motion': 2921,
 'ballots': 'ballots'}

Posebej predlagatelj zakona; oznaka zakona in/ali povezava na besedilo

vote

 {'person': {'slug': '87-marko-bandelli',
   'name': 'Marko Bandelli',
   'honorific_prefix': None,
   'honorific_suffix': None,
   'preferred_pronoun': '',
   'group': {'name': 'Stranka Alenke Bratušek',
    'acronym': 'SAB',
    'slug': '8-stranka-alenke-bratusek',
    'color': '#C9736A',
    'classification': 'pg'},
   'image': 'https://s3.fr-par.scw.cloud/parlameter/slovenija/P341.png'},
  'option': 'for'},

option for, abstain, absent, against, did not vote (restrain)

print(votes[10]["ballots"][0])

vote = votes[n-1]
vote["ballots"] 

import copy

i = 1414
v = copy.deepcopy(votes[i])
v["ballots"] = "ballots"
v

# show first ballot
with open("votesfinal.json", "r") as infile:
    votes = json.load(infile)
    print(votes[0]["ballots"][0])

seznam glasovanj

with open("list.txt", "w", encoding='utf-8') as lst:
    for i, vote in enumerate(votes):
        lst.write(
            "{0} {1} {2} {3}{4}".format(
                str(i).rjust(4),
                str(vote["id"]).rjust(6),
                vote["timestamp"],
                vote["name"],
                "\n"
            )              
        )

R

> wdir <- "C:/Users/vlado/DL/data/parlament"
> setwd(wdir)
> library(rjson)
> js <- "votesfinal.json"
> V <- fromJSON(file=js)
> length(V)
[1] 2919
> names(V[[1]])
 [1] "id"            "tags"          "results"       "has_ballots"   "created_at"    "updated_at"   
 [7] "name"          "timestamp"     "needs_editing" "result"        "motion"        "ballots"      
> length(V[[1]]$ballots)
[1] 89
> typeof(V[[1]]$ballots)
[1] "list"
> V[[1]]$ballots[[1]]
$person
$person$slug                 [1] "23-mojca-znidaric"
$person$name                 [1] "Mojca Žnidarič"
$person$honorific_prefix     NULL
$person$honorific_suffix     NULL
$person$preferred_pronoun    [1] ""
$person$group
$person$group$name           [1] "Stranka modernega centra"
$person$group$acronym        [1] "SMC"
$person$group$slug           [1] "5-stranka-modernega-centra"
$person$group$color          [1] "#09a2cc"
$person$group$classification [1] "pg"
$person$image                [1] "https://s3.fr-par.scw.cloud/parlameter/slovenija/P316.png"
$option                      [1] "for"
> n <- length(V)
> Time <- Name <- P <- N <- A <- R <- Z <- Id <- rep(NA,n)
> for(i in 1:n) { Id[i] <- V[[i]]$id; P[i] <- V[[i]]$results$'for';
+   N[i] <- V[[i]]$results$against; A[i] <- V[[i]]$results$abstain;
+   Z[i] <- V[[i]]$results$absent; R[i] <- V[[i]]$results$'did not vote';
+   Time[i] <- V[[i]]$timestamp; Name[i] <- V[[i]]$name }

> code <- c(); code['for'] <- "p"; code['abstain'] <- "a"
> code['absent'] <- "z"; code['against'] <- "n"; code['did not vote'] <- "r"

> Per["Igor Zorčič"] <- 1; m <- 1
> for(i in 1:n){
+   b <- V[[i]]$ballots; k <- 0
+   for(e in b) {
+     nam <- e$person$name; k <- k+1
+     j <- as.integer(Per[nam])
+     if(is.na(j)){
+       m <- m+1; Per[nam] <- m; j <- m 
+     }
+   }
+ }
> m <- length(Per)
> M <- matrix("z",nrow=n,ncol=m)

> 
> Per["Igor Zorčič"] <- 1; m <- 1
> for(i in 1:n){
+   b <- V[[i]]$ballots; k <- 0
+   for(e in b) {
+     nam <- e$person$name; k <- k+1
+     j <- as.integer(Per[nam])
+     if(is.na(j)){
+       m <- m+1; Per[nam] <- m; j <- m 
+     }
+     M[i,j] <- code[e$option]
+   }
+ }
> 

> Nam <- rep("",n)
> for(i in 1:n) Nam[i] <- substr(Name[i],1,25)
> rownames(M) <- Nam
> colnames(M) <- names(Per)
> write.table(M, 'votes.mat')

> ZI <- data.frame(id=Id,name=Name,time=Time,p=P,n=N,a=A,r=R,z=Z)
> write.csv(ZI,file="zakoni.csv")

Notes

Dodati značke +invalidi , -invalidi … področja

vlado/work/2m/dat/parlameter.txt · Last modified: 2022/05/11 21:14 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki