Skip to content

Commit

Permalink
Github Repo Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
joswinemmanuel committed Jan 13, 2025
0 parents commit b1238ba
Show file tree
Hide file tree
Showing 12 changed files with 572 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h3>A GitHub popular Repository Filter webapp developed using Flask framework in Python.</h3>
32 changes: 32 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from flask import Flask, render_template, request

from repos.api import repos_with_most_stars
from repos.exceptions import GitHubApiException

app = Flask(__name__)

available_languages = ["Python", "JavaScript", "Ruby", "Java", "C", "C++", "TypeScript", "HTML", "CSS"]


@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'GET':
# Use the list of all the languages
selected_languages = available_languages
elif request.method == 'POST':
# Use the languages we selected in the request form
selected_languages = request.form.getlist("languages")

results = repos_with_most_stars(selected_languages)

return render_template(
'index.html',
selected_languages=selected_languages,
available_languages=available_languages,
results=results)


@app.errorhandler(GitHubApiException)
def handle_api_error(error):
return render_template('error.html', message=error)

7 changes: 7 additions & 0 deletions flask_commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FLASK_APP=hello.py flask run
for windows
$env:FLASK_APP = 'hello.py'; flask run

FLASK_APP=hello.py FLASK_ENV=development flask run
for windows
$env:FLASK_APP = 'hello.py'; $env:FLASK_ENV = 'development'; flask run
30 changes: 30 additions & 0 deletions repos/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from repos.exceptions import GitHubApiException
from repos.models import GitHubRepo
import requests
GITHUB_API_URL = "https://api.github.com/search/repositories"

def create_query(languages, min_stars):
"""
Create the query string for the GitHub search API,
based on the minimum amount of stars for a project, and
the provided programming languages.
"""
# Notice we are calling .strip() on each language, to clear it of leading
# and trailing whitespace
query = " ".join(f"language:{language.strip()}" for language in languages)
query = query + f" stars:>{min_stars}"
return query

def repos_with_most_stars(languages, min_stars=40000, sort="stars", order="desc"):
query = create_query(languages, min_stars)
parameters = {"q": query, "sort": sort, "order": order}
print(parameters)
response = requests.get(GITHUB_API_URL, params=parameters)

if response.status_code != 200:
raise GitHubApiException(response.status_code)

response_json = response.json()
items = response_json["items"]
return [GitHubRepo(item["name"], item["language"], item["stargazers_count"], item["html_url"]) for item in items]

9 changes: 9 additions & 0 deletions repos/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GitHubApiException(Exception):

def __init__(self, status_code):
if status_code == 403:
message = "Rate limit reached. Please wait a minute and try again."
else:
message = f"HTTP Status Code was: {status_code}."

super().__init__("A GitHub API Error Occurred: " + message)
13 changes: 13 additions & 0 deletions repos/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class GitHubRepo:

def __init__(self, name, language, num_stars, url_data):
self.name = name
self.language = language
self.num_stars = num_stars
self.url_data = url_data

def __str__(self):
return f"-> {self.name} is a {self.language} repo with {self.num_stars} stars and url {self.url_data}."

def __repr__(self):
return f'GitHubRepo({self.name}, {self.language}, {self.num_stars}, {self.url_data})'
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==3.1.0
Requests==2.32.3
Gunicorn
Binary file added static/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b1238ba

Please sign in to comment.