|
| 1 | +import csv, json, logging, math, requests |
| 2 | + |
| 3 | +from model.cls_repo import Repo |
| 4 | + |
| 5 | +logging.basicConfig(level=logging.INFO) |
| 6 | + |
| 7 | +def get_url(url): |
| 8 | +headers = {'Content-Type': 'application/json' } |
| 9 | +logging.info(url) |
| 10 | +return requests.get(url, headers=headers, verify=False) |
| 11 | + |
| 12 | +def to_json(url): |
| 13 | +response = get_url(url) |
| 14 | +logging.info("to json "+url) |
| 15 | +return json.loads(response.text) |
| 16 | + |
| 17 | +def get_count(): |
| 18 | +url = "https://api..com/users/bearddan2000" |
| 19 | +json_object = to_json(url) |
| 20 | +logging.info("get count") |
| 21 | +return json_object['public_repos'] |
| 22 | + |
| 23 | +def get_repo_by_page(page: int): |
| 24 | +lst = [] |
| 25 | +url = f"https://api..com/users/bearddan2000/repos?per_page=100&page={page}" |
| 26 | +json_object = to_json(url) |
| 27 | +for item in json_object: |
| 28 | +name = item['name'] |
| 29 | +try: |
| 30 | +if len(name.split("-")) > 1: |
| 31 | +desc=item['description'] |
| 32 | +topics=item['topics'] |
| 33 | +repo = Repo(name,desc,topics) |
| 34 | +lst.append(repo) |
| 35 | +logging.info(repo) |
| 36 | +except: |
| 37 | +pass |
| 38 | +return lst |
| 39 | + |
| 40 | +def repo_to_csv(rows: list): |
| 41 | +# field names |
| 42 | +fields = ['Name', 'Desc', 'Topics'] |
| 43 | +logging.info(fields) |
| 44 | +with open('/app/Repo.csv', 'w') as f: |
| 45 | + |
| 46 | +# using csv.writer method from CSV package |
| 47 | +write = csv.writer(f) |
| 48 | + |
| 49 | +write.writerow(fields) |
| 50 | +write.writerows(rows) |
| 51 | + |
| 52 | +def main(): |
| 53 | +print("hello world") |
| 54 | +repo = [] |
| 55 | +total_by_hundred: int = get_count()/100 |
| 56 | +total_rounded: int = math.ceil(total_by_hundred) |
| 57 | +for page in range(total_rounded): |
| 58 | +collection = get_repo_by_page(page+1) |
| 59 | +if collection is not None: |
| 60 | +repo.append(collection) |
| 61 | +repo_to_csv(repo) |
| 62 | + |
| 63 | +main() |
| 64 | + |
| 65 | +def test_get_count_get_url(): |
| 66 | +url = "https://api..com/users/bearddan2000" |
| 67 | +assert get_url(url).status_code == 200 |
| 68 | + |
| 69 | +def test_get_count_to_json_public_repos(): |
| 70 | +url = "https://api..com/users/bearddan2000" |
| 71 | +assert to_json(url).has_key('public_repos') == True |
| 72 | + |
| 73 | +def test_get_count(): |
| 74 | +assert get_count() > 0 |
| 75 | + |
| 76 | +def test_get_repo_get_url(): |
| 77 | +url = "https://api..com/users/bearddan2000/repos?per_page=100&page=1" |
| 78 | +assert get_url(url).status_code == 200 |
| 79 | + |
| 80 | +def test_get_repo_to_json_name(): |
| 81 | +url = "https://api..com/users/bearddan2000/repos?per_page=100&page=1" |
| 82 | +assert to_json(url).has_key('name') == True |
| 83 | + |
| 84 | +def test_get_repo_to_json_description(): |
| 85 | +url = "https://api..com/users/bearddan2000/repos?per_page=100&page=1" |
| 86 | +assert to_json(url).has_key('description') == True |
| 87 | + |
| 88 | +def test_get_repo_to_json_topics(): |
| 89 | +url = "https://api..com/users/bearddan2000/repos?per_page=100&page=1" |
| 90 | +assert to_json(url).has_key('topics') == True |
0 commit comments