-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
193 lines (131 loc) · 6.4 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
URLS = ["https://learnweb3.io/minis/", "https://learnweb3.io/lessons/", "https://learnweb3.io/degrees/"]
LEADERBOARD_URL = 'https://learnweb3.io/leaderboard/'
PREFIX_URL = "https://learnweb3.io"
minis_count = 0
minis_url = list()
minis_title = list()
lessons_count = 0
lessons_url = list()
lessons_title = list()
degrees_count = 0
degrees_url = list()
degrees_title = list()
def get_html(url):
return requests.get(url, timeout=None).text
def get_resources(url, course_type):
urls = list()
titles = list()
date_added = list()
try:
source_code = get_html(url)
soup = BeautifulSoup(source_code, 'html.parser')
whole_section = soup.find_all('section')
courses = BeautifulSoup(str(whole_section), 'html.parser').find_all('a')
for course in courses:
if course_type in course['href']:
urls.append(course['href'])
count = len(urls)
if course_type == 'degrees':
course_titles = soup.select("h3.flex")
else:
course_titles = soup.select("h3.flex.justify-between.text-xl.font-medium")
for course_title in course_titles:
titles.append(course_title.get_text())
dates = soup.select('span.text-xs.text-gray-700')
for d in dates:
date_added.append(d.get_text())
return count, titles, urls, date_added
except Exception as e:
print("Error:", e)
# For getting the top 5 people in the leaderboard
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-notifications")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
driver.get(LEADERBOARD_URL)
usernames = list()
github_links = list()
for i in range(1,6):
username = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, f'//*[@id="__next"]/div/div[1]/div[1]/div/main/div/div/ul/li[{i}]/a/div/div[1]/div[3]/div/p'))
)
usernames.append(username.text)
profile_links = [f'https://learnweb3.io/u/{username}' for username in usernames]
# print(profile_links)
for link in profile_links:
driver.get(link)
try:
github = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="__next"]/div/div[1]/div[1]/div/div[4]/div/div[1]/div[2]/div/a[1]'))
)
github_links.append(github.get_attribute('href'))
except:
github_links.append('')
driver.close()
top_5_leaderboard = list()
for i in range(5):
top_5_leaderboard.append(f"**{i+1}. [{usernames[i]}]({github_links[i]})**\n\n")
# For all the different resources like minis, lessons and degrees
minis_count, minis_titles, minis_urls, minis_dates = get_resources(URLS[0], "minis")
lessons_count, lessons_titles, lessons_urls, lessons_dates = get_resources(URLS[1], "lessons")
degrees_count, degrees_titles, degrees_urls, degrees_dates = get_resources(URLS[2], "degrees")
minis_urls = [PREFIX_URL + url for url in minis_urls]
lessons_urls = [PREFIX_URL + url for url in lessons_urls]
degrees_urls = [PREFIX_URL + url for url in degrees_urls]
total_count = minis_count + lessons_count + degrees_count
heading = "# LearnWeb3 Platform Resource Updates \n"
description = f"This repository scraps all the resources (Minis, Lessons and Degrees) available on [LearnWeb3]({PREFIX_URL}) platform every day and updates the README file with the total number of resources, the first 5 resources available from each category, and the top 5 people in the leaderboard. \n"
total_resources_heading = f"#### **Total Number of Resources on LearnWeb3: {total_count}** \n"
# for minis content
recent_minis_titles = minis_titles[:5]
recent_minis_urls = minis_urls[:5]
recent_minis_dates = minis_dates[:5]
recent_minis = list()
for i in range(5):
recent_minis.append(f"**{i+1}. [{recent_minis_titles[i]}]({recent_minis_urls[i]})** | Date Added: {recent_minis_dates[i]} \n\n")
minis_heading = "## Minis on LearnWeb3 \n"
minis_description = f"Total Number of Minis available: {minis_count} \n #### First 5 Minis \n"
# for lessons content
recent_lessons_titles = lessons_titles[:5]
recent_lessons_urls = lessons_urls[:5]
recent_lessons_dates = lessons_dates[:5]
recent_lessons = list()
for i in range(5):
recent_lessons.append(f"**{i+1}. [{recent_lessons_titles[i]}]({recent_lessons_urls[i]})** | Date Added: {recent_lessons_dates[i]} \n\n")
lessons_heading = "## Lessons on LearnWeb3 \n"
lessons_description = f"Total Number of Lessons available: {lessons_count} \n #### First 5 Lessons \n"
# for degrees content
recent_degrees = list()
if degrees_count > 5:
recent_degrees_titles = degrees_titles[:5]
recent_degrees_urls = degrees_urls[:5]
recent_degrees_dates = degrees_dates[:5]
for i in range(5):
recent_degrees.append(f"**{i+1}. [{recent_degrees_titles[i]}]({recent_degrees_urls[i]})** | Date Added: {recent_degrees_dates[i]} \n\n")
else:
for i in range(degrees_count):
recent_degrees.append(f"**{i+1}. [{degrees_titles[i]}]({degrees_urls[i]})** | Date Added: {degrees_dates[i]} \n\n")
degrees_heading = "## Degrees on LearnWeb3 \n"
if degrees_count > 5:
degrees_description = f"Total Number of Degrees available: {degrees_count} \n #### First 5 Degrees \n"
else:
degrees_description = f"Total Number of Degrees available: {degrees_count} \n #### All Degrees \n"
leaderboard_heading = "## Leaderboard (Top 5) \n"
# full README content
README_CONTENT = heading + description + total_resources_heading + minis_heading + minis_description + "".join(recent_minis) + lessons_heading + lessons_description + "".join(recent_lessons) + degrees_heading + degrees_description + "".join(recent_degrees) + leaderboard_heading + "".join(top_5_leaderboard)
with open('README.md', 'w') as readme:
readme.write(README_CONTENT)