server/sbin/hypothesis_feed.py
author ymh <ymh.work@gmail.com>
Mon, 17 Sep 2018 01:35:46 +0200
changeset 4 df751568fda6
parent 0 5f4fcbc80b37
permissions -rw-r--r--
Add dashboardId for calculating discussionId
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
#!/usr/bin/env python
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
# coding: utf-8
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import argparse
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import json
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import requests
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
def retrieve_group_annotations(args):
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    url =  args.url
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    if url is None:
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
        raise argparse.ArgumentTypeError('URL must be provided')
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    ids = []
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    params = {}
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    for config_tag in ['group', 'user', 'tag', 'limit']:
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        config_val = vars(args).get(config_tag)
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        if config_val is not None:
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
            params[config_tag] = config_val
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    headers = {}
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    token = args.token
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    if token is not None:
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        headers["Authorization"] = "Bearer " + token
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    res = requests.get(url, params = params, headers = headers)
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    print(res.json()["total"])
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    return res.json()
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
def save(data, path):
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    with open(path, 'w') as outfile:
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        json.dump(data, outfile, indent=4, separators=(',', ': '))
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
if __name__ == "__main__":
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    parser = argparse.ArgumentParser(description='Import annotations.')
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    parser.add_argument("url", type=str, help="the search url")
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    parser.add_argument("--token", "-T", type=str, help="the access token")
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    parser.add_argument("--group", "-g", type=str, help="filter by group")
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    parser.add_argument("--user", "-u", type=str, help="filter by user")
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    parser.add_argument("--tag", "-t", type=str, help="filter by tag")
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    parser.add_argument("--limit", "-l", type=int, default=200, help="annoations limit")
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    args = parser.parse_args()
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    ids = retrieve_group_annotations(args)
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    save(ids, "../assets/annotations.json")
5f4fcbc80b37 Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56