How to get a daily posts report for selected authors into your mailbox in Python

in #utopian-io7 years ago

Although Steemit UI provides a feed feature which list posts from the authors you are following, Sometimes it is still too much information for you to read. Here I will show you how to write a Python script to generate a daily posts report for selected authors and send it to your email address automatically.

The library needed

In this script, we are using Steem Python. Please make sure you have installed it before trying the script.

The code

Here is the code to do the job:

import smtplib
from email.mime.text import MIMEText
from datetime import datetime
from steem.blog import Blog
from sys import argv
import traceback

if len(argv) !=3:
  print ('Usage: python daily_summary.py MAX_NUMBER_OF_POSTS_TO_CHECK ACCOUNT_LIST')
  exit()
max_daily_number_of_posts_to_check = int(argv[1])
users                              = argv[2].split(',')

steemit_url = 'https://steemit.com'

summary = ''

try:
  for user in users:
    summary += "User %s <br/>"  % user
    summary += "------------------<br/>"
    posts = Blog(user)
    for p in posts.take(max_daily_number_of_posts_to_check):
      p_date = p['created']
      today = datetime.today().date()
      created_today = (p_date.date()==today)

      if created_today == True:
        summary += "<a href=\"%s%s\">%s</a><br/>" % (steemit_url, p['url'], p['title'])
        summary += " Category: %s<br/>" % str(p['tags'][0])
        summary += " Votes: %s<br/>" % str(p['net_votes'])
        summary += " Comments: %s<br/>" % str(len(list(p.get_replies())))
        summary += " Rewards: %s<br/>" % str(p['pending_payout_value'])
        summary +=  "<br/>"

    summary +=  "<br/>"

  username = 'YOUR_GMAIL_ADDRESS'
  password = 'YOUR_GMAIL_PASSWORD'

  toaddrs  = 'YOUR_EMAIL'
  subject = 'Daily post summary: ' + datetime.today().strftime('%Y-%m-%d')
  message = MIMEText(summary.encode('utf-8'), _subtype='html', _charset='utf-8')
  message['Subject'] = subject

  server = smtplib.SMTP_SSL('smtp.gmail.com',465)
  server.ehlo()
  server.login(username,password)
  server.sendmail(username, toaddrs, message.as_string())
  server.close()
except:
  print ('Something went wrong...')
  traceback.print_exc()

Run the script

To run the script, first replace GMAIL account / password by yours. You also need to change your gmail settings to allow less secure apps, as described here:
https://support.google.com/accounts/answer/6010255

Now you can run this command:

python daily_summary.py 3 kwonn,samrg472

It will generate the report and send it to your email box.

image.png

You can modify the above command, e.g. pass your favourite author list to the command and your customized report.

Automate the report generation

Now you can specify a daily cron job to automate the report generation. Please refer to cron documentation here: https://help.ubuntu.com/community/CronHowto



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

edit: self-upvoted for visibility
edit2: this is a type of tutorial I want to see more of! Keep it up!

Hey @yuxid I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Please back

Congratulations @yuxid! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes
Award for the number of upvotes received
You published a post every day of the week

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!