Skip to content

Commit

Permalink
Updated imports
Browse files Browse the repository at this point in the history
- Changed the imports to match #36 point  5.
- Added typing to variables that were missing it
- Updated gitignore to ignore every pyc file
  • Loading branch information
Lukasaurus11 committed Feb 21, 2024
1 parent b1a4219 commit d9f09f7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ codecompasslib/Tests/.pytest_cache/
codecompasslib/Tests/functional/__pycache__/
codecompasslib/Tests/functional/.pytest_cache/
codecompasslib/PretrainedModels/
backend/__pycache__/
backend/__pycache__/
*.pyc
31 changes: 16 additions & 15 deletions codecompasslib/API/get_bulk_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from requests import Response, get
from requests.exceptions import HTTPError
from pandas import DataFrame
from codecompasslib.API.helper_functions import load_secret, get_repo_fields, save_to_csv

Expand All @@ -22,7 +23,7 @@ def get_users(user_amount: int = 100) -> (list, bool):
}

try:
response: requests.Response = requests.get(url, headers=HEADER, params=query_parameters, allow_redirects=False)
response: Response = get(url, headers=HEADER, params=query_parameters, allow_redirects=False)
response.raise_for_status()

users_data: list = []
Expand All @@ -32,7 +33,7 @@ def get_users(user_amount: int = 100) -> (list, bool):

return users_data, True

except requests.exceptions.HTTPError as err:
except HTTPError as err:
print(f"HTTP error occurred: {err}")
return [], False
except Exception as err:
Expand All @@ -48,7 +49,7 @@ def get_followers(username: str) -> (list, bool):
"""
url: str = f'https://api.github.com/users/{username}/followers'
try:
response: requests.Response = requests.get(url, headers=HEADER, allow_redirects=False)
response: Response = get(url, headers=HEADER, allow_redirects=False)
response.raise_for_status()

followers: list = []
Expand All @@ -58,7 +59,7 @@ def get_followers(username: str) -> (list, bool):

return followers, True

except requests.exceptions.HTTPError as err:
except HTTPError as err:
print(f"HTTP error occurred: {err}")
return [], False
except Exception as err:
Expand All @@ -74,7 +75,7 @@ def get_following(username: str) -> (list, bool):
"""
url: str = f'https://api.github.com/users/{username}/following'
try:
response: requests.Response = requests.get(url, headers=HEADER, allow_redirects=False)
response: Response = get(url, headers=HEADER, allow_redirects=False)
response.raise_for_status()

following: list = []
Expand All @@ -84,7 +85,7 @@ def get_following(username: str) -> (list, bool):

return following, True

except requests.exceptions.HTTPError as err:
except HTTPError as err:
print(f"HTTP error occurred: {err}")
return [], False
except Exception as err:
Expand All @@ -100,7 +101,7 @@ def get_user_repos(username: str) -> (list, bool):
"""
url: str = f'https://api.github.com/users/{username}/repos'
try:
response: requests.Response = requests.get(url, headers=HEADER, allow_redirects=False)
response: Response = get(url, headers=HEADER, allow_redirects=False)
response.raise_for_status()

repo_data: list = []
Expand All @@ -110,7 +111,7 @@ def get_user_repos(username: str) -> (list, bool):

return repo_data, True

except requests.exceptions.HTTPError as err:
except HTTPError as err:
print(f"HTTP error occurred: {err}")
return [], False
except Exception as err:
Expand Down Expand Up @@ -159,7 +160,7 @@ def get_misc_data(query_parameters: list = None) -> bool:
data_list: list = []
for query in query_list:
try:
response: requests.Response = requests.get(url, headers=HEADER, params=query, allow_redirects=False)
response: Response = get(url, headers=HEADER, params=query, allow_redirects=False)
response.raise_for_status()

repos_data: list = []
Expand All @@ -169,17 +170,17 @@ def get_misc_data(query_parameters: list = None) -> bool:
repos_data.append(repo_info)

data_list.append(repos_data)
except requests.exceptions.HTTPError as err:
except HTTPError as err:
print(f"HTTP error occurred: {err}")
return False
except Exception as err:
print(f"An error occurred: {err}")
return False

data_list: list = [item for sublist in data_list for item in sublist]
df = DataFrame(data_list)
df: DataFrame = DataFrame(data_list)
df.drop_duplicates(subset='id', keep='first', inplace=True)
save_to_csv(df, 'miscData.csv')
save_to_csv(df, 'original/miscData.csv')
return True


Expand Down Expand Up @@ -222,7 +223,7 @@ def get_bulk_data(user_amount: int = 100) -> bool:
print("An error occurred with getting users.")
return False

print("Amount of users: ", len(users))
print(f"Amount of users: {len(users)}")

if len(users) > 3000:
print("Too many users, limiting it to 3000")
Expand All @@ -239,7 +240,7 @@ def get_bulk_data(user_amount: int = 100) -> bool:

users_repos: list = [item for sublist in users_repos for item in sublist]

df = DataFrame(users_repos)
df: DataFrame = DataFrame(users_repos)
df.drop_duplicates(subset='id', keep='first', inplace=True)
save_to_csv(df, 'original/bulkDataNew.csv')
return True
4 changes: 2 additions & 2 deletions codecompasslib/API/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def save_to_csv(data: any, filename: str) -> None:
:param filename: The name of the file.
:return: Does not return anything.
"""
df = DataFrame(data)
df: DataFrame = DataFrame(data)
df.to_csv(Path(PARENT_PATH + '/Data/' + filename), index=False)


Expand Down Expand Up @@ -65,6 +65,6 @@ def load_secret() -> str:
:return: the token as a string.
"""
with open(OUTER_PATH + '/secrets/pat.json') as f:
token_data = load(f)
token_data: dict = load(f)
token: str = token_data['token']
return token
3 changes: 2 additions & 1 deletion tests/test_functional.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from codecompasslib.API.get_bulk_data import *
from codecompasslib.API.get_bulk_data import get_users, get_followers, get_following, get_user_repos, get_misc_data, \
get_bulk_data


def test_get_users(sample_size) -> None:
Expand Down

0 comments on commit d9f09f7

Please sign in to comment.