Skip to content

Commit

Permalink
Lint new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradIT committed Oct 21, 2022
1 parent 02cb71b commit e0ce3aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
17 changes: 10 additions & 7 deletions Parler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def __init__(self, debug: bool = False, config_file: string = None):
self.session.headers["User-Agent"] = ua.random

self.__log = logging.getLogger("parler-py-api")
self.__log.setLevel(level=logging.DEBUG if self.__debug else logging.ERROR)
self.__log.setLevel(
level=logging.DEBUG if self.__debug else logging.ERROR)
self.__log.debug(f"User-Agent: {self.session.headers['User-Agent']}")
# Default values
self.__reconnects = 0
Expand Down Expand Up @@ -78,7 +79,8 @@ def __init__(self, debug: bool = False, config_file: string = None):
def handle_response(self, response):
if self.__reconnects >= self.__max_reconnects:
raise Exception(
"Internal abort; {} reconnect attemps".format(self.__max_reconnects)
"Internal abort; {} reconnect attemps".format(
self.__max_reconnects)
)
elif response.status_code >= 400 and response.status_code <= 428:
raise Exception(
Expand Down Expand Up @@ -140,9 +142,9 @@ def discover_feed(self, limit: int = None, cursor: str = None) -> dict:

def user_feed(self, username: str = "", cursor: int = 1, limit: int = 10, media_only: int = 0) -> dict:
params = (
("page", cursor),
("limit", limit),
("media_only", media_only)
("page", cursor),
("limit", limit),
("media_only", media_only)
)
response = self.get("v0/public/user/%s/feed" % username, params=params)
if self.handle_response(response).status_code != 200:
Expand All @@ -156,8 +158,9 @@ def user_feed(self, username: str = "", cursor: int = 1, limit: int = 10, media_

def trending(self, tab: str = "today") -> dict:
if tab != "today":
raise self.OldParameterException("%s no longer supported in newer Parler API." % ("tab=top"))

raise self.OldParameterException(
"%s no longer supported in newer Parler API." % ("tab=top"))

response = self.get("v0/public/trending/parleys/today")
if self.handle_response(response).status_code != 200:
self.__log.warning(f"Status: {response.status_code}")
Expand Down
40 changes: 22 additions & 18 deletions Parler/with_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ def __init__(self, debug: bool = False, config_file: string = None):
"access_token": "",
"refresh_token": ""
}

class NotLoggedIn(Exception):
pass

def login(self, identifier: str, password: str) -> dict:

data = (
{"identifier": identifier, "password": password}
)
response = self.post("v0/login", json=data)

if self.handle_response(response).status_code != 200:
self.__log.warning(f"Status: {response.status_code}")
return self.login(identifier=identifier, password=password)

received = response.json()
self.__credentials["access_token"] = received["access_token"]
self.__credentials["refresh_token"] = received["refresh_token"]

self.session.headers["Authorization"] = "Bearer " + received["access_token"]

self.session.headers["Authorization"] = "Bearer " + \
received["access_token"]
return response.json()

@property
Expand All @@ -63,10 +65,11 @@ def feed(
subscriptions_only: bool = False,
limit: int = 10
) -> Optional[dict]:

if hide_echoes or subscriptions_only:
raise self.OldParameterException("%s no longer supported in newer Parler API." % ("hide_echoes" if hide_echoes else ("subscriptions_only" if subscriptions_only else "")))

raise self.OldParameterException("%s no longer supported in newer Parler API." % (
"hide_echoes" if hide_echoes else ("subscriptions_only" if subscriptions_only else "")))

params = (
("page", cursor),
("limit", limit),
Expand All @@ -87,8 +90,8 @@ def feed(
@check_login
def users(self, searchtag: str = "", cursor: int = 1, limit: int = 10) -> dict:
params = (
("page", cursor),
("limit", limit),
("page", cursor),
("limit", limit),
)
response = self.get("v0/search/user/%s" % searchtag, params=params)
if self.handle_response(response).status_code != 200:
Expand All @@ -103,8 +106,8 @@ def users(self, searchtag: str = "", cursor: int = 1, limit: int = 10) -> dict:
@check_login
def hashtags(self, searchtag="", cursor: int = 1, limit: int = 10) -> dict:
params = (
("page", cursor),
("limit", limit),
("page", cursor),
("limit", limit),
)
response = self.get("v0/search/hashtag/%s" % searchtag, params=params)
if self.handle_response(response).status_code != 200:
Expand Down Expand Up @@ -144,8 +147,8 @@ def trending_users(self):
@check_login
def following(self, username: str = "", cursor: int = 1, limit: int = 20) -> dict:
params = (
("page", cursor),
("limit", limit),
("page", cursor),
("limit", limit),
)
response = self.get("v0/user/%s/following" % username, params=params)
if self.handle_response(response).status_code != 200:
Expand All @@ -165,7 +168,8 @@ def comments(self, post_id: str = "", cursor: int = 0, limit: int = 10, timestam
("limit", limit),
("timestamp", timestamp),
)
response = self.get("v0/parleys/%s/comments_before/" % post_id, params=params)
response = self.get("v0/parleys/%s/comments_before/" %
post_id, params=params)
if self.handle_response(response).status_code != 200:
self.__log.warning(f"Status: {response.status_code}")
return self.comments(post_id=post_id, cursor=cursor, limit=limit, timestamp=timestamp)
Expand Down Expand Up @@ -199,11 +203,11 @@ def unfollow_user(self, username) -> dict:
"""

@check_login
def created_items(self, username="", limit=10, cursor="", media_only: int = 0) -> dict:
def created_items(self, username="", limit=10, cursor="", media_only: int = 0) -> dict:
params = (
("page", cursor),
("limit", limit),
("media_only", media_only)
("page", cursor),
("limit", limit),
("media_only", media_only)
)
response = self.get("v0/user/%s/feed/" % username, params=params)
if self.handle_response(response).status_code != 200:
Expand Down

0 comments on commit e0ce3aa

Please sign in to comment.