-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_api_db.py
121 lines (96 loc) · 3 KB
/
fetch_api_db.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python3
"""Module to fetch data from the extenal api"""
import requests
import creds
def get_urls():
"""get the courses urls"""
url = "https://udemy-course-scrapper-api.p.rapidapi.com/course-names/course-instructor/course-url"
headers = {
"X-RapidAPI-Key": creds.api_key,
"X-RapidAPI-Host": creds.api_host
}
response = requests.get(url, headers=headers)
urls_data = response.json()
urls = []
for data in urls_data.values():
for url in data.values():
urls.append(url)
return urls
def get_instructors():
"""get the get the courses instructors"""
url = "https://udemy-course-scrapper-api.p.rapidapi.com/course-names/course-instructor"
headers = {
"X-RapidAPI-Key": creds.api_key,
"X-RapidAPI-Host": creds.api_host
}
response = requests.get(url, headers=headers)
instructors_data = response.json()
instructors = []
for data in instructors_data.values():
for instructor in data.values():
instructors.append(instructor)
return instructors
def get_titles():
"""get the courses titles"""
url = "https://udemy-course-scrapper-api.p.rapidapi.com/course-names"
headers = {
"X-RapidAPI-Key": creds.api_key,
"X-RapidAPI-Host": creds.api_host
}
response = requests.get(url, headers=headers)
titles_data = response.json()
titles = []
for data in titles_data.values():
for title in data.values():
titles.append(title)
return titles
def assemble_data():
""""organise all courses details"""
titles = get_titles()
urls = get_urls()
instructors = get_instructors()
index = 0
courses = []
while index < len(titles):
courses.append({"title": titles[index],
"URL": urls[index],
"instructor": instructors[index]
})
index += 1
return courses
#courses_data = assemble_data()
#print(courses_data)
#fetch the datas in the rapid api to app db
#with engine.connect() as conn:
# for course in courses_data:
# title = course["title"]
# url = course["URL"]
# instructor = course["instructor"]
# counter = 1
#
# query = text("INSERT INTO courses (title, url, instructor) VALUES (:title, :url, :instructor)")
# try:
# conn.execute(query, {"title": title, "url": url, "instructor": instructor})
# print(f"Insert successful!", counter)
# except Exception as e:
# print(f"Error inserting data: {e}")
#
# counter += 1
#
# conn.commit()
#
# print("All Insert successful!")
#fetch data from app db
#with engine.connect() as conn:
# query = text("SELECT title, url, instructor FROM courses")
#
# result = conn.execute(query)
#
# rows = result.fetchall()
#
# for row in rows:
# title = row[0]
# url = row[1]
# instructor = row[2]
#
# print(f"Title: {title}, URL: {url}, Instructor: {instructor}")