def add_default_subreddit(user): subreddit = Subreddit(user_id=user.id, title='RZNU subreddit', description='Nothing special here') db.session.add(subreddit) db.session.commit() return subreddit
def _subreddit(self, subreddit_name, short_title): s = None try: s = Subreddit.objects.get(name=subreddit_name) return s except: search_url = "http://www.reddit.com/reddits/search.json?q=" + short_title subreddit_result = RedditRequest(search_url, self._wait_time).read() search_dict = json.loads(subreddit_result) search_results = search_dict["data"]["children"] for potential_subreddit in search_results: potential_subreddit = potential_subreddit["data"] if potential_subreddit["name"] == subreddit_name: s = Subreddit( url=potential_subreddit["url"], name=potential_subreddit["name"], title=potential_subreddit["title"], description=potential_subreddit["description"], public_description=potential_subreddit["public_description"], reddit_id=potential_subreddit["id"], ) s.save() break return s
def subreddits_root(): subreddits = Subreddit.query.all() form = SubredditForm(request.form) if request.method == 'POST': if form.validate(): subreddit = Subreddit(user_id=current_user.id, title=request.form.get('title'), description=request.form.get('description')) db.session.add(subreddit) db.session.commit() return redirect(f'/subreddits/{subreddit.id}') flash('Invalid data entered') return render_template('subreddits.html', form=form, subreddits=subreddits) return render_template('subreddits.html', form=form, subreddits=subreddits)
def handle(self, *args, **options): subredditName = options['subreddit'] reddit = praw.Reddit(client_id=settings.REDDIT['CLIENT_ID'], client_secret=settings.REDDIT['CLIENT_SECRET'], user_agent=settings.REDDIT['USER_AGENT']) subredditName = subredditName.lower().strip() try: sub = Subreddit.objects.get(name=subredditName) print("Subreddit is already added\n") except Subreddit.DoesNotExist: prawSubreddit = reddit.subreddit(subredditName) try: print("Name: {}\nDescription: {}\nNSFW: {}".format( prawSubreddit.display_name, prawSubreddit.public_description, prawSubreddit.over18)) except Exception: print("Subreddit {} does not exist".format(subredditName)) return try: doesExist = Subreddit.objects.get( name=prawSubreddit.display_name) print("Subreddit is already added\n") return except Subreddit.DoesNotExist: pass if not prawSubreddit.public_description: print("Subreddit has no description") return sub = Subreddit() sub.name = prawSubreddit.display_name sub.description = prawSubreddit.public_description sub.nsfw = prawSubreddit.over18 sub.save() for sub in Subreddit.objects.all(): print(sub.display_name)
def _scrape_subreddits(self): subreddits_page_request = urllib2.Request( "http://www.reddit.com/reddits.json", headers={"User-agent": "hivemindio"} ) subreddits_page_handler = urllib2.urlopen(subreddits_page_request) subreddits_json = subreddits_page_handler.read() subreddits = json.loads(subreddits_json) subreddits = subreddits["data"] # after = subreddits['after'] for subreddit in subreddits["children"]: subreddit = subreddit["data"] try: s = Subreddit.objects.get(url=subreddit["url"]) except: s = Subreddit( url=subreddit["url"], name=subreddit["name"], title=subreddit["title"], description=subreddit["description"], public_description=subreddit["public_description"], reddit_id=subreddit["id"], ) s.save() time.sleep(60) while "after" in subreddits: subreddits_page_request = urllib2.Request( "http://www.reddit.com/reddits.json?after=" + subreddits["after"], headers={"User-agent": "pseudorandomstring"}, ) subreddits_page_handler = urllib2.urlopen(subreddits_page_request) subreddits_json = subreddits_page_handler.read() subreddits = json.loads(subreddits_json) subreddits = subreddits["data"] # after = subreddits['after'] for subreddit in subreddits["children"]: subreddit = subreddit["data"] print subreddit["url"] try: s = Subreddit.objects.get(url=subreddit["url"]) except: try: s = Subreddit( url=subreddit["url"], name=subreddit["name"], title=subreddit["title"], description=subreddit["description"], public_description=subreddit["public_description"], reddit_id=subreddit["id"], ) s.save() except: print subreddit exit() time.sleep(60)