{"action":"created","comment":{"url":"https://api.github.com/repos/muniri92/build-scraper/pulls/comments/62580015","id":62580015,"diff_hunk":"@@ -0,0 +1,147 @@\n+import io\n+import sys\n+import requests\n+from bs4 import BeautifulSoup\n+\n+\n+INSPECTION_DOMAIN = 'http://info.kingcounty.gov'\n+INSPECTION_PATH = '/health/ehs/foodsafety/inspections/Results.aspx'\n+INSPECTION_PARAMS = {\n+    'Output': 'W',\n+    'Business_Name': '',\n+    'Business_Address': '',\n+    'Longitude': '',\n+    'Latitude': '',\n+    'City': 'Seattle',\n+    'Zip_Code': '98101',\n+    'Inspection_Type': 'All',\n+    'Inspection_Start': '',\n+    'Inspection_End': '',\n+    'Inspection_Closed_Business': 'A',\n+    'Violation_Points': '',\n+    'Violation_Red_Points': '',\n+    'Violation_Descr': '',\n+    'Fuzzy_Search': 'N',\n+    'Sort': 'B'\n+}\n+\n+\n+def get_inspection_page(**kwargs):\n+    url = INSPECTION_DOMAIN + INSPECTION_PATH\n+    params = INSPECTION_PARAMS.copy()\n+    for key, val in kwargs.items():\n+        if key in INSPECTION_PARAMS:\n+            params[key] = val\n+    resp = requests.get(url, params=params)\n+    resp.raise_for_status()  # <- This is a no-op if there is no HTTP error\n+    return resp.content, resp.encoding\n+\n+\n+def write_file(content, filename):\n+    # with open(filename, \"w\") as f:\n+        # f.write(content)\n+    file = open(filename, \"w\")\n+    file.write(str(content))\n+\n+\n+def load_inspection_page(name, **kwargs):\n+    content, encoding = get_inspection_page(**kwargs)\n+    write_file(content.decode(encoding), name)\n+    file = io.open(name, \"r\")\n+    return file\n+\n+\n+def parse_source(html):\n+    parsed = BeautifulSoup(html, 'html5lib')\n+    return parsed\n+\n+\n+def extract_data_listings(html):\n+    return html.findAll('div', {\"id\": lambda x: x and x.startswith('PR') and x.endswith(\"~\")})\n+\n+    # id_finder = re.compile(r'PR[\\d]+~')\n+    # return html.find_all('div', id=id_finder)\n+\n+\n+def has_two_tds(elem):\n+    is_tr = elem.name == 'tr'\n+    td_children = elem.find_all('td', recursive=False)\n+    has_two = len(td_children) == 2\n+    return is_tr and has_two\n+\n+\n+def clean_data(td):\n+    data = td.string\n+    try:\n+        return data.strip(\" \\n:-\")\n+    except AttributeError:\n+        return u\"\"\n+\n+\n+def extract_restaurant_metadata(elem):\n+    metadata_rows = elem.find('tbody').find_all(\n+        has_two_tds, recursive=False\n+    )\n+    rdata = {}\n+    current_label = ''\n+    for row in metadata_rows:\n+        key_cell, val_cell = row.find_all('td', recursive=False)\n+        new_label = clean_data(key_cell)\n+        current_label = new_label if new_label else current_label\n+        rdata.setdefault(current_label, []).append(clean_data(val_cell))\n+    return rdata\n+\n+\n+def is_inspection_row(elem):\n+    is_tr = elem.name == 'tr'\n+    if not is_tr:\n+        return False\n+    td_children = elem.find_all('td', recursive=False)\n+    has_four = len(td_children) == 4\n+    this_text = clean_data(td_children[0]).lower()\n+    contains_word = 'inspection' in this_text\n+    does_not_start = not this_text.startswith('inspection')\n+    return is_tr and has_four and contains_word and does_not_start\n+\n+\n+def extract_score_data(elem):\n+    inspection_rows = elem.find_all(is_inspection_row)\n+    samples = len(inspection_rows)\n+    total = high_score = average = 0\n+    for row in inspection_rows:\n+        strval = clean_data(row.find_all('td')[2])\n+        try:\n+            intval = int(strval)\n+        except (ValueError, TypeError):\n+            samples -= 1\n+        else:\n+            total += intval\n+            high_score = intval if intval > high_score else high_score\n+    if samples:\n+        average = total / float(samples)\n+    data = {\n+        u'Average Score': average,\n+        u'High Score': high_score,\n+        u'Total Inspections': samples\n+    }\n+    return data\n+\n+\n+if __name__ == '__main__':\n+    kwargs = {\n+        'Inspection_Start': '2/1/2013',\n+        'Inspection_End': '2/1/2015',\n+        'Zip_Code': '98109'\n+    }\n+    if len(sys.argv) > 1 and sys.argv[1] == 'test':\n+        html, encoding = load_inspection_page('inspection_page.html')\n+    else:\n+        html, encoding = get_inspection_page(**kwargs)\n+    doc = parse_source(html)\n+    write_file(doc, 'inspection_page.html')","path":"scraper.py","position":141,"original_position":141,"commit_id":"de98c09e4477e6968844f037be9437d640f70d6a","original_commit_id":"de98c09e4477e6968844f037be9437d640f70d6a","user":{"login":"cewing","id":417109,"avatar_url":"https://avatars.githubusercontent.com/u/417109?v=3","gravatar_id":"","url":"https://api.github.com/users/cewing","html_url":"https://github.com/cewing","followers_url":"https://api.github.com/users/cewing/followers","following_url":"https://api.github.com/users/cewing/following{/other_user}","gists_url":"https://api.github.com/users/cewing/gists{/gist_id}","starred_url":"https://api.github.com/users/cewing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cewing/subscriptions","organizations_url":"https://api.github.com/users/cewing/orgs","repos_url":"https://api.github.com/users/cewing/repos","events_url":"https://api.github.com/users/cewing/events{/privacy}","received_events_url":"https://api.github.com/users/cewing/received_events","type":"User","site_admin":false},"body":"You probably don't want to do this every time.  If you're testing and reading from `inspection_page.html` already, then there's certainly no reason to write back out to it.","created_at":"2016-05-09T21:55:22Z","updated_at":"2016-05-09T21:55:22Z","html_url":"https://github.com/muniri92/build-scraper/pull/1#discussion_r62580015","pull_request_url":"https://api.github.com/repos/muniri92/build-scraper/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/muniri92/build-scraper/pulls/comments/62580015"},"html":{"href":"https://github.com/muniri92/build-scraper/pull/1#discussion_r62580015"},"pull_request":{"href":"https://api.github.com/repos/muniri92/build-scraper/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/muniri92/build-scraper/pulls/1","id":69283466,"html_url":"https://github.com/muniri92/build-scraper/pull/1","diff_url":"https://github.com/muniri92/build-scraper/pull/1.diff","patch_url":"https://github.com/muniri92/build-scraper/pull/1.patch","issue_url":"https://api.github.com/repos/muniri92/build-scraper/issues/1","number":1,"state":"closed","locked":false,"title":"step 1 completed","user":{"login":"muniri92","id":16530816,"avatar_url":"https://avatars.githubusercontent.com/u/16530816?v=3","gravatar_id":"","url":"https://api.github.com/users/muniri92","html_url":"https://github.com/muniri92","followers_url":"https://api.github.com/users/muniri92/followers","following_url":"https://api.github.com/users/muniri92/following{/other_user}","gists_url":"https://api.github.com/users/muniri92/gists{/gist_id}","starred_url":"https://api.github.com/users/muniri92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muniri92/subscriptions","organizations_url":"https://api.github.com/users/muniri92/orgs","repos_url":"https://api.github.com/users/muniri92/repos","events_url":"https://api.github.com/users/muniri92/events{/privacy}","received_events_url":"https://api.github.com/users/muniri92/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-05-08T23:37:52Z","updated_at":"2016-05-09T21:55:22Z","closed_at":"2016-05-08T23:38:45Z","merged_at":"2016-05-08T23:38:45Z","merge_commit_sha":"004f0a4b9c32602871f81a1afc133882fb173725","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/muniri92/build-scraper/pulls/1/commits","review_comments_url":"https://api.github.com/repos/muniri92/build-scraper/pulls/1/comments","review_comment_url":"https://api.github.com/repos/muniri92/build-scraper/pulls/comments{/number}","comments_url":"https://api.github.com/repos/muniri92/build-scraper/issues/1/comments","statuses_url":"https://api.github.com/repos/muniri92/build-scraper/statuses/de98c09e4477e6968844f037be9437d640f70d6a","head":{"label":"muniri92:step_1","ref":"step_1","sha":"de98c09e4477e6968844f037be9437d640f70d6a","user":{"login":"muniri92","id":16530816,"avatar_url":"https://avatars.githubusercontent.com/u/16530816?v=3","gravatar_id":"","url":"https://api.github.com/users/muniri92","html_url":"https://github.com/muniri92","followers_url":"https://api.github.com/users/muniri92/followers","following_url":"https://api.github.com/users/muniri92/following{/other_user}","gists_url":"https://api.github.com/users/muniri92/gists{/gist_id}","starred_url":"https://api.github.com/users/muniri92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muniri92/subscriptions","organizations_url":"https://api.github.com/users/muniri92/orgs","repos_url":"https://api.github.com/users/muniri92/repos","events_url":"https://api.github.com/users/muniri92/events{/privacy}","received_events_url":"https://api.github.com/users/muniri92/received_events","type":"User","site_admin":false},"repo":{"id":58332976,"name":"build-scraper","full_name":"muniri92/build-scraper","owner":{"login":"muniri92","id":16530816,"avatar_url":"https://avatars.githubusercontent.com/u/16530816?v=3","gravatar_id":"","url":"https://api.github.com/users/muniri92","html_url":"https://github.com/muniri92","followers_url":"https://api.github.com/users/muniri92/followers","following_url":"https://api.github.com/users/muniri92/following{/other_user}","gists_url":"https://api.github.com/users/muniri92/gists{/gist_id}","starred_url":"https://api.github.com/users/muniri92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muniri92/subscriptions","organizations_url":"https://api.github.com/users/muniri92/orgs","repos_url":"https://api.github.com/users/muniri92/repos","events_url":"https://api.github.com/users/muniri92/events{/privacy}","received_events_url":"https://api.github.com/users/muniri92/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/muniri92/build-scraper","description":"","fork":false,"url":"https://api.github.com/repos/muniri92/build-scraper","forks_url":"https://api.github.com/repos/muniri92/build-scraper/forks","keys_url":"https://api.github.com/repos/muniri92/build-scraper/keys{/key_id}","collaborators_url":"https://api.github.com/repos/muniri92/build-scraper/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/muniri92/build-scraper/teams","hooks_url":"https://api.github.com/repos/muniri92/build-scraper/hooks","issue_events_url":"https://api.github.com/repos/muniri92/build-scraper/issues/events{/number}","events_url":"https://api.github.com/repos/muniri92/build-scraper/events","assignees_url":"https://api.github.com/repos/muniri92/build-scraper/assignees{/user}","branches_url":"https://api.github.com/repos/muniri92/build-scraper/branches{/branch}","tags_url":"https://api.github.com/repos/muniri92/build-scraper/tags","blobs_url":"https://api.github.com/repos/muniri92/build-scraper/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/muniri92/build-scraper/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/muniri92/build-scraper/git/refs{/sha}","trees_url":"https://api.github.com/repos/muniri92/build-scraper/git/trees{/sha}","statuses_url":"https://api.github.com/repos/muniri92/build-scraper/statuses/{sha}","languages_url":"https://api.github.com/repos/muniri92/build-scraper/languages","stargazers_url":"https://api.github.com/repos/muniri92/build-scraper/stargazers","contributors_url":"https://api.github.com/repos/muniri92/build-scraper/contributors","subscribers_url":"https://api.github.com/repos/muniri92/build-scraper/subscribers","subscription_url":"https://api.github.com/repos/muniri92/build-scraper/subscription","commits_url":"https://api.github.com/repos/muniri92/build-scraper/commits{/sha}","git_commits_url":"https://api.github.com/repos/muniri92/build-scraper/git/commits{/sha}","comments_url":"https://api.github.com/repos/muniri92/build-scraper/comments{/number}","issue_comment_url":"https://api.github.com/repos/muniri92/build-scraper/issues/comments{/number}","contents_url":"https://api.github.com/repos/muniri92/build-scraper/contents/{+path}","compare_url":"https://api.github.com/repos/muniri92/build-scraper/compare/{base}...{head}","merges_url":"https://api.github.com/repos/muniri92/build-scraper/merges","archive_url":"https://api.github.com/repos/muniri92/build-scraper/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/muniri92/build-scraper/downloads","issues_url":"https://api.github.com/repos/muniri92/build-scraper/issues{/number}","pulls_url":"https://api.github.com/repos/muniri92/build-scraper/pulls{/number}","milestones_url":"https://api.github.com/repos/muniri92/build-scraper/milestones{/number}","notifications_url":"https://api.github.com/repos/muniri92/build-scraper/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/muniri92/build-scraper/labels{/name}","releases_url":"https://api.github.com/repos/muniri92/build-scraper/releases{/id}","deployments_url":"https://api.github.com/repos/muniri92/build-scraper/deployments","created_at":"2016-05-08T21:49:31Z","updated_at":"2016-05-08T23:38:46Z","pushed_at":"2016-05-09T04:18:59Z","git_url":"git://github.com/muniri92/build-scraper.git","ssh_url":"git@github.com:muniri92/build-scraper.git","clone_url":"https://github.com/muniri92/build-scraper.git","svn_url":"https://github.com/muniri92/build-scraper","homepage":null,"size":266,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":true,"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":0,"default_branch":"master"}},"base":{"label":"muniri92:master","ref":"master","sha":"20e7a26f5707ec85833928c07c133c8d179a818e","user":{"login":"muniri92","id":16530816,"avatar_url":"https://avatars.githubusercontent.com/u/16530816?v=3","gravatar_id":"","url":"https://api.github.com/users/muniri92","html_url":"https://github.com/muniri92","followers_url":"https://api.github.com/users/muniri92/followers","following_url":"https://api.github.com/users/muniri92/following{/other_user}","gists_url":"https://api.github.com/users/muniri92/gists{/gist_id}","starred_url":"https://api.github.com/users/muniri92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muniri92/subscriptions","organizations_url":"https://api.github.com/users/muniri92/orgs","repos_url":"https://api.github.com/users/muniri92/repos","events_url":"https://api.github.com/users/muniri92/events{/privacy}","received_events_url":"https://api.github.com/users/muniri92/received_events","type":"User","site_admin":false},"repo":{"id":58332976,"name":"build-scraper","full_name":"muniri92/build-scraper","owner":{"login":"muniri92","id":16530816,"avatar_url":"https://avatars.githubusercontent.com/u/16530816?v=3","gravatar_id":"","url":"https://api.github.com/users/muniri92","html_url":"https://github.com/muniri92","followers_url":"https://api.github.com/users/muniri92/followers","following_url":"https://api.github.com/users/muniri92/following{/other_user}","gists_url":"https://api.github.com/users/muniri92/gists{/gist_id}","starred_url":"https://api.github.com/users/muniri92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muniri92/subscriptions","organizations_url":"https://api.github.com/users/muniri92/orgs","repos_url":"https://api.github.com/users/muniri92/repos","events_url":"https://api.github.com/users/muniri92/events{/privacy}","received_events_url":"https://api.github.com/users/muniri92/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/muniri92/build-scraper","description":"","fork":false,"url":"https://api.github.com/repos/muniri92/build-scraper","forks_url":"https://api.github.com/repos/muniri92/build-scraper/forks","keys_url":"https://api.github.com/repos/muniri92/build-scraper/keys{/key_id}","collaborators_url":"https://api.github.com/repos/muniri92/build-scraper/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/muniri92/build-scraper/teams","hooks_url":"https://api.github.com/repos/muniri92/build-scraper/hooks","issue_events_url":"https://api.github.com/repos/muniri92/build-scraper/issues/events{/number}","events_url":"https://api.github.com/repos/muniri92/build-scraper/events","assignees_url":"https://api.github.com/repos/muniri92/build-scraper/assignees{/user}","branches_url":"https://api.github.com/repos/muniri92/build-scraper/branches{/branch}","tags_url":"https://api.github.com/repos/muniri92/build-scraper/tags","blobs_url":"https://api.github.com/repos/muniri92/build-scraper/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/muniri92/build-scraper/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/muniri92/build-scraper/git/refs{/sha}","trees_url":"https://api.github.com/repos/muniri92/build-scraper/git/trees{/sha}","statuses_url":"https://api.github.com/repos/muniri92/build-scraper/statuses/{sha}","languages_url":"https://api.github.com/repos/muniri92/build-scraper/languages","stargazers_url":"https://api.github.com/repos/muniri92/build-scraper/stargazers","contributors_url":"https://api.github.com/repos/muniri92/build-scraper/contributors","subscribers_url":"https://api.github.com/repos/muniri92/build-scraper/subscribers","subscription_url":"https://api.github.com/repos/muniri92/build-scraper/subscription","commits_url":"https://api.github.com/repos/muniri92/build-scraper/commits{/sha}","git_commits_url":"https://api.github.com/repos/muniri92/build-scraper/git/commits{/sha}","comments_url":"https://api.github.com/repos/muniri92/build-scraper/comments{/number}","issue_comment_url":"https://api.github.com/repos/muniri92/build-scraper/issues/comments{/number}","contents_url":"https://api.github.com/repos/muniri92/build-scraper/contents/{+path}","compare_url":"https://api.github.com/repos/muniri92/build-scraper/compare/{base}...{head}","merges_url":"https://api.github.com/repos/muniri92/build-scraper/merges","archive_url":"https://api.github.com/repos/muniri92/build-scraper/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/muniri92/build-scraper/downloads","issues_url":"https://api.github.com/repos/muniri92/build-scraper/issues{/number}","pulls_url":"https://api.github.com/repos/muniri92/build-scraper/pulls{/number}","milestones_url":"https://api.github.com/repos/muniri92/build-scraper/milestones{/number}","notifications_url":"https://api.github.com/repos/muniri92/build-scraper/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/muniri92/build-scraper/labels{/name}","releases_url":"https://api.github.com/repos/muniri92/build-scraper/releases{/id}","deployments_url":"https://api.github.com/repos/muniri92/build-scraper/deployments","created_at":"2016-05-08T21:49:31Z","updated_at":"2016-05-08T23:38:46Z","pushed_at":"2016-05-09T04:18:59Z","git_url":"git://github.com/muniri92/build-scraper.git","ssh_url":"git@github.com:muniri92/build-scraper.git","clone_url":"https://github.com/muniri92/build-scraper.git","svn_url":"https://github.com/muniri92/build-scraper","homepage":null,"size":266,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":true,"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":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/muniri92/build-scraper/pulls/1"},"html":{"href":"https://github.com/muniri92/build-scraper/pull/1"},"issue":{"href":"https://api.github.com/repos/muniri92/build-scraper/issues/1"},"comments":{"href":"https://api.github.com/repos/muniri92/build-scraper/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/muniri92/build-scraper/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/muniri92/build-scraper/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/muniri92/build-scraper/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/muniri92/build-scraper/statuses/de98c09e4477e6968844f037be9437d640f70d6a"}}}}