-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.py
79 lines (57 loc) · 1.97 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import json
import os
import requests
def get_skills_file():
return 'data/skills.json'
def get_organizations_file():
return 'data/organizations.json'
def get_badges_file(id):
return 'data/badges/{}.json'.format(
id
)
def get_search_terms_file():
return 'data/search-terms.json'
def get_items_by_search_term(search_term):
r = requests.get('https://www.credly.com/api/v1/global_search?q=' + search_term)
return r.json()['data']
def search_terms():
with open(get_search_terms_file(), 'r') as file:
return json.load(file)
def get_items_from_file(file_name):
if os.path.isfile(file_name):
# read json data from file
with open(file_name, 'r') as file:
return json.load(file)
else:
# create a dictionary if file does not exist
return {}
def set_items_from_file(file_name, items):
with open(file_name, "w") as outfile:
json.dump(items, outfile)
def crawl_search_terms(terms):
for search_term in terms:
organizations = get_items_from_file(get_organizations_file())
skills = get_items_from_file(get_skills_file())
items = get_items_by_search_term(search_term)
print(search_term, len(items))
for item in items:
if item['type'] == 'Organization':
item['logo'] = item['photo']['url']
del item["type"]
del item["photo"]
organizations[item['id']] = item
elif item['type'] == 'Skill':
skills[item['id']] = item
set_items_from_file(get_organizations_file(), organizations)
set_items_from_file(get_skills_file(), skills)
def get_badges(badges, url):
print(url)
try:
r = requests.get(url).json()
for badge in r["data"]:
badges[badge['id']] = badge
if r["metadata"]["next_page_url"]:
get_badges(badges, r["metadata"]["next_page_url"])
except:
print("Error")
return badges