diff --git a/.gitignore b/.gitignore index f076304..00a6280 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ env \__pycache__ venv/ +ven/ temp \ No newline at end of file diff --git a/README.md b/README.md index d69a707..b682380 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ | 3 | [api/event/\] | https://osc-api.herokuapp.com/api/event/8 | GET data from a particular event (from Event ID). | | 4 | [api/event/latest] | https://osc-api.herokuapp.com/api/event/latest | GET data of the latest OSC event. | | 5 | [api/event/announcement] | https://osc-api.herokuapp.com/api/event/announcement?api_key= | POST to this endpoint to send a discord announcement | +| 6 | [api/projects/] | https://osc-api.herokuapp.com/api/projects | GET all public repos from the github organisation. | ## Contributing diff --git a/src/routes/__init__.py b/src/routes/__init__.py index 5f4480a..4e5a8f8 100644 --- a/src/routes/__init__.py +++ b/src/routes/__init__.py @@ -1,10 +1,12 @@ from flask import Blueprint from src.routes.events import event from src.routes.eb_details import eb +from src.routes.projects import projects api_blueprint = Blueprint("API", __name__, url_prefix="/api/v1/") api_blueprint.register_blueprint(event.event_bp) api_blueprint.register_blueprint(eb.eb_bp) +api_blueprint.register_blueprint(projects.projects_bp) @api_blueprint.route("/", methods=["GET"]) diff --git a/src/routes/projects/projects.py b/src/routes/projects/projects.py new file mode 100644 index 0000000..e91751d --- /dev/null +++ b/src/routes/projects/projects.py @@ -0,0 +1,46 @@ +from flask import Blueprint, current_app, jsonify +from dotenv import load_dotenv +from src.utils.project_img import ParseOSCrepo +import requests + +load_dotenv() +projects_bp = Blueprint("projects_bp", __name__, url_prefix="/projects") + + +def img_link(github_repolink): # Web scraping the image link from github + req = requests.get(f"{github_repolink}") + parseObj = ParseOSCrepo() + parseObj.feed(req.text) + return parseObj.token + + +@projects_bp.route("/", methods=["GET"]) +def project_info(): + current_app.config["JSON_SORT_KEYS"] = False + results = [] + req = requests.get( + "https://api.github.com/users/Open-Source-Community-VIT-AP/repos" + ) + jsonfile = req.json() + + for i in jsonfile: # Formatting the data + results.append( + { + "Stars": i["stargazers_count"], + "Name": i["name"], + "Description": i["description"], + "Image": img_link(i["html_url"]), + "Repository_link": i["html_url"], + "SSH": i["ssh_url"], + } + ) + + for i in range(len(results)): # Sorting according to stars + for j in range(len(results) - 1): + if results[j]["Stars"] < results[j + 1]["Stars"]: + results[j], results[j + 1] = results[j + 1], results[j] + + for i in results: # Removing stars from the results + del i["Stars"] + + return jsonify(results[0:10]) diff --git a/src/utils/project_img.py b/src/utils/project_img.py new file mode 100644 index 0000000..d102482 --- /dev/null +++ b/src/utils/project_img.py @@ -0,0 +1,23 @@ +from html.parser import HTMLParser + +# HTML parser for scraping image link from GitHub repository +class ParseOSCrepo(HTMLParser): + token: str = None + + def handle_starttag(self, tag: str, attrs: str): + if self.token: + return + if tag != "meta": + return + token = None + for (index, (i, j)) in enumerate(attrs): + if i == "content": + token = j + if all([i == "property", j == "og:image"]): + if token: + self.token = token + return + for (inner_index, (ni, nj)) in enumerate(attrs, start=index): + if ni == "content": + self.token = nj + return