def __get_current_nonce(self) -> str: """ Returns the current nonce :raises: NotLoggedInException """ try: nonce = json.loads(parse.unquote( self.__login_cookie["me"]))["id"][0:16] except TypeError: raise NotLoggedInException() if not self.logged_in or nonce is None or nonce == "": raise NotLoggedInException() return nonce
def ratelimit(self) -> int: """ Returns the remaining ratelimit (uploads) for the currently logged in user :raises: NotLoggedInException """ if not self.logged_in: raise NotLoggedInException() r = get(url=self.api_url + "items/ratelimited") try: return r.json()["left"] except KeyError: raise NotLoggedInException() except TypeError: return 0
def get_messages_with_user(self, user: str, older: str or str = None) -> str: """ Gets messages from a specific user Parameters ---------- :param user: str username from the other user :param older: int or str messages older than this id will be returned :return: str Returns messages from a specified user """ r = "" if older is None: r = get("https://pr0gramm.com/api/inbox/messages", params={"with": user}, cookies=self.__login_cookie) else: r = get("https://pr0gramm.com/api/inbox/messages", params={ "with": user, "older": id }, cookies=self.__login_cookie) content = json.loads(r.content.decode("utf-8")) try: if content["code"] == 403: raise NotLoggedInException() except KeyError: pass return r.content.decode("utf-8")
def get_inbox(self, older: int = 0) -> str: """ login required Gets messages from inbox Parameters ---------- :param older: int gets the next messages after 'older' :return: json Returns messages """ r = "" if older <= 0: r = get("https://pr0gramm.com/api/inbox/all", params={}, cookies=self.__login_cookie) else: r = get("https://pr0gramm.com/api/inbox/all", params={'older': older}, cookies=self.__login_cookie) content = json.loads(r.content.decode("utf-8")) try: if content["code"] == 403: raise NotLoggedInException() except KeyError: pass return r.content.decode("utf-8")
def vote_tag(self, id: int, vote: int) -> bool: """ Vote for a tag with a specific id Parameters ---------- :param id: int or str tag id that will be voted for :param vote: int or str type of vote: -1 = - 0 = unvote (nothing) 1 = + :return: bool returns true if the vote was successful else false :raises: NotLoggedInException if user is not logged in """ if self.logged_in: nonce = json.loads(parse.unquote( self.__login_cookie["me"]))["id"][0:16] r = post(self.api_url + "tags/vote", data={ "id": id, "vote": vote, '_nonce': nonce }, cookies=self.__login_cookie) return r.status_code == 200 else: raise NotLoggedInException()
def vote_comment(self, id: int or str, vote: int or str) -> bool: """ Vote for a comment with a specific id Parameters ---------- :param id: int or str comment id that will be voted for :param vote: int or str type of vote: -1 = - 0 = unvote (nothing) 1 = + 2 = add to favorite :return: bool returns true if the vote was successful else false :raises: NotLoggedInException if user is not logged in """ if self.logged_in: nonce = self.__get_current_nonce() r = post(self.api_url + "comments/vote", data={ "id": id, "vote": vote, '_nonce': nonce }, cookies=self.__login_cookie) return r.status_code == 200 else: raise NotLoggedInException()
def __raise_possible_exceptions(r: requests.models.Response): """ Raises exceptions depending on http status code from request :param r: response returned by a request :return: None :raises NotLoggedInException if status code is 403 (forbidden) """ if r.status_code == 403: raise NotLoggedInException()
def get_user_comments(self, user: str, created: int = -1, older: bool = True, flag: int = 1) -> str: """ login required Get comments For example: 'https://pr0gramm.com/api/profile/comments?name=itssme&before=1528718127' Parameters ---------- :param user: str get uploads from one specific user :param created: int Date of the comment :param older bool None gets the newest comment True gets all comments older than item False gets all comments newer than item :param flag: int or str see api.md for details call calculate_flag if you are not sure what flag to use :return: str json reply from api """ if older is None: r = get(self.profile_user + "?name=" + user, params={'flags': flag}, cookies=self.__login_cookie) elif older: r = get(self.profile_comments + "?name=" + user, params={ 'flags': flag, 'before': created }, cookies=self.__login_cookie) else: r = get(self.profile_comments + "?name=" + user, params={ 'flags': flag, 'after': created }, cookies=self.__login_cookie) try: if r.content["code"] == 403: raise NotLoggedInException() except KeyError: pass return r.content.decode("utf-8")