<reponame>Fire-The-Fox/LatestSubredditPost
import urllib.error as url_error
import urllib.request as requests
from json import loads
from random import choice


class _UnknownSubreddit(Exception):
    pass


class _WrongPostType(Exception):
    pass


def reddit_json(subreddit="r/Python", post_type="new"):
    """

    :param subreddit: subreddit of which you want to get data
    :param post_type: new or top
    :return: api respond ( json data )
    """
    url = ""

    if "r/" not in subreddit:
        raise _UnknownSubreddit(f"Subreddit with name: {subreddit} was not found!")
    if post_type not in ["new", "top"]:
        raise _WrongPostType(f"Post type with name: {post_type} was not found! Only new and top are accepted!")
    if post_type == "new":
        url = f"https://www.reddit.com/{subreddit}/{post_type}.json"
    if post_type == "top":
        url = f"https://www.reddit.com/{subreddit}/{post_type}.json?t=all"
    try:
        data = requests.urlopen(url).read()
        encoding = requests.urlopen(url).info().get_content_charset('utf-8')
        json_data = loads(data.decode(encoding))
        return json_data
    except url_error.HTTPError:
        return {"data": "Too Many Requests"}


def first_post(reddit_api_respond: dict):
    """
    This function will return first post
    :param reddit_api_respond: json respond from api ( checker.reddit_json() )
    :return: list
    """
    if reddit_api_respond["data"] == "Too Many Requests":
        return ["STOP NOW"]
    else:
        post_data = reddit_api_respond["data"]["children"][0]["data"]
        post_title = post_data["title"]
        post_description = post_data["selftext"]
        post_video = post_data["is_video"]
        post_score = post_data["score"]
        post_thumbnail = post_data["thumbnail"]
        post_comments_count = post_data["num_comments"]
        post_author = post_data["author"]
        post_url = "https://www.reddit.com" + post_data["permalink"]
        return [post_title, post_description, post_video, post_score, post_thumbnail, post_comments_count, post_author, post_url]


def random_post(reddit_api_respond: dict):
    """
    This function will return random post
    :param reddit_api_respond: json respond from api ( checker.reddit_json() )
    :return: list
    """
    if reddit_api_respond["data"] == "Too Many Requests":
        return [""]
    else:
        post_data = reddit_api_respond["data"]["children"]
        post_data = choice(post_data)
        post_data = post_data["data"]
        post_title = post_data["title"]
        post_description = post_data["selftext"]
        post_video = post_data["is_video"]
        post_score = post_data["score"]
        post_thumbnail = post_data["thumbnail"]
        post_comments_count = post_data["num_comments"]
        post_author = post_data["author"]
        post_url = "https://www.reddit.com" + post_data["permalink"]
        return [post_title, post_description, post_video, post_score, post_thumbnail, post_comments_count, post_author, post_url]

import urllib.error as url_error
import urllib.request as requests
from json import loads
from random import choice


class _UnknownSubreddit(Exception):
    pass


class _WrongPostType(Exception):
    pass


def reddit_json(subreddit="r/Python", post_type="new"):
    """

    :param subreddit: subreddit of which you want to get data
    :param post_type: new or top
    :return: api respond ( json data )
    """
    url = ""

    if "r/" not in subreddit:
        raise _UnknownSubreddit(f"Subreddit with name: {subreddit} was not found!")
    if post_type not in ["new", "top"]:
        raise _WrongPostType(f"Post type with name: {post_type} was not found! Only new and top are accepted!")
    if post_type == "new":
        url = f"https://www.reddit.com/{subreddit}/{post_type}.json"
    if post_type == "top":
        url = f"https://www.reddit.com/{subreddit}/{post_type}.json?t=all"
    try:
        data = requests.urlopen(url).read()
        encoding = requests.urlopen(url).info().get_content_charset('utf-8')
        json_data = loads(data.decode(encoding))
        return json_data
    except url_error.HTTPError:
        return {"data": "Too Many Requests"}


def first_post(reddit_api_respond: dict):
    """
    This function will return first post
    :param reddit_api_respond: json respond from api ( checker.reddit_json() )
    :return: list
    """
    if reddit_api_respond["data"] == "Too Many Requests":
        return ["STOP NOW"]
    else:
        post_data = reddit_api_respond["data"]["children"][0]["data"]
        post_title = post_data["title"]
        post_description = post_data["selftext"]
        post_video = post_data["is_video"]
        post_score = post_data["score"]
        post_thumbnail = post_data["thumbnail"]
        post_comments_count = post_data["num_comments"]
        post_author = post_data["author"]
        post_url = "https://www.reddit.com" + post_data["permalink"]
        return [post_title, post_description, post_video, post_score, post_thumbnail, post_comments_count, post_author, post_url]


def random_post(reddit_api_respond: dict):
    """
    This function will return random post
    :param reddit_api_respond: json respond from api ( checker.reddit_json() )
    :return: list
    """
    if reddit_api_respond["data"] == "Too Many Requests":
        return [""]
    else:
        post_data = reddit_api_respond["data"]["children"]
        post_data = choice(post_data)
        post_data = post_data["data"]
        post_title = post_data["title"]
        post_description = post_data["selftext"]
        post_video = post_data["is_video"]
        post_score = post_data["score"]
        post_thumbnail = post_data["thumbnail"]
        post_comments_count = post_data["num_comments"]
        post_author = post_data["author"]
        post_url = "https://www.reddit.com" + post_data["permalink"]
        return [post_title, post_description, post_video, post_score, post_thumbnail, post_comments_count, post_author, post_url]
:param subreddit: subreddit of which you want to get data     :param post_type: new or top     :return: api respond ( json data ) This function will return first post     :param reddit_api_respond: json respond from api ( checker.reddit_json() )     :return: list This function will return random post     :param reddit_api_respond: json respond from api ( checker.reddit_json() )     :return: list