Coder Social home page Coder Social logo

reddit-posts-on-email's Introduction

Reddit-posts-on-email

Send the reddit post you want to your email!

Requirements: Python 3.8+, Praw, google-api-python-client google-auth-httplib2 google-auth-oauthlib

You may have trouble installing google requirements, if you have trouble try:

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

FAQ

Can I add a database to my project?

Yes, you can. I use mongodb in this example, but you can use other db. If you will use mongodb too, You should edit “collectionname” inside database.py.

"""
Well, in this example I only store submission.id to get already sent posts in subreddit
and Already created unique and ttl indexes via mongosh 
"""

import praw
import os
from datetime import datetime
from dotenv import load_dotenv
from gmail import send_message, gmail_authenticate 
from database import db, check_mongo_connection

load_dotenv()

reddit = praw.Reddit(
    client_id= os.getenv(''),
    client_secret= os.getenv(''),
    user_agent= os.getenv(''),
)

send_to = os.getenv('')
formated_string = []
#Check if your connection with mongodb works
check_connection = check_mongo_connection(os.getenv(''))

for submission in reddit.subreddit('name of subreddit').new():  
    #Create a variable that check if index already exist inside collection
	check_if_exist = db.posts.find_one({'id': submission.id})
		if not check_if_exist:
		"""
		Insert id to check if exist in unique inside mongodb and 
		get date to delete when ttl index expire
		"""	
		db.collection.insert_many(
			[
				{
					'id': submission.id,
					'date': datetime.utcnow()
				}
			]	
		)
                           
		email_body = f'Sub: {submission.subreddit}\nTitle: {submission.title}\nLink: reddit.com/{submission.permalink}\n'
    		formated_string.append(email_body)
    
  if formated_string == []:
        print('Nothing to send')
        pass
  else:
        service = gmail_authenticate()
        send_message(service, '', '', '\n'.join(formated_string))
        print('Your email was sent!')

How add sleep time in email function?

It’s simple, just create a condition and put logic inside

if sleep_time != 0:
        if formated_string == []:
           print('Nothing to send')
           pass
        else:
           service = gmail_authenticate()
           send_message(service, send_to, 'Posts reddit', '\n'.join(formated_string))
           print('Your email was sent!')
           time.sleep(sleep_time)

Is it possible have more than 1 sub?

Yes, but it is a bit trick, you have two options.

I recommend this one if you will use 3 or less subs:

import praw
import os
from dotenv import load_dotenv
from gmail import send_message, gmail_authenticate 

load_dotenv()

reddit = praw.Reddit(
    client_id= os.getenv(''),
    client_secret= os.getenv(''),
    user_agent= os.getenv(''),
)

send_to = os.getenv('')
formated_string = []

for submission in reddit.subreddit("anime_irl+nier+sports").new():  
    email_body = f'Sub: {submission.subreddit}\nTitle: {submission.title}\nLink: reddit.com/{submission.permalink}\n'
    formated_string.append(email_body)
    
    if formated_string == []:
        print('Nothing to send')
        pass
    else:
        service = gmail_authenticate()
        send_message(service, send_to, '', '\n'.join(formated_string))
        print('Your email was sent!')

If you will use more than 3 you will need create a list and use a for, Idk why praw method doesn’t work with more than 3 subs.

import praw
import os
from dotenv import load_dotenv
from gmail import send_message, gmail_authenticate 

load_dotenv()

reddit = praw.Reddit(
    client_id= os.getenv(''),
    client_secret= os.getenv(''),
    user_agent= os.getenv(''),
)
sub_names = ['anime_irl', 'nier', 'sports', 'NBA2k']
send_to = os.getenv('')
formated_string = []

for subs in sub_names:
    for submission in reddit.subreddit(subs).new():  
        email_body = f'Sub: {submission.subreddit}\nTitle: {submission.title}\nLink: reddit.com/{submission.permalink}\n'
        formated_string.append(email_body)

        if formated_string == []:
            print('Nothing to send')
            pass
        else:
            service = gmail_authenticate()
            send_message(service, send_to, '', '\n'.join(formated_string))
            print('Your email was sent!')

Can I use only gmail functions?

Yeah, you can use, just use gmail.py and import functions.

Can I search for tags with praw?

Yeah, you can use something like that.

import praw
import os
from dotenv import load_dotenv
from gmail import send_message, gmail_authenticate 

load_dotenv()

reddit = praw.Reddit(
    client_id= os.getenv(''),
    client_secret= os.getenv(''),
    user_agent= os.getenv(''),
)

send_to = os.getenv('')
formated_string = []

for subs in sub_names:
    for submission in reddit.subreddit('artcommissions').new():  
        #Hiring is a tag in artcommissions subreddit
	if 'hiring' in submission.title:
		email_body = f'Sub: {submission.subreddit}\nTitle: {submission.title}\nLink: reddit.com/{submission.permalink}\n'
        	formated_string.append(email_body)

        if formated_string == []:
            print('Nothing to send')
            pass
        else:
            service = gmail_authenticate()
            send_message(service, send_to, '', '\n'.join(formated_string))
            print('Your email was sent!')

If you had trouble because sensitive case you can lower submission.title to avoid this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.