-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b1238ba
Showing
12 changed files
with
572 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Flask==3.1.0 | ||
Requests==2.32.3 | ||
Gunicorn |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.