{"action":"created","comment":{"url":"https://api.github.com/repos/hasadna/anyway/pulls/comments/40071713","id":40071713,"diff_hunk":"@@ -0,0 +1,110 @@\n+import email\n+import imaplib\n+import os\n+from datetime import datetime, timedelta\n+import time\n+from process import time_delta\n+import sys\n+import argparse\n+\n+##############################################################################################\n+# importmail.py is responsible for extracting and downloading united hatzala email attachments\n+# Requirements: Setting an environment variable named MAILPASS containing the password\n+# Note: 1. This script is being called by united.py prior to DB import\n+#       2. If 'detach_dir' is empty the script would download all available files\n+#          If it's not, only the last file would be downloaded\n+#       3. Consider emptying the mail directory from time to time to speed things up\n+#       4. Command line arguments:\n+#           --password can provide password via cmd line instead of an env variable\n+#           --lastmail is currently set to default True\n+##############################################################################################\n+\n+\n+def main():\n+    maildir = 'united-hatzala/data'\n+    detach_dir = 'static/data/united'\n+    username = 'anyway@anyway.co.il'\n+\n+    parser = argparse.ArgumentParser()\n+    parser.add_argument('--password', default='')\n+    parser.add_argument('--lastmail', action='store_true', default=False)\n+    args = parser.parse_args()\n+\n+    try:\n+        if args.password:\n+            passwd = args.password\n+        else:\n+            passwd = os.environ['MAILPASS']     # Set and environment variable MAILPASS with the password\n+    except:\n+        print \"Please set env var MAILPASS, or provide one using importmail.py --password <pass>\"\n+        exit()\n+\n+    imapsession = imaplib.IMAP4_SSL('imap.gmail.com')\n+    try:\n+        imapsession.login(username, passwd)\n+    except:\n+        print 'Bad credentials, Not able to sign in!'\n+        exit()\n+\n+    try:\n+        imapsession.select(maildir)\n+        typ, data = imapsession.search(None, 'ALL')\n+    except:\n+        print 'Error searching given maibox: %s' % maildir\n+        exit()\n+\n+    filefound = False\n+    listdir = os.listdir(detach_dir)\n+\n+    isempty = True if not listdir or len(listdir) == 1 or not args.lastmail else False\n+    total = 0\n+\n+    # Iterating over all emails\n+    started = datetime.now()\n+    print \"Login successful! Importing files, please hold...\"\n+    for msgId in data[0].split():\n+        typ, messageparts = imapsession.fetch(msgId, '(RFC822)')\n+        if typ != 'OK':\n+            print 'Error fetching mail.'\n+            raise\n+\n+        emailbody = messageparts[0][1]\n+        mail = email.message_from_string(emailbody)\n+        mtime = datetime.strptime(mail['Date'][:-6], '%a, %d %b %Y %H:%M:%S')\n+\n+        if not isempty:\n+            # Accident folder is not empty, we only need the latest\n+            if datetime.now() - mtime < timedelta(hours=4):\n+                filefound = True\n+            else:\n+                continue\n+\n+        for part in mail.walk():\n+            if part.get_content_maintype() == 'multipart':\n+                continue\n+            if part.get('Content-Disposition') is None:\n+                continue\n+            filename = part.get_filename()\n+\n+            if bool(filename) and filename[-3:] == \"csv\":\n+                filename = 'UH-{0}_{1}-{2}.csv'.format(mtime.date(), mtime.hour, mtime.minute)\n+                filepath = os.path.join(detach_dir, filename)\n+                if os.path.isfile(filepath):\n+                    break\n+                total += 1\n+                print 'Currently loading: ' + filename\n+                sys.stdout.write(\"\\033[F\")","path":"importmail.py","position":96,"original_position":96,"commit_id":"fcf650db55d6ba6af8525ef74194643b8679c24c","original_commit_id":"fcf650db55d6ba6af8525ef74194643b8679c24c","user":{"login":"danielhers","id":263363,"avatar_url":"https://avatars.githubusercontent.com/u/263363?v=3","gravatar_id":"","url":"https://api.github.com/users/danielhers","html_url":"https://github.com/danielhers","followers_url":"https://api.github.com/users/danielhers/followers","following_url":"https://api.github.com/users/danielhers/following{/other_user}","gists_url":"https://api.github.com/users/danielhers/gists{/gist_id}","starred_url":"https://api.github.com/users/danielhers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danielhers/subscriptions","organizations_url":"https://api.github.com/users/danielhers/orgs","repos_url":"https://api.github.com/users/danielhers/repos","events_url":"https://api.github.com/users/danielhers/events{/privacy}","received_events_url":"https://api.github.com/users/danielhers/received_events","type":"User","site_admin":false},"body":"If a shorter filename is printed, the previous one's last character(s) are still shown. Maybe write as many space character as the previous filename's length before writing this filename.","created_at":"2015-09-22T10:30:59Z","updated_at":"2015-09-22T10:30:59Z","html_url":"https://github.com/hasadna/anyway/pull/435#discussion_r40071713","pull_request_url":"https://api.github.com/repos/hasadna/anyway/pulls/435","_links":{"self":{"href":"https://api.github.com/repos/hasadna/anyway/pulls/comments/40071713"},"html":{"href":"https://github.com/hasadna/anyway/pull/435#discussion_r40071713"},"pull_request":{"href":"https://api.github.com/repos/hasadna/anyway/pulls/435"}}},"pull_request":{"url":"https://api.github.com/repos/hasadna/anyway/pulls/435","id":45038626,"html_url":"https://github.com/hasadna/anyway/pull/435","diff_url":"https://github.com/hasadna/anyway/pull/435.diff","patch_url":"https://github.com/hasadna/anyway/pull/435.patch","issue_url":"https://api.github.com/repos/hasadna/anyway/issues/435","number":435,"state":"open","locked":false,"title":"United Hatzala data integration","user":{"login":"omerxx","id":12237955,"avatar_url":"https://avatars.githubusercontent.com/u/12237955?v=3","gravatar_id":"","url":"https://api.github.com/users/omerxx","html_url":"https://github.com/omerxx","followers_url":"https://api.github.com/users/omerxx/followers","following_url":"https://api.github.com/users/omerxx/following{/other_user}","gists_url":"https://api.github.com/users/omerxx/gists{/gist_id}","starred_url":"https://api.github.com/users/omerxx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omerxx/subscriptions","organizations_url":"https://api.github.com/users/omerxx/orgs","repos_url":"https://api.github.com/users/omerxx/repos","events_url":"https://api.github.com/users/omerxx/events{/privacy}","received_events_url":"https://api.github.com/users/omerxx/received_events","type":"User","site_admin":false},"body":"#427: Integrating United Hatzala data.\r\nThe DB population and csv parsing are both done in the new `united.py`\r\nI created a different marker for their accidents as they have much 'thinner' data.\r\nAccidents are only given in severities `קשה` and `קלה` so there are no fatal markers.\r\n\r\n* Update: Accidents are being imported using `importmail.py` which downloads the .csv files directly from gmail. the script can be called directly or automatically in `united.py`. If it finds `static/data/united/` empty it downloads all accidents history. If it's not, then only the last file is downloaded.\r\n\r\n@danielhers - Can this PR be deployed to a staging app with the latest accidents file?\r\n\r\nExample shot:\r\n![image](https://cloud.githubusercontent.com/assets/12237955/9875541/773dfa60-5bb7-11e5-87ba-286bf54b74f7.png)\r\n\r\n\r\n<!---\r\n@huboard:{\"order\":438.0,\"milestone_order\":435,\"custom_state\":\"\"}\n-->\r\n","created_at":"2015-09-15T11:40:09Z","updated_at":"2015-09-22T10:30:59Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6baed1cc5e2df97bb55808c2a1d41bb15dc2953b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/hasadna/anyway/pulls/435/commits","review_comments_url":"https://api.github.com/repos/hasadna/anyway/pulls/435/comments","review_comment_url":"https://api.github.com/repos/hasadna/anyway/pulls/comments{/number}","comments_url":"https://api.github.com/repos/hasadna/anyway/issues/435/comments","statuses_url":"https://api.github.com/repos/hasadna/anyway/statuses/fcf650db55d6ba6af8525ef74194643b8679c24c","head":{"label":"omerxx:united-data","ref":"united-data","sha":"fcf650db55d6ba6af8525ef74194643b8679c24c","user":{"login":"omerxx","id":12237955,"avatar_url":"https://avatars.githubusercontent.com/u/12237955?v=3","gravatar_id":"","url":"https://api.github.com/users/omerxx","html_url":"https://github.com/omerxx","followers_url":"https://api.github.com/users/omerxx/followers","following_url":"https://api.github.com/users/omerxx/following{/other_user}","gists_url":"https://api.github.com/users/omerxx/gists{/gist_id}","starred_url":"https://api.github.com/users/omerxx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omerxx/subscriptions","organizations_url":"https://api.github.com/users/omerxx/orgs","repos_url":"https://api.github.com/users/omerxx/repos","events_url":"https://api.github.com/users/omerxx/events{/privacy}","received_events_url":"https://api.github.com/users/omerxx/received_events","type":"User","site_admin":false},"repo":{"id":35717001,"name":"anyway","full_name":"omerxx/anyway","owner":{"login":"omerxx","id":12237955,"avatar_url":"https://avatars.githubusercontent.com/u/12237955?v=3","gravatar_id":"","url":"https://api.github.com/users/omerxx","html_url":"https://github.com/omerxx","followers_url":"https://api.github.com/users/omerxx/followers","following_url":"https://api.github.com/users/omerxx/following{/other_user}","gists_url":"https://api.github.com/users/omerxx/gists{/gist_id}","starred_url":"https://api.github.com/users/omerxx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omerxx/subscriptions","organizations_url":"https://api.github.com/users/omerxx/orgs","repos_url":"https://api.github.com/users/omerxx/repos","events_url":"https://api.github.com/users/omerxx/events{/privacy}","received_events_url":"https://api.github.com/users/omerxx/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/omerxx/anyway","description":"ANYWAY - Car accidents map","fork":true,"url":"https://api.github.com/repos/omerxx/anyway","forks_url":"https://api.github.com/repos/omerxx/anyway/forks","keys_url":"https://api.github.com/repos/omerxx/anyway/keys{/key_id}","collaborators_url":"https://api.github.com/repos/omerxx/anyway/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/omerxx/anyway/teams","hooks_url":"https://api.github.com/repos/omerxx/anyway/hooks","issue_events_url":"https://api.github.com/repos/omerxx/anyway/issues/events{/number}","events_url":"https://api.github.com/repos/omerxx/anyway/events","assignees_url":"https://api.github.com/repos/omerxx/anyway/assignees{/user}","branches_url":"https://api.github.com/repos/omerxx/anyway/branches{/branch}","tags_url":"https://api.github.com/repos/omerxx/anyway/tags","blobs_url":"https://api.github.com/repos/omerxx/anyway/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/omerxx/anyway/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/omerxx/anyway/git/refs{/sha}","trees_url":"https://api.github.com/repos/omerxx/anyway/git/trees{/sha}","statuses_url":"https://api.github.com/repos/omerxx/anyway/statuses/{sha}","languages_url":"https://api.github.com/repos/omerxx/anyway/languages","stargazers_url":"https://api.github.com/repos/omerxx/anyway/stargazers","contributors_url":"https://api.github.com/repos/omerxx/anyway/contributors","subscribers_url":"https://api.github.com/repos/omerxx/anyway/subscribers","subscription_url":"https://api.github.com/repos/omerxx/anyway/subscription","commits_url":"https://api.github.com/repos/omerxx/anyway/commits{/sha}","git_commits_url":"https://api.github.com/repos/omerxx/anyway/git/commits{/sha}","comments_url":"https://api.github.com/repos/omerxx/anyway/comments{/number}","issue_comment_url":"https://api.github.com/repos/omerxx/anyway/issues/comments{/number}","contents_url":"https://api.github.com/repos/omerxx/anyway/contents/{+path}","compare_url":"https://api.github.com/repos/omerxx/anyway/compare/{base}...{head}","merges_url":"https://api.github.com/repos/omerxx/anyway/merges","archive_url":"https://api.github.com/repos/omerxx/anyway/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/omerxx/anyway/downloads","issues_url":"https://api.github.com/repos/omerxx/anyway/issues{/number}","pulls_url":"https://api.github.com/repos/omerxx/anyway/pulls{/number}","milestones_url":"https://api.github.com/repos/omerxx/anyway/milestones{/number}","notifications_url":"https://api.github.com/repos/omerxx/anyway/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/omerxx/anyway/labels{/name}","releases_url":"https://api.github.com/repos/omerxx/anyway/releases{/id}","created_at":"2015-05-16T09:06:26Z","updated_at":"2015-06-03T20:42:52Z","pushed_at":"2015-09-22T09:12:55Z","git_url":"git://github.com/omerxx/anyway.git","ssh_url":"git@github.com:omerxx/anyway.git","clone_url":"https://github.com/omerxx/anyway.git","svn_url":"https://github.com/omerxx/anyway","homepage":"http://www.anyway.co.il","size":20959,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"hasadna:dev","ref":"dev","sha":"bf3b6577c4cf160fafed82006afa84c272efe760","user":{"login":"hasadna","id":503639,"avatar_url":"https://avatars.githubusercontent.com/u/503639?v=3","gravatar_id":"","url":"https://api.github.com/users/hasadna","html_url":"https://github.com/hasadna","followers_url":"https://api.github.com/users/hasadna/followers","following_url":"https://api.github.com/users/hasadna/following{/other_user}","gists_url":"https://api.github.com/users/hasadna/gists{/gist_id}","starred_url":"https://api.github.com/users/hasadna/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hasadna/subscriptions","organizations_url":"https://api.github.com/users/hasadna/orgs","repos_url":"https://api.github.com/users/hasadna/repos","events_url":"https://api.github.com/users/hasadna/events{/privacy}","received_events_url":"https://api.github.com/users/hasadna/received_events","type":"Organization","site_admin":false},"repo":{"id":15057515,"name":"anyway","full_name":"hasadna/anyway","owner":{"login":"hasadna","id":503639,"avatar_url":"https://avatars.githubusercontent.com/u/503639?v=3","gravatar_id":"","url":"https://api.github.com/users/hasadna","html_url":"https://github.com/hasadna","followers_url":"https://api.github.com/users/hasadna/followers","following_url":"https://api.github.com/users/hasadna/following{/other_user}","gists_url":"https://api.github.com/users/hasadna/gists{/gist_id}","starred_url":"https://api.github.com/users/hasadna/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hasadna/subscriptions","organizations_url":"https://api.github.com/users/hasadna/orgs","repos_url":"https://api.github.com/users/hasadna/repos","events_url":"https://api.github.com/users/hasadna/events{/privacy}","received_events_url":"https://api.github.com/users/hasadna/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/hasadna/anyway","description":"ANYWAY - Car accidents map","fork":true,"url":"https://api.github.com/repos/hasadna/anyway","forks_url":"https://api.github.com/repos/hasadna/anyway/forks","keys_url":"https://api.github.com/repos/hasadna/anyway/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hasadna/anyway/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hasadna/anyway/teams","hooks_url":"https://api.github.com/repos/hasadna/anyway/hooks","issue_events_url":"https://api.github.com/repos/hasadna/anyway/issues/events{/number}","events_url":"https://api.github.com/repos/hasadna/anyway/events","assignees_url":"https://api.github.com/repos/hasadna/anyway/assignees{/user}","branches_url":"https://api.github.com/repos/hasadna/anyway/branches{/branch}","tags_url":"https://api.github.com/repos/hasadna/anyway/tags","blobs_url":"https://api.github.com/repos/hasadna/anyway/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hasadna/anyway/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hasadna/anyway/git/refs{/sha}","trees_url":"https://api.github.com/repos/hasadna/anyway/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hasadna/anyway/statuses/{sha}","languages_url":"https://api.github.com/repos/hasadna/anyway/languages","stargazers_url":"https://api.github.com/repos/hasadna/anyway/stargazers","contributors_url":"https://api.github.com/repos/hasadna/anyway/contributors","subscribers_url":"https://api.github.com/repos/hasadna/anyway/subscribers","subscription_url":"https://api.github.com/repos/hasadna/anyway/subscription","commits_url":"https://api.github.com/repos/hasadna/anyway/commits{/sha}","git_commits_url":"https://api.github.com/repos/hasadna/anyway/git/commits{/sha}","comments_url":"https://api.github.com/repos/hasadna/anyway/comments{/number}","issue_comment_url":"https://api.github.com/repos/hasadna/anyway/issues/comments{/number}","contents_url":"https://api.github.com/repos/hasadna/anyway/contents/{+path}","compare_url":"https://api.github.com/repos/hasadna/anyway/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hasadna/anyway/merges","archive_url":"https://api.github.com/repos/hasadna/anyway/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hasadna/anyway/downloads","issues_url":"https://api.github.com/repos/hasadna/anyway/issues{/number}","pulls_url":"https://api.github.com/repos/hasadna/anyway/pulls{/number}","milestones_url":"https://api.github.com/repos/hasadna/anyway/milestones{/number}","notifications_url":"https://api.github.com/repos/hasadna/anyway/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hasadna/anyway/labels{/name}","releases_url":"https://api.github.com/repos/hasadna/anyway/releases{/id}","created_at":"2013-12-09T19:57:13Z","updated_at":"2015-07-13T18:54:21Z","pushed_at":"2015-09-22T09:12:56Z","git_url":"git://github.com/hasadna/anyway.git","ssh_url":"git@github.com:hasadna/anyway.git","clone_url":"https://github.com/hasadna/anyway.git","svn_url":"https://github.com/hasadna/anyway","homepage":"http://www.anyway.co.il","size":22839,"stargazers_count":12,"watchers_count":12,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":53,"mirror_url":null,"open_issues_count":60,"forks":53,"open_issues":60,"watchers":12,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/hasadna/anyway/pulls/435"},"html":{"href":"https://github.com/hasadna/anyway/pull/435"},"issue":{"href":"https://api.github.com/repos/hasadna/anyway/issues/435"},"comments":{"href":"https://api.github.com/repos/hasadna/anyway/issues/435/comments"},"review_comments":{"href":"https://api.github.com/repos/hasadna/anyway/pulls/435/comments"},"review_comment":{"href":"https://api.github.com/repos/hasadna/anyway/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/hasadna/anyway/pulls/435/commits"},"statuses":{"href":"https://api.github.com/repos/hasadna/anyway/statuses/fcf650db55d6ba6af8525ef74194643b8679c24c"}}}}