Skip to content

Commit

Permalink
Merge pull request #47 from bellingcat/dev
Browse files Browse the repository at this point in the history
dev => 2.4.2.0
  • Loading branch information
rly0nheart committed Nov 25, 2023
2 parents dd7f62b + 417ff55 commit 9e7668e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 57 deletions.
12 changes: 10 additions & 2 deletions knewkarma/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import os

OPERATION_MODES: list = ["user", "subreddit", "post", "posts", "search", "quit"]
DATA_SORT_CRITERION: list = ["all", "controversial", "new", "top", "best", "hot", "rising"]
DATA_SORT_CRITERION: list = [
"all",
"controversial",
"new",
"top",
"best",
"hot",
"rising",
]
POST_LISTINGS: list = ["all", "best", "controversial", "popular", "rising"]

# Construct path to the program's directory
Expand All @@ -16,7 +24,7 @@

__author__: str = "Richard Mwewa"
__about__: str = "https://about.me/rly0nheart"
__version__: str = "2.4.1.0"
__version__: str = "2.4.2.0"
__pypi_project_endpoint__: str = "https://pypi.org/pypi/knewkarma/json"
__description__: str = """
# Knew Karma
Expand Down
125 changes: 71 additions & 54 deletions knewkarma/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ def get_data(endpoint: str) -> Union[dict, list]:
try:
with requests.Session() as session:
with session.get(
url=endpoint,
headers={
"User-Agent": f"Knew-Karma/{__version__} "
f"(Python {python_version}; +https://about.me/rly0nheart)"
},
url=endpoint,
headers={
"User-Agent": f"Knew-Karma/{__version__} "
f"(Python {python_version}; +https://about.me/rly0nheart)"
},
) as response:
if response.status_code == 200:
return response.json()
else:
error_message: dict = response.json()
log.error(f"An API error occurred: {error_message}")
log.error(f"An API error occurred: {response.json()}")
return {}
except requests.exceptions.RequestException as error:
log.error(f"An HTTP error occurred: {error}")
Expand All @@ -47,7 +46,7 @@ def get_data(endpoint: str) -> Union[dict, list]:

@staticmethod
def validate_data(
data: Union[dict, list], valid_key: str = None
data: Union[dict, list], valid_key: str = None
) -> Union[dict, list]:
"""
Validates the input data. If it's a dictionary and a valid_key is provided,
Expand Down Expand Up @@ -97,51 +96,69 @@ def get_updates(self):

remote_version: str = release.get("version")
# Splitting the version strings into components
remote_parts = remote_version.split('.')
local_parts = __version__.split('.')
remote_parts: list = remote_version.split(".")
local_parts: list = __version__.split(".")

update_message: str = ""
notification_timeout: int = 0
icon_file: str = "icon.ico" if os.name == "nt" else "icon.png"

# Check for differences in version parts
if remote_parts[0] != local_parts[0]:
update_message = (f"MAJOR update ({remote_version}) available."
f" It might introduce significant changes.")
update_message = (
f"MAJOR update ({remote_version}) available."
f" It might introduce significant changes."
)
notification_timeout = 60
elif remote_parts[1] != local_parts[1]:
update_message = (f"MINOR update ({remote_version}) available."
f" Includes small feature changes/improvements.")
update_message = (
f"MINOR update ({remote_version}) available."
f" Includes small feature changes/improvements."
)
notification_timeout = 50
elif remote_parts[2] != local_parts[2]:
update_message = (f"PATCH update ({remote_version}) available."
f" Generally for bug fixes and small tweaks.")
update_message = (
f"PATCH update ({remote_version}) available."
f" Generally for bug fixes and small tweaks."
)
notification_timeout = 30
elif len(remote_parts) > 3 and len(local_parts) > 3 and remote_parts[3] != local_parts[3]:
update_message = (f"BUILD update ({remote_version}) available."
f" Might be for specific builds or special versions.")
elif (
len(remote_parts) > 3
and len(local_parts) > 3
and remote_parts[3] != local_parts[3]
):
update_message = (
f"BUILD update ({remote_version}) available."
f" Might be for specific builds or special versions."
)
notification_timeout = 15

try:
# Catch and ignore all warnings (specific warning is at:
# https://github.com/kivy/plyer/blob/
# 8c0e11ff2e356ea677e96b0d7907d000c8f4bbd0/plyer/platforms/linux/notification.py#L99C8-L99C8)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
if update_message:
try:
# Catch and ignore all warnings (specific warning is at:
# https://github.com/kivy/plyer/blob/
# 8c0e11ff2e356ea677e96b0d7907d000c8f4bbd0/plyer/platforms/linux/notification.py#L99C8-L99C8)
with warnings.catch_warnings():
warnings.simplefilter("ignore")

# Notify user about the new release.
notification.notify(
update_message,
app_icon=f"{os.path.join(CURRENT_FILE_DIRECTORY, 'icons', icon_file)}",
timeout=notification_timeout,
)
except NotImplementedError: # Gets raised on systems that do not have a desktop environment
log.info(update_message)
# Notify user about the new release.
notification.notify(
title="Knew Karma",
message=update_message,
app_icon=os.path.join(
CURRENT_FILE_DIRECTORY, "icons", icon_file
),
timeout=notification_timeout,
)
except (
NotImplementedError
): # Gets raised on systems that do not have a desktop environment
log.info(update_message)

def get_profile(
self,
profile_source: str,
profile_type: str = Literal["user_profile", "subreddit_profile"],
self,
profile_source: str,
profile_type: str = Literal["user_profile", "subreddit_profile"],
) -> dict:
"""
Retrieves profile data from a specified source.
Expand Down Expand Up @@ -170,18 +187,18 @@ def get_profile(
return self.validate_data(data=profile.get("data", {}), valid_key="created_utc")

def get_posts(
self,
posts_sort_criterion: str,
posts_limit: int,
posts_type: str = Literal[
"user_posts",
"user_comments",
"subreddit_posts",
"search_posts",
"listing_posts",
"front_page_posts",
],
posts_source: str = None,
self,
posts_sort_criterion: str,
posts_limit: int,
posts_type: str = Literal[
"user_posts",
"user_comments",
"subreddit_posts",
"search_posts",
"listing_posts",
"front_page_posts",
],
posts_source: str = None,
) -> list:
"""
Retrieves posts from a specified source.
Expand Down Expand Up @@ -234,11 +251,11 @@ def get_posts(
return self.validate_data(data=posts.get("data", {}).get("children", []))

def get_post_data(
self,
subreddit: str,
post_id: str,
comments_sort_criterion: str,
comments_limit: int,
self,
subreddit: str,
post_id: str,
comments_sort_criterion: str,
comments_limit: int,
) -> tuple:
"""
Gets a post's data.
Expand All @@ -252,7 +269,7 @@ def get_post_data(
"""
data: dict = self.get_data(
endpoint=f"{self.base_reddit_endpoint}/r/{subreddit}/comments/{post_id}.json"
f"?sort={comments_sort_criterion}&limit={comments_limit}"
f"?sort={comments_sort_criterion}&limit={comments_limit}"
)
return (
self.validate_data(data=data, valid_key="upvote_ratio"),
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "knewkarma"
version = "2.4.1.0"
version = "2.4.2.0"
description = "A Reddit Data Analysis Toolkit."
authors = ["Richard Mwewa <rly0nheart@duck.com>"]
readme = "README.md"
Expand Down

0 comments on commit 9e7668e

Please sign in to comment.