-
-
Save chrisport/b27cd5f9c259c5cdc491d4221a32e2aa to your computer and use it in GitHub Desktop.
A python script to extract votes from a mural response.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import json | |
# The mural.json must contain the JSON response of the Mural API of your Mural. | |
# You can get it from your browser directly using 'Inspect': | |
# 1) Open Inspect and the 'network' tab in your browser (https://developer.chrome.com/docs/devtools/network/) | |
# 2) Enter the Mural you want to export votings from | |
# 3) Search for "/api/murals" and copy the response (starting with '{"version":5,"width":...') | |
# 4) Paste it into mural.json | |
file_name='mural.json' | |
# if True, only unique votes get counted, if False multiple votes of same person on same sticky will be counted | |
count_unique_votes=True | |
# process voting response | |
def count_votes(votes): | |
if count_unique_votes: | |
return sum(1 for vote in votes if votes[vote] > 0) | |
else: | |
return sum(votes[vote] for vote in votes) | |
def extract_votes(widgets, voting_session): | |
for card_id in voting_session['votes']: | |
card_votes = count_votes(voting_session['votes'][card_id]) | |
if card_id in widgets: | |
widget_name = widgets[card_id]['properties']['text'].replace(",", ".") | |
yield [widget_name, card_votes] | |
def extract_voting_sessions(data): | |
for votingSessionName in data['votingSessions']: | |
voting_session = data['votingSessions'][votingSessionName] | |
votes = list(extract_votes(data['widgets'], voting_session)) | |
yield { | |
"name": votingSessionName, | |
"votes": votes | |
} | |
# Main / IO | |
def write_voting_session_to_csv(voting_session): | |
with open(f'{voting_session["name"]}.csv', 'w', newline='') as csvfile: | |
csv_writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) | |
csv_writer.writerow(["Item","Votes"]) | |
[csv_writer.writerow(voted_item) for voted_item in voting_session["votes"]] | |
def convert_response_to_csv(fileName): | |
with open(fileName) as json_file: | |
data = json.load(json_file) | |
[write_voting_session_to_csv(voting_session) for voting_session in extract_voting_sessions(data)] | |
convert_response_to_csv(file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment