Post the subject of Gmail on twitter

Yes, we are assuming an environment where you cannot access twitter even if you send an email to the outside or to any destination during work, although it is Sef Sef. Emails will be deleted automatically after tweeting. The purpose is to prevent duplicate postings with linked evidence destruction. Yes, you need to install it somewhere at home or rent it and run it regularly. http://symfoware.blog68.fc2.com/blog-entry-891.html We are indebted to you for getting the subject of Gmail. Or rather, it's a copy. I just removed the elements other than the subject acquisition as much as possible and added a posting function to delete the email. Thank you for your support.

gmail_twitter.py


#!/usr/bin/env python
# -*- coding:utf-8 -*-
import imaplib, email, email.Header
import sys
from urllib import urlencode

#I have my own Oauth authentication token.
#If you don't set it aside, you're afraid of disclosure due to an accident.
import oat
"""
About Gmail acquisition
http://symfoware.blog68.fc2.com/blog-entry-891.html
We have received more.
"""
class imap4mail(object):


    def __init__(self, data):
        """
Perform parsing of the mail data given in the constructor
        """
        self.files = {}

        #Parse message
        msg = email.message_from_string(data)
        #Title acquisition
        self.title = self.decode(msg.get('Subject'))


    def decode(self, dec_target):

        """
Email title, sender decoding
        """
        decodefrag = email.Header.decode_header(dec_target)
        title = ''

        for frag, enc in decodefrag:
            if enc:
                title += unicode(frag, enc)
            else:
                title += unicode(frag)
        return title


def analize_mail(mail):

    #Post the subject of the retrieved email
    oat.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status': mail.title.encode('utf-8')}))

if __name__ == "__main__":

    host = 'imap.gmail.com'
    user = '[email protected]'
    password = 'password'
    mailbox = 'INBOX'

    #Mail server specification
    M = imaplib.IMAP4_SSL(host=host)
    #Login
    M.login(user, password)

    #Mailbox selection
    M.select(mailbox)

    typ, data = M.search(None, 'ALL')
    for num in data[0].split():
        typ, data = M.fetch(num, '(RFC822)')
        mail = imap4mail(data[0][1])
#Post to twitter
        analize_mail(mail)
#Delete email after posting
        M.store(num, '+FLAGS', '\\Deleted')
    M.expunge()
    M.close()
    M.logout()

Yes, comments etc. are almost as they are. If I change the comment, that's it.

Recommended Posts

Post the subject of Gmail on twitter
Try to estimate the number of likes on Twitter
Post to your account using the API on Twitter
Tweet the triple forecast of the boat race on Twitter
I studied with Kaggle Start Book on the subject of kaggle [Part 1]
I checked the image of Science University on Twitter with Word2Vec.
Investigate the effect of outliers on correlation
Display the graph of tensorBoard on jupyter
Change the order of PostgreSQL on Heroku
twitter on python3
The behavior of signal () depends on the compile options
[2020July] Check the UDID of the iPad on Linux
Use the latest version of PyCharm on Ubuntu
Send Gmail at the end of the process [Python]
Calculate the probability of outliers on a boxplot
At the time of python update on ubuntu
Change the resolution of Ubuntu running on VirtualBox
[AWS S3] Confirmation of the existence of folders on S3
Save the search results on Twitter to CSV.
Drawing on Jupyter using the plot function of pandas
Looking back on the transition of the Qiita Advent calendar
Predict the gender of Twitter users with machine learning
Difference in results depending on the argument of multiprocess.Process
Scroll Japanese on the LED of RaspberryPi Sense HAT
A Study on Visualization of the Scope of Prediction Models
Create a shape on the trajectory of an object
Announcing the availability of Java 11 LTS on Amazon Linux 2
The beginning of cif2cell
Character count on Twitter
the zen of Python
The story of sys.path.append ()
Revenge of the Types: Revenge of types
Notes on the version of CUDA, cuDNN where tensorflow-gpu worked
Put the latest version of Python on linux (Debian) on Chromebook
A note on the default behavior of collate_fn in PyTorch
I analyzed tweets about the new coronavirus posted on Twitter
The story of how the Python bottle worked on Sakura Internet
Follow the mystery of orthographic-pedant that suddenly appeared on GitHub !!
Roughly calculate the estimated sleep time based on twitter usage time
Survey on the use of machine learning in real services
Let's execute the command on time with the bot of discord
Analyzing data on the number of corona patients in Japan
Count the number of characters in the text on the clipboard on mac
Data analysis based on the election results of the Tokyo Governor's election (2020)
[Python] Calculate the angle consisting of three points on the coordinates
Try to improve the accuracy of Twitter like number estimation
[Python] I tried to visualize the follow relationship of Twitter
Until the start of the django tutorial with pycharm on Windows
Post images of Papillon regularly on Python + AWS Lambda + Slack
Set information such as length on the edge of NetworkX
Create a correlation diagram from the conversation history of twitter
Install the latest version of Git on your Linux server
Exploring the possibility of recovering deleted VMDK files on VMFS
Get the host name of the host PC with Docker on Linux