From ec2c50bb03bdbaf60c44f11edf897c05ee9fe740 Mon Sep 17 00:00:00 2001 From: boomer Date: Wed, 2 Feb 2022 15:10:10 +0530 Subject: [PATCH 1/5] Added projects route --- .gitignore | 1 + src/routes/__init__.py | 3 +- src/routes/projects/projects.py | 68 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/routes/projects/projects.py 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/src/routes/__init__.py b/src/routes/__init__.py index 5f4480a..8c48a95 100644 --- a/src/routes/__init__.py +++ b/src/routes/__init__.py @@ -1,11 +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) @api_blueprint.route("/", methods=["GET"]) def get_data(): diff --git a/src/routes/projects/projects.py b/src/routes/projects/projects.py new file mode 100644 index 0000000..6a0cc6c --- /dev/null +++ b/src/routes/projects/projects.py @@ -0,0 +1,68 @@ +from flask import Blueprint , jsonify +from dotenv import load_dotenv +import requests +from html.parser import HTMLParser + + +load_dotenv() +projects = Blueprint("projects", __name__, url_prefix="/projects") + + +class ParseOSCrepo(HTMLParser): #The HTML parser for scraping img link from + token: str = None + + def handle_starttag(self, tag: str, attrs: str): + if self.token: + return + + if tag != "meta": + return + + token = None + for (index, (k, v)) in enumerate(attrs): + if k == "content": + token = v + + if all([k == "property", v == "og:image"]): + if token: + self.token = token + return + for (inner_index, (nk, nv)) in enumerate(attrs, start=index): + if nk == "content": + self.token = nv + return + +def imglink(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.route("/", methods=["GET"]) +def project_info(): + 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" : imglink(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'] Date: Wed, 2 Feb 2022 15:25:46 +0530 Subject: [PATCH 2/5] Added to documentation --- README.md | 1 + 1 file changed, 1 insertion(+) 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 From f275b7ea776861e359c64450c07f3d0e05729525 Mon Sep 17 00:00:00 2001 From: boomer Date: Wed, 2 Feb 2022 15:30:25 +0530 Subject: [PATCH 3/5] Limited the results to the 10 most popular repos --- src/routes/projects/projects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/projects/projects.py b/src/routes/projects/projects.py index 6a0cc6c..607bfc0 100644 --- a/src/routes/projects/projects.py +++ b/src/routes/projects/projects.py @@ -65,4 +65,4 @@ def project_info(): for i in results: #Removing stars from the results del i['stars'] - return jsonify(results) + return jsonify(results[0:10]) From 6bf04d959e8eb5aa79e13964843db4ba6c361868 Mon Sep 17 00:00:00 2001 From: vijay Date: Wed, 2 Feb 2022 16:31:46 +0530 Subject: [PATCH 4/5] Formatted with black --- src/routes/__init__.py | 3 +- src/routes/projects/projects.py | 67 ++++++++++++++++----------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/routes/__init__.py b/src/routes/__init__.py index 8c48a95..4e5a8f8 100644 --- a/src/routes/__init__.py +++ b/src/routes/__init__.py @@ -6,7 +6,8 @@ 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) +api_blueprint.register_blueprint(projects.projects_bp) + @api_blueprint.route("/", methods=["GET"]) def get_data(): diff --git a/src/routes/projects/projects.py b/src/routes/projects/projects.py index 607bfc0..bcb37cb 100644 --- a/src/routes/projects/projects.py +++ b/src/routes/projects/projects.py @@ -1,68 +1,67 @@ -from flask import Blueprint , jsonify +from flask import Blueprint, jsonify from dotenv import load_dotenv import requests from html.parser import HTMLParser - load_dotenv() -projects = Blueprint("projects", __name__, url_prefix="/projects") +projects_bp = Blueprint("projects_bp", __name__, url_prefix="/projects") -class ParseOSCrepo(HTMLParser): #The HTML parser for scraping img link from +class ParseOSCrepo(HTMLParser): # The HTML parser for scraping img link from token: str = None def handle_starttag(self, tag: str, attrs: str): if self.token: return - if tag != "meta": return - token = None - for (index, (k, v)) in enumerate(attrs): - if k == "content": - token = v - - if all([k == "property", v == "og:image"]): + 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, (nk, nv)) in enumerate(attrs, start=index): - if nk == "content": - self.token = nv + for (inner_index, (ni, nj)) in enumerate(attrs, start=index): + if ni == "content": + self.token = nj return -def imglink(github_repolink): #Web scraping the image link from github - req = requests.get(f'{github_repolink}') + +def imglink(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.route("/", methods=["GET"]) +@projects_bp.route("/", methods=["GET"]) def project_info(): results = [] - req = requests.get("https://api.github.com/users/Open-Source-Community-VIT-AP/repos") - jsonfile = req.json() + req = requests.get( + "https://api.github.com/users/Open-Source-Community-VIT-AP/repos" + ) + jsonfile = req.json() - for i in jsonfile: #Formatting the data + for i in jsonfile: # Formatting the data results.append( { - "stars": i["stargazers_count"], - "Name" : i['name'], - "Description" : i['description'], - "Image" : imglink(i['html_url']), - "Repository_link": i['html_url'], - "ssh" : i["ssh_url"] + "Stars": i["stargazers_count"], + "Name": i["name"], + "Description": i["description"], + "Image": imglink(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'] Date: Wed, 2 Feb 2022 16:47:51 +0530 Subject: [PATCH 5/5] Moved helper function to utils dir, disabled sort. --- src/routes/projects/projects.py | 35 +++++++-------------------------- src/utils/project_img.py | 23 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 28 deletions(-) create mode 100644 src/utils/project_img.py diff --git a/src/routes/projects/projects.py b/src/routes/projects/projects.py index bcb37cb..e91751d 100644 --- a/src/routes/projects/projects.py +++ b/src/routes/projects/projects.py @@ -1,35 +1,13 @@ -from flask import Blueprint, jsonify +from flask import Blueprint, current_app, jsonify from dotenv import load_dotenv +from src.utils.project_img import ParseOSCrepo import requests -from html.parser import HTMLParser load_dotenv() projects_bp = Blueprint("projects_bp", __name__, url_prefix="/projects") -class ParseOSCrepo(HTMLParser): # The HTML parser for scraping img link from - 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 - - -def imglink(github_repolink): # Web scraping the image link from github +def img_link(github_repolink): # Web scraping the image link from github req = requests.get(f"{github_repolink}") parseObj = ParseOSCrepo() parseObj.feed(req.text) @@ -38,6 +16,7 @@ def imglink(github_repolink): # Web scraping the image link from github @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" @@ -50,7 +29,7 @@ def project_info(): "Stars": i["stargazers_count"], "Name": i["name"], "Description": i["description"], - "Image": imglink(i["html_url"]), + "Image": img_link(i["html_url"]), "Repository_link": i["html_url"], "SSH": i["ssh_url"], } @@ -58,10 +37,10 @@ def project_info(): 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"]: + 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"] + 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