Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added projects route #9

Merged
merged 5 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
env
\__pycache__
venv/
ven/
temp
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| 3 | [api/event/\<int:id>] | 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

Expand Down
2 changes: 2 additions & 0 deletions src/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -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"])
Expand Down
46 changes: 46 additions & 0 deletions src/routes/projects/projects.py
Original file line number Diff line number Diff line change
@@ -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://github.com/gitapi/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])
23 changes: 23 additions & 0 deletions src/utils/project_img.py
Original file line number Diff line number Diff line change
@@ -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