Last active
March 14, 2019 23:25
-
-
Save dipietrantonio/66b146f33f62af084f79233951a4aa23 to your computer and use it in GitHub Desktop.
Parse a doodle poll from plain html page.
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
""" | |
Author: Cristian Di Pietrantonio | |
Doodle scheduler | |
""" | |
import json | |
import requests | |
import datetime | |
def parse_doodle(pollID): | |
""" | |
Retrieves poll data from doodle.com given the poll identifier. | |
Parameters: | |
----------- | |
- `pollID`: the poll identifier (get it from the url) | |
Returns: | |
-------- | |
a tuple (participants, options, calendar) where | |
- `participants` is a mapping userID -> userName | |
- `options` is a list of datetime objects, representing the poll options | |
- `calendar` is a mapping optionIndex -> list of userID | |
""" | |
JSON = requests.get("https://doodle.com/api/v2.0/polls/" + pollID).content.decode('utf-8') | |
JSON = json.loads(JSON) | |
options = [datetime.datetime.fromtimestamp(x['start']/1000) for x in JSON['options']] | |
calendar = dict([(i, list()) for i in range(len(options))]) | |
participants = dict() | |
for participant in JSON['participants']: | |
pID = participant['id'] | |
pName = participant['name'] | |
participants[pID] = pName | |
for i, pref in enumerate(participant['preferences']): | |
if pref == 1: | |
calendar[i].append(pID) | |
for k in calendar: | |
if len(calendar[k]) == 0: | |
calendar[k].append(-1) # empty shift | |
participants[-1] = "<vuoto>" | |
return participants, options, calendar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment