{"action":"created","comment":{"url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/comments/170847540","pull_request_review_id":99591175,"id":170847540,"diff_hunk":"@@ -21,112 +22,165 @@\n #  along with eis-waffel. If not, see <http://www.gnu.org/licenses/>.\n #\n \n-#***************************************\n-# App\n+# ***************************************\n+\n+from datetime import datetime\n+from datetime import timedelta\n+\n+import dateutil.parser\n+import logging\n import requests\n \n \n-class Waffel:\n-    # due to historic reasons music is named arts and arts is named campus in EIS\n-    TRACK_NAME_MAP = {\"art\": \"music\", \"campus\": \"arts\"}\n+logging.basicConfig()  # TODO: changed print to logging.\n+logger = logging.getLogger(__file__)\n \n-    def __init__(self, api_url, timeout=30):\n+\n+class Waffel(object):\n+\n+    def __init__(\n+            self,\n+            api_url,\n+            tracks,\n+            locations,\n+            timezone='Europe/Vienna',\n+            timeout=30):\n         self.api_url = api_url\n+        self.track_map = {it['eis_id']: it['id'] for it in tracks}\n+        self.location_map = {it['eis_id']: it['id'] for it in locations}\n+        self.timezone = timezone\n         self.timeout = timeout\n-        self.headers = []\n+        self.year = datetime.now().year\n+        self.headers = {'Accept': 'application/json; charset=utf-8'}\n+        self.min_delta = timedelta(minutes=30)\n+        self.max_delta = timedelta(hours=12)\n+\n+    # due to historic reasons music is named arts and arts is named campus in EIS  # noqa\n+    TRACK_NAME_MAP = {\"art\": \"music\", \"campus\": \"arts\"}\n \n     def __fetch_objects(self, objtype, url):\n         try:\n             r = requests.get(url, headers=self.headers, timeout=self.timeout)\n             r.raise_for_status()\n             ret = r.json()\n             if ret['status'] != 'ok':\n-                print(\"fetching %s failed, API returned status: %s\" % (objtype, ret['status']))\n+                logger.error(\n+                    \"fetching %s failed, API returned status: %s\" % (\n+                        objtype,\n+                        ret['status']\n+                    )\n+                )\n                 return None\n             return ret['result']\n \n         except requests.exceptions.RequestException as e:\n-            print(\"fetching %s failed: %s\" % (objtype, e))\n+            logger.error(\"fetching %s failed: %s\" % (objtype, e))\n             return None\n \n-    def get_events(self, year, track=None):\n-        url = '%s?method=Event.detail&lang=en&year=%d' % (self.api_url, year)\n+    def parse_date(self, dt):\n+        return dateutil.parser.parse(dt)\n+\n+    def dt_to_epoch(self, dt):\n+        # https://stackoverflow.com/a/11743262/1337474\n+        return int((dt - datetime(1970, 1, 1)).total_seconds())\n+\n+    def dt_within(self, start, end):\n+        now = datetime.now()\n+        # now = datetime(2018, 2, 28, 18, 0)  # TODO: use/change this to test\n+        if (\n+            start > now + self.max_delta\n+            or end < now - self.min_delta\n+        ):\n+            return False\n+        return True\n+\n+    def make_event(self, start, end, title, subtitle, track_id):\n+        # \"start\":     Startzeit in der gegebenen Zeitzone als HH:MM\n+        # \"startts\":   Startzeit als unix epoch timestamp\n+        # \"end\":       Endzeit in der gegebenen Zeitzone als HH:MM\n+        # \"endts\":     Endzeit als unix epoch timestamp\n+        # \"title\":     Titel des Event\n+        # \"subtitle\":  Untertitel des Event (optional)\n+        # \"track\":     discourse, music oder arts\n+        #\n+        # TODO: check, if timezone-naive is really OK\n+        #       converting to timezone-aware datetimes shouldn't make a\n+        #       difference.\n+        return {\n+            'start': start.strftime('%H:%M'),\n+            'startts': self.dt_to_epoch(start),\n+            'end': end.strftime('%H:%M'),\n+            'endts': self.dt_to_epoch(end),\n+            'title': title,\n+            'subtitle': subtitle,\n+            'track': self.track_map.get(track_id),\n+        }\n+\n+    def get_events(self, track=None):\n+        url = '%s?method=Event.detail&lang=en&year=%d' % (\n+            self.api_url,\n+            self.year\n+        )\n         if track:\n             url = '%s&track=%s' % (url, track)\n \n-        return self.__fetch_objects(\"events\", url)\n-\n-    def get_locations(self, year, track=None):\n-        url = '%s?method=Location.detail&lang=en&year=%d' % (self.api_url, year)\n+        result = self.__fetch_objects(\"events\", url)\n+\n+        ret = {}\n+        for event in result:\n+            start = self.parse_date(event['begin'])\n+            end = self.parse_date(event['end'])\n+            if not self.dt_within(start, end):\n+                continue\n+            location = self.location_map.get(event['location_id'], None)\n+            if not location:\n+                logger.warn(\n+                    'location_id %s not found in location mapping' %\n+                    event['location_id']\n+                )\n+                continue\n+\n+            events = []\n+            if event['track'] in ('art',):\n+                for appearance in event['apps']:\n+                    app_start = self.parse_date(appearance['begin'])\n+                    app_end = self.parse_date(appearance['end'])\n+                    if not self.dt_within(app_start, app_end):\n+                        continue\n+                    events.append(self.make_event(\n+                        app_start,\n+                        app_end,\n+                        appearance['name'],\n+                        appearance['name_add'],  # TODO: name_add ok? type could also be of interest.  # noqa\n+                        appearance['track'],\n+                    ))\n+            else:\n+                events.append(self.make_event(\n+                    start,\n+                    end,\n+                    event['title'],\n+                    event['subtitle'],\n+                    event['track'],\n+                ))\n+\n+            if not events:\n+                continue\n+            ret.setdefault(location, [])\n+            ret[location] += events\n+\n+        # TODO: needed?\n+        # Sort for startts","path":"waffel/waffel.py","position":174,"original_position":174,"commit_id":"ca86083162c1e736d2a0d12f20c0493eca358a14","original_commit_id":"ca86083162c1e736d2a0d12f20c0493eca358a14","user":{"login":"equinox0815","id":1231362,"avatar_url":"https://avatars1.githubusercontent.com/u/1231362?v=4","gravatar_id":"","url":"https://api.github.com/users/equinox0815","html_url":"https://github.com/equinox0815","followers_url":"https://api.github.com/users/equinox0815/followers","following_url":"https://api.github.com/users/equinox0815/following{/other_user}","gists_url":"https://api.github.com/users/equinox0815/gists{/gist_id}","starred_url":"https://api.github.com/users/equinox0815/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/equinox0815/subscriptions","organizations_url":"https://api.github.com/users/equinox0815/orgs","repos_url":"https://api.github.com/users/equinox0815/repos","events_url":"https://api.github.com/users/equinox0815/events{/privacy}","received_events_url":"https://api.github.com/users/equinox0815/received_events","type":"User","site_admin":false},"body":"I think the lua code does it's own sorting so this not really needed.. but the lua code is highly time constrained as it also generates background animations and other stuff using OpenGL. Therefore it's good that the data is already sorted.","created_at":"2018-02-27T08:37:10Z","updated_at":"2018-02-27T08:45:05Z","html_url":"https://github.com/spreadspace/info-beamer-elevate/pull/1#discussion_r170847540","pull_request_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/1","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/comments/170847540"},"html":{"href":"https://github.com/spreadspace/info-beamer-elevate/pull/1#discussion_r170847540"},"pull_request":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/1","id":171542827,"html_url":"https://github.com/spreadspace/info-beamer-elevate/pull/1","diff_url":"https://github.com/spreadspace/info-beamer-elevate/pull/1.diff","patch_url":"https://github.com/spreadspace/info-beamer-elevate/pull/1.patch","issue_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues/1","number":1,"state":"open","locked":false,"title":"schedule creation","user":{"login":"thet","id":170891,"avatar_url":"https://avatars3.githubusercontent.com/u/170891?v=4","gravatar_id":"","url":"https://api.github.com/users/thet","html_url":"https://github.com/thet","followers_url":"https://api.github.com/users/thet/followers","following_url":"https://api.github.com/users/thet/following{/other_user}","gists_url":"https://api.github.com/users/thet/gists{/gist_id}","starred_url":"https://api.github.com/users/thet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thet/subscriptions","organizations_url":"https://api.github.com/users/thet/orgs","repos_url":"https://api.github.com/users/thet/repos","events_url":"https://api.github.com/users/thet/events{/privacy}","received_events_url":"https://api.github.com/users/thet/received_events","type":"User","site_admin":false},"body":"schedule creation finished so far.\r\n\r\nplease check for ``TODO`` comments and especially review those lines.\r\n\r\nfor testing, you have to change the ``now`` value to a fixed one in waffel.py dt_within function (line 89).","created_at":"2018-02-27T01:52:44Z","updated_at":"2018-02-27T08:45:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"79109ac6c085e75164aebfccc1a90d82f255b79b","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/1/commits","review_comments_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/1/comments","review_comment_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/comments{/number}","comments_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues/1/comments","statuses_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/statuses/ca86083162c1e736d2a0d12f20c0493eca358a14","head":{"label":"spreadspace:thet-eis","ref":"thet-eis","sha":"ca86083162c1e736d2a0d12f20c0493eca358a14","user":{"login":"spreadspace","id":16418234,"avatar_url":"https://avatars3.githubusercontent.com/u/16418234?v=4","gravatar_id":"","url":"https://api.github.com/users/spreadspace","html_url":"https://github.com/spreadspace","followers_url":"https://api.github.com/users/spreadspace/followers","following_url":"https://api.github.com/users/spreadspace/following{/other_user}","gists_url":"https://api.github.com/users/spreadspace/gists{/gist_id}","starred_url":"https://api.github.com/users/spreadspace/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spreadspace/subscriptions","organizations_url":"https://api.github.com/users/spreadspace/orgs","repos_url":"https://api.github.com/users/spreadspace/repos","events_url":"https://api.github.com/users/spreadspace/events{/privacy}","received_events_url":"https://api.github.com/users/spreadspace/received_events","type":"Organization","site_admin":false},"repo":{"id":109186390,"name":"info-beamer-elevate","full_name":"spreadspace/info-beamer-elevate","owner":{"login":"spreadspace","id":16418234,"avatar_url":"https://avatars3.githubusercontent.com/u/16418234?v=4","gravatar_id":"","url":"https://api.github.com/users/spreadspace","html_url":"https://github.com/spreadspace","followers_url":"https://api.github.com/users/spreadspace/followers","following_url":"https://api.github.com/users/spreadspace/following{/other_user}","gists_url":"https://api.github.com/users/spreadspace/gists{/gist_id}","starred_url":"https://api.github.com/users/spreadspace/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spreadspace/subscriptions","organizations_url":"https://api.github.com/users/spreadspace/orgs","repos_url":"https://api.github.com/users/spreadspace/repos","events_url":"https://api.github.com/users/spreadspace/events{/privacy}","received_events_url":"https://api.github.com/users/spreadspace/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/spreadspace/info-beamer-elevate","description":"Info Beamer Package for Elevate Festival","fork":true,"url":"https://api.github.com/repos/spreadspace/info-beamer-elevate","forks_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/forks","keys_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/keys{/key_id}","collaborators_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/teams","hooks_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/hooks","issue_events_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues/events{/number}","events_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/events","assignees_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/assignees{/user}","branches_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/branches{/branch}","tags_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/tags","blobs_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/refs{/sha}","trees_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/trees{/sha}","statuses_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/statuses/{sha}","languages_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/languages","stargazers_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/stargazers","contributors_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/contributors","subscribers_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/subscribers","subscription_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/subscription","commits_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/commits{/sha}","git_commits_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/commits{/sha}","comments_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/comments{/number}","issue_comment_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues/comments{/number}","contents_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/contents/{+path}","compare_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/compare/{base}...{head}","merges_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/merges","archive_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/downloads","issues_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues{/number}","pulls_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls{/number}","milestones_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/milestones{/number}","notifications_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/labels{/name}","releases_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/releases{/id}","deployments_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/deployments","created_at":"2017-11-01T21:34:26Z","updated_at":"2017-11-02T19:10:31Z","pushed_at":"2018-02-27T01:55:16Z","git_url":"git://github.com/spreadspace/info-beamer-elevate.git","ssh_url":"git@github.com:spreadspace/info-beamer-elevate.git","clone_url":"https://github.com/spreadspace/info-beamer-elevate.git","svn_url":"https://github.com/spreadspace/info-beamer-elevate","homepage":"https://elevate.at","size":2328,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"spreadspace:master","ref":"master","sha":"692d6ab73f51481f3d23bf2a978a14cd2a4990b5","user":{"login":"spreadspace","id":16418234,"avatar_url":"https://avatars3.githubusercontent.com/u/16418234?v=4","gravatar_id":"","url":"https://api.github.com/users/spreadspace","html_url":"https://github.com/spreadspace","followers_url":"https://api.github.com/users/spreadspace/followers","following_url":"https://api.github.com/users/spreadspace/following{/other_user}","gists_url":"https://api.github.com/users/spreadspace/gists{/gist_id}","starred_url":"https://api.github.com/users/spreadspace/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spreadspace/subscriptions","organizations_url":"https://api.github.com/users/spreadspace/orgs","repos_url":"https://api.github.com/users/spreadspace/repos","events_url":"https://api.github.com/users/spreadspace/events{/privacy}","received_events_url":"https://api.github.com/users/spreadspace/received_events","type":"Organization","site_admin":false},"repo":{"id":109186390,"name":"info-beamer-elevate","full_name":"spreadspace/info-beamer-elevate","owner":{"login":"spreadspace","id":16418234,"avatar_url":"https://avatars3.githubusercontent.com/u/16418234?v=4","gravatar_id":"","url":"https://api.github.com/users/spreadspace","html_url":"https://github.com/spreadspace","followers_url":"https://api.github.com/users/spreadspace/followers","following_url":"https://api.github.com/users/spreadspace/following{/other_user}","gists_url":"https://api.github.com/users/spreadspace/gists{/gist_id}","starred_url":"https://api.github.com/users/spreadspace/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spreadspace/subscriptions","organizations_url":"https://api.github.com/users/spreadspace/orgs","repos_url":"https://api.github.com/users/spreadspace/repos","events_url":"https://api.github.com/users/spreadspace/events{/privacy}","received_events_url":"https://api.github.com/users/spreadspace/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/spreadspace/info-beamer-elevate","description":"Info Beamer Package for Elevate Festival","fork":true,"url":"https://api.github.com/repos/spreadspace/info-beamer-elevate","forks_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/forks","keys_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/keys{/key_id}","collaborators_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/teams","hooks_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/hooks","issue_events_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues/events{/number}","events_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/events","assignees_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/assignees{/user}","branches_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/branches{/branch}","tags_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/tags","blobs_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/refs{/sha}","trees_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/trees{/sha}","statuses_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/statuses/{sha}","languages_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/languages","stargazers_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/stargazers","contributors_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/contributors","subscribers_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/subscribers","subscription_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/subscription","commits_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/commits{/sha}","git_commits_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/git/commits{/sha}","comments_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/comments{/number}","issue_comment_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues/comments{/number}","contents_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/contents/{+path}","compare_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/compare/{base}...{head}","merges_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/merges","archive_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/downloads","issues_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues{/number}","pulls_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls{/number}","milestones_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/milestones{/number}","notifications_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/labels{/name}","releases_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/releases{/id}","deployments_url":"https://api.github.com/repos/spreadspace/info-beamer-elevate/deployments","created_at":"2017-11-01T21:34:26Z","updated_at":"2017-11-02T19:10:31Z","pushed_at":"2018-02-27T01:55:16Z","git_url":"git://github.com/spreadspace/info-beamer-elevate.git","ssh_url":"git@github.com:spreadspace/info-beamer-elevate.git","clone_url":"https://github.com/spreadspace/info-beamer-elevate.git","svn_url":"https://github.com/spreadspace/info-beamer-elevate","homepage":"https://elevate.at","size":2328,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/1"},"html":{"href":"https://github.com/spreadspace/info-beamer-elevate/pull/1"},"issue":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues/1"},"comments":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/spreadspace/info-beamer-elevate/statuses/ca86083162c1e736d2a0d12f20c0493eca358a14"}},"author_association":"NONE"}}
{"actor":{"display_login":"equinox0815"}}