Package: reddit-python-api
Reddit Python APIA simple unofficial read-only Reddit Python API allowing access to both subreddit and user articles, build withhttpx.Table of contentsIntroduction and requirementsInstallationReddit app & configurationUsageCreate the APIGet a list of articles in a subredditGet a list of user articlesSort typesReturned articles typeDisclaimerIntroduction and requirementsThis Python API was built usinghttpxandPython 3.10.
Python version at least3.10is required.Full list of Python requirements is inrequirements.txtfile.No additional API wrapper was used (likePRAW), Reddit API is accessed directly.
It should be much faster when accessing a list of articles.No data is stored.Reddit is accessed inread-onlymode.Currently, this API allows access to articles in either subreddits or from users.
It doesn't access comments.InstallationProject is available inPyPi:pipinstallreddit-python-apiReddit app & configurationIn order to use this API you need to create a Reddit app athttps://old.reddit.com/prefs/apps/.
Two parameters are requiredclient idandsecretfrom your app.
Those values will be used to acquire OAuth 2.0 token from Reddit API itself.No other data is necessary, since the API works inread-onlymode.When creating the API you can also specify user agent used for communication with Reddit API.
By defaultReddit Python API (by Electronic-Mango on GitHub)is used.
Reddit required a meaningful user agent to be provided by apps.UsageCreate the APITwo parameters are required - app id and secret:fromredditpythonapiimportRedditreddit=Reddit("your client ID","your app secret")You can also specify user-agent:fromredditpythonapiimportRedditreddit=Reddit("your client ID","your app secret","custom user agent")Get a list of articles in a subredditreddit=Reddit("your client ID","your app secret")# reddit.subreddit_articles("subreddit name", load_count, sort_type)articles=reddit.subreddit_articles("Python",10,ArticlesSortType.HOT)First argument is a name of a subreddit.Second argument specifies how many articles should be loaded.
API can return lower number if there are fewer articles in a given subreddit than provided.Third argument specifies sort type.
Following sort types are supported:ArticlesSortType.HOTArticlesSortType.NEWArticlesSortType.RISINGArticlesSortType.TOPArticlesSortType.CONTROVERSIALGet a list of user articlesreddit=Reddit("your client ID","your app secret")# reddit.user_articles("username", load_count, sort_type)articles=reddit.user_articles("spez",20,ArticlesSortType.CONTROVERSIAL)General usage is the same as for subreddits.Sort typesSort types are available inArticlesSortTypeenum. These sort types are available:ArticlesSortType.HOTArticlesSortType.NEWArticlesSortType.RISINGArticlesSortType.TOPArticlesSortType.CONTROVERSIALReturned articles typeReturned type is a list of articles, where each article is aSubmission.Submissionis just an alias todict[str, Any].
Returned data is directly representing JSON returned by official Reddit API, there are no modification to responses.You can check official Reddit API documentation for:users -https://www.reddit.com/dev/api/#GET_user_{username}_submittedsubreddits -https://www.reddit.com/dev/api/#GET_hot(and other endpoints for different sort types)Returned dicts arelistingsdirectly from official Reddit API.
Article contents are quite long, but I've left their additional parsing the user.
Here is an example of one article dict:{'approved_at_utc':None,'subreddit':'wow','selftext':'https://youtu.be/IHZru-6M8BY\n\nJason Hall, currently an indie developer and former Blizzard employee said in one of his videos\n\n&gt;A $15 microtransaction horse made more money than StarCraft 2\n\nHe worked two years of overtime on StarCraft 2: Wings of Liberty. And the entire game ended up making less money for Blizzard than a single mount in World of Warcraft.','author_fullname':'t2_gnuby','saved':False,'mod_reason_title':None,'gilded':0,'clicked':False,'title':'$15 horse for WoW made more money than StarCraft 2: Wings of Liberty','link_flair_richtext':[{'e':'text','t':'Discussion'}],'subreddit_name_prefixed':'r/wow','hidden':False,'pwls':6,'link_flair_css_class':'discussion','downs':0,'thumbnail_height':None,'top_awarded_type':None,'hide_score':False,'name':'t3_17tpwfa','quarantine':False,'link_flair_text_color':'dark','upvote_ratio':0.94,'author_flair_background_color':None,'subreddit_type':'public','ups':2303,'total_awards_received':0,'media_embed':{},'thumbnail_width':None,'author_flair_template_id':None,'is_original_content':False,'user_reports':[],'secure_media':None,'is_reddit_media_domain':False,'is_meta':False,'category':None,'secure_media_embed':{},'link_flair_text':'Discussion','can_mod_post':False,'score':2303,'approved_by':None,'is_created_from_ads_ui':False,'author_premium':False,'thumbnail':'self','edited':False,'author_flair_css_class':None,'author_flair_richtext':[],'gildings':{},'post_hint':'self','content_categories':None,'is_self':True,'mod_note':None,'created':1699812579.0,'link_flair_type':'richtext','wls':6,'removed_by_category':None,'banned_by':None,'author_flair_type':'text','domain':'self.wow','allow_live_comments':True,'selftext_html':'&lt;!-- SC_OFF --&gt;&lt;div class="md"&gt;&lt;p&gt;&lt;a href="https://youtu.be/IHZru-6M8BY"&gt;https://youtu.be/IHZru-6M8BY&lt;/a&gt;&lt;/p&gt;\n\n&lt;p&gt;Jason Hall, currently an indie developer and former Blizzard employee said in one of his videos&lt;/p&gt;\n\n&lt;blockquote&gt;\n&lt;p&gt;A $15 microtransaction horse made more money than StarCraft 2&lt;/p&gt;\n&lt;/blockquote&gt;\n\n&lt;p&gt;He worked two years of overtime on StarCraft 2: Wings of Liberty. And the entire game ended up making less money for Blizzard than a single mount in World of Warcraft.&lt;/p&gt;\n&lt;/div&gt;&lt;!-- SC_ON --&gt;','likes':None,'suggested_sort':None,'banned_at_utc':None,'view_count':None,'archived':False,'no_follow':False,'is_crosspostable':False,'pinned':False,'over_18':False,'preview':{'images':[{'source':{'url':'https://external-preview.redd.it/aTydt70E_WWF4CDra361TKJGXyuZfwQ_WUkymcp6gW8.jpg?auto=webp&amp;s=22796fd51cff670774bfdc6c30fcc5681afaaab6','width':480,'height':360},'resolutions':[{'url':'https://external-preview.redd.it/aTydt70E_WWF4CDra361TKJGXyuZfwQ_WUkymcp6gW8.jpg?width=108&amp;crop=smart&amp;auto=webp&amp;s=6038c06ccc24cd82791506a765b92a96c4481a7d','width':108,'height':81},{'url':'https://external-preview.redd.it/aTydt70E_WWF4CDra361TKJGXyuZfwQ_WUkymcp6gW8.jpg?width=216&amp;crop=smart&amp;auto=webp&amp;s=1b6308d7254313e680e6f8a995fe2ebe11c667a7','width':216,'height':162},{'url':'https://external-preview.redd.it/aTydt70E_WWF4CDra361TKJGXyuZfwQ_WUkymcp6gW8.jpg?width=320&amp;crop=smart&amp;auto=webp&amp;s=afb31085dde1286d636a2bdff992cdfccf744653','width':320,'height':240}],'variants':{},'id':'aqHslU2jfEu8nGDHD1GfMh4aADRzrHE6jlApmtUPZYU'}],'enabled':False},'all_awardings':[],'awarders':[],'media_only':False,'link_flair_template_id':'ad4fefe2-494b-11ea-9fda-0ecf49ab1e6d','can_gild':False,'spoiler':False,'locked':False,'author_flair_text':None,'treatment_tags':[],'visited':False,'removed_by':None,'num_reports':None,'distinguished':None,'subreddit_id':'t5_2qio8','author_is_blocked':False,'mod_reason_by':None,'removal_reason':None,'link_flair_background_color':'','id':'17tpwfa','is_robot_indexable':True,'report_reasons':None,'author':'ErgoNonSim','discussion_type':None,'num_comments':563,'send_replies':False,'whitelist_status':'all_ads','contest_mode':False,'mod_reports':[],'author_patreon_flair':False,'author_flair_text_color':None,'permalink':'/r/wow/comments/17tpwfa/15_horse_for_wow_made_more_money_than_starcraft_2/','parent_whitelist_status':'all_ads','stickied':False,'url':'https://www.reddit.com/r/wow/comments/17tpwfa/15_horse_for_wow_made_more_money_than_starcraft_2/','subreddit_subscribers':2427761,'created_utc':1699812579.0,'num_crossposts':2,'media':None,'is_video':False}DisclaimerThis API is in no way affiliated, associated, authorized, endorsed by, or in any way officially connected with Reddit.
This is an independent and unofficial project.
Use at your own risk.