Created
April 25, 2018 13:39
-
-
Save thismusicdude/b2cf6270a820e2fcd923eb837bda6e8f to your computer and use it in GitHub Desktop.
L-RAW - Little Reddit-Api-Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Latest Update: 25.4.2018 | |
import urllib.request | |
import json | |
class LRAW: | |
""" | |
L-RAW - Little Reddit Api Wrapper | |
:author: github.com/ponyschreck | |
""" | |
def __init__(self, _subreddit, category, sort): | |
""" | |
:param _subreddit: str | |
""" | |
au = "https://www.reddit.com/r/" + str(_subreddit) + "/" + str(category) + ".json?sort=" + str(sort) + "/" | |
url = None | |
try: | |
url = urllib.request.urlopen(au) | |
except urllib.request.HTTPError as e: | |
print("Error: " + e.msg) | |
try: | |
urllib.request.urlcleanup() | |
except urllib.request.HTTPError as e: | |
print("Error: " + e.msg) | |
s = None | |
try: | |
s = str(url.read().decode("utf-8")) | |
except urllib.request.HTTPError as e: | |
print("Error: " + e.msg) | |
try: | |
url.close() | |
except urllib.request.HTTPError as e: | |
print("Error: " + e.msg) | |
res = json.loads(s) | |
data = res["data"] | |
self.posts = data["children"] | |
self.kind = res["kind"] | |
def get_all_posts(self): | |
""" | |
Gets all posts | |
:return: all posts | |
""" | |
return self.posts | |
def get_post(self, number: int = 0): | |
""" | |
gets all posts | |
:param number: the post number | |
:return: dictionary | |
""" | |
return self.posts[number]["data"] | |
@staticmethod | |
def get_preview_picture(post,): | |
return post["preview"]["images"][0]["source"]["url"] | |
@staticmethod | |
def get_link_to_user(post): | |
return "https://reddit.com/user/" + post["author"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment