Spaces:
Sleeping
Sleeping
Commit ·
95e759d
0
Parent(s):
major push: add frontend + backend
Browse files- main.py +52 -0
- requirements.txt +5 -0
main.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import bs4
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
from flask import Flask
|
| 5 |
+
from flask_cors import CORS
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
+
|
| 10 |
+
def fetch_github_page(**kwargs):
|
| 11 |
+
try:
|
| 12 |
+
url = 'https://github.com/topics/hacktoberfest?l=python'
|
| 13 |
+
response = requests.get(url)
|
| 14 |
+
response.raise_for_status()
|
| 15 |
+
return response.content
|
| 16 |
+
except requests.RequestException as e:
|
| 17 |
+
return f"Error fetching data: {e}"
|
| 18 |
+
|
| 19 |
+
def parse_repositories(page_content):
|
| 20 |
+
soup = bs4.BeautifulSoup(page_content, 'html.parser')
|
| 21 |
+
articles = soup.find_all("article")
|
| 22 |
+
repositories = []
|
| 23 |
+
|
| 24 |
+
for article in articles:
|
| 25 |
+
repo_info = [a_tag.text.strip() for a_tag in article.find_all("a") if a_tag.text.strip()]
|
| 26 |
+
|
| 27 |
+
if len(repo_info) >= 3 and "Star" in repo_info[2]:
|
| 28 |
+
owner = repo_info[0]
|
| 29 |
+
repo_name = repo_info[1]
|
| 30 |
+
stars = repo_info[2]
|
| 31 |
+
repo_link = f"https://github.com/{owner}/{repo_name}"
|
| 32 |
+
|
| 33 |
+
repositories.append({
|
| 34 |
+
"repo": repo_name,
|
| 35 |
+
"owner": owner,
|
| 36 |
+
"stars": stars,
|
| 37 |
+
"link": repo_link
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
return repositories
|
| 41 |
+
|
| 42 |
+
@app.route('/')
|
| 43 |
+
def get_repos():
|
| 44 |
+
page_content = fetch_github_page()
|
| 45 |
+
if isinstance(page_content, str) and page_content.startswith("Error"):
|
| 46 |
+
return page_content
|
| 47 |
+
|
| 48 |
+
repositories = parse_repositories(page_content)
|
| 49 |
+
return json.dumps(repositories, indent=4)
|
| 50 |
+
|
| 51 |
+
if __name__ == '__main__':
|
| 52 |
+
app.run(port=8000, debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==3.0.3
|
| 2 |
+
beautifulsoup4==4.12.3
|
| 3 |
+
requests==2.32.3
|
| 4 |
+
html5lib==1.1
|
| 5 |
+
flask-cors==5.0.0
|