Skip to content

Commit 2dd3dea

Browse files
authored
Merge pull request #9 from Open-Source-Community-VIT-AP/project
Completed setting up a route for OSC projects
2 parents 8f6206b + 04df080 commit 2dd3dea

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
env
44
\__pycache__
55
venv/
6+
ven/
67
temp

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| 3 | [api/event/\<int:id>] | https://osc-api.herokuapp.com/api/event/8 | GET data from a particular event (from Event ID). |
1616
| 4 | [api/event/latest] | https://osc-api.herokuapp.com/api/event/latest | GET data of the latest OSC event. |
1717
| 5 | [api/event/announcement] | https://osc-api.herokuapp.com/api/event/announcement?api_key= | POST to this endpoint to send a discord announcement |
18+
| 6 | [api/projects/] | https://osc-api.herokuapp.com/api/projects | GET all public repos from the github organisation. |
1819

1920
## Contributing
2021

src/routes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from flask import Blueprint
22
from src.routes.events import event
33
from src.routes.eb_details import eb
4+
from src.routes.projects import projects
45

56
api_blueprint = Blueprint("API", __name__, url_prefix="/api/v1/")
67
api_blueprint.register_blueprint(event.event_bp)
78
api_blueprint.register_blueprint(eb.eb_bp)
9+
api_blueprint.register_blueprint(projects.projects_bp)
810

911

1012
@api_blueprint.route("/", methods=["GET"])

src/routes/projects/projects.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from flask import Blueprint, current_app, jsonify
2+
from dotenv import load_dotenv
3+
from src.utils.project_img import ParseOSCrepo
4+
import requests
5+
6+
load_dotenv()
7+
projects_bp = Blueprint("projects_bp", __name__, url_prefix="/projects")
8+
9+
10+
def img_link(github_repolink): # Web scraping the image link from github
11+
req = requests.get(f"{github_repolink}")
12+
parseObj = ParseOSCrepo()
13+
parseObj.feed(req.text)
14+
return parseObj.token
15+
16+
17+
@projects_bp.route("/", methods=["GET"])
18+
def project_info():
19+
current_app.config["JSON_SORT_KEYS"] = False
20+
results = []
21+
req = requests.get(
22+
"https://api.github.com/users/Open-Source-Community-VIT-AP/repos"
23+
)
24+
jsonfile = req.json()
25+
26+
for i in jsonfile: # Formatting the data
27+
results.append(
28+
{
29+
"Stars": i["stargazers_count"],
30+
"Name": i["name"],
31+
"Description": i["description"],
32+
"Image": img_link(i["html_url"]),
33+
"Repository_link": i["html_url"],
34+
"SSH": i["ssh_url"],
35+
}
36+
)
37+
38+
for i in range(len(results)): # Sorting according to stars
39+
for j in range(len(results) - 1):
40+
if results[j]["Stars"] < results[j + 1]["Stars"]:
41+
results[j], results[j + 1] = results[j + 1], results[j]
42+
43+
for i in results: # Removing stars from the results
44+
del i["Stars"]
45+
46+
return jsonify(results[0:10])

src/utils/project_img.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from html.parser import HTMLParser
2+
3+
# HTML parser for scraping image link from GitHub repository
4+
class ParseOSCrepo(HTMLParser):
5+
token: str = None
6+
7+
def handle_starttag(self, tag: str, attrs: str):
8+
if self.token:
9+
return
10+
if tag != "meta":
11+
return
12+
token = None
13+
for (index, (i, j)) in enumerate(attrs):
14+
if i == "content":
15+
token = j
16+
if all([i == "property", j == "og:image"]):
17+
if token:
18+
self.token = token
19+
return
20+
for (inner_index, (ni, nj)) in enumerate(attrs, start=index):
21+
if ni == "content":
22+
self.token = nj
23+
return

0 commit comments

Comments
 (0)