Skip to content

Instantly share code, notes, and snippets.

@Itz-fork
Last active December 6, 2023 09:13
Show Gist options
  • Save Itz-fork/31abf07fa757be58d3221a8ab826a5ed to your computer and use it in GitHub Desktop.
Save Itz-fork/31abf07fa757be58d3221a8ab826a5ed to your computer and use it in GitHub Desktop.
Reddit Post Search API Example
import requests
def search_reddit_posts(q, sub_reddit=None):
if sub_reddit:
req = requests.get(f"https://nexa-apis.herokuapp.com/reddit?query={q}&subreddit={sub_reddit}").json()
else:
req = requests.get(f"https://nexa-apis.herokuapp.com/reddit?query={q}").json()
if req["status"] == "Ok":
return req["data"]
else:
return None
cont = """
Title: {}
Image Url: {}
Author: {}
Content: {}
Sub: {}
Post number {}
---------------------------
"""
rs = search_reddit_posts("Python", "ProgrammerHumor")
if rs:
for item in rs:
image = item["image"]
title = item["title"]
author = item['author']
pst_content = item["post_content"] if item["post_content"] else ""
subred = item["subreddit"]
result_no = item["result_no"]
print(cont.format(title, image, author, pst_content, subred, result_no))
else:
print("Oops, Error Happend!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment