Easy Twitter Posting Program

I made it with reference to the idea of how to assemble, which @shiracamus taught me.

Certainly, it's easy to get into your head when you make corrections when you forget it. Maintenance power will increase.

How to use.

You can use it immediately by starting it with a shell script or something.

The message function that I put in for the time being


TL().message([username],[text])

You can do it with.

Like this スクリーンショット 2017-03-31 10.32.19.png

script

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from twitter import *

class TL:
    def __init__(self):
        #Please get and enter by yourself
        OAUTH_TOKEN = ""
        OAUTH_SECRET = ""
        CONSUMER_KEY = ""
        CONSUMER_SECRET = ""
        self.T = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,CONSUMER_KEY, CONSUMER_SECRET))
    def tweet(self,text):
        self.T.statuses.update(status=text)
    def message(self,username,text):
        self.T.direct_messages.new(user=username,text=text)

class UI:
    def text():
        return input("Enter a tweet:")
    def generate_tag():
        tag = ''
        while True:
            t = input("tag(#None):")
            if t == "":
                break
            else:
                tag += " #" + t
        return tag
    def tweet(text):
        print("Posting...")
        TL().tweet(text)
        print("-"*20 + "\n" + text + "\n" + "-"*20)


if __name__ == '__main__':
    while True:
        text = UI.text() + UI.generate_tag()
        UI.tweet(text)

Postscript: tweepy was easier to handle

# -*- coding: utf8 -*-
import tweepy

class Account:
    consumer_key        = ""
    consumer_secret     = ""
    access_token_key    = ""
    access_token_secret = ""
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token_key, access_token_secret)
    api = tweepy.API(auth)
class TL():
    def __init__(self):
        self.api = Account.api
    def post(self,text):
        self.api.update_status(text)
    def post_media(self,media,text):
        self.api.update_with_media(media,status=text)
    def home_timeline(self,count=5):
        return self.api.home_timeline(count=count)
    def user_timeline(self,count=5):
        return self.api.user_timeline(screen_name=[username],count=count)
    def get_followers(self):
        return self.api.followers(count=10)
    def send_message(self,screen_name,text):
        self.api.send_direct_message(screen_name, text)

class UI:
    def hr(icon="-"):
        print(icon*30)
    def post():
        text = UI.text()
        if text in ["media","jpg","png","img"]:
            media = input("URL or image path:").replace(".png ",".png ").replace(".jpg ",".jpg ")
            text = UI.text() + UI.tag()
            TL().post_media(media,text)
        else:
            text = text + UI.tag()
            TL().post(text)
        UI.hr()
        print(text)
        UI.hr()
    def send_message(screen_name,text):
        TL().send_message(screen_name,text)
    def home_timeline():
        for tl in TL().home_timeline():
            print(tl.created_at) #datetime display
            print(tl.text)
            UI.hr()
    def user_timeline():
        for tl in TL().user_timeline():
            print(tl.created_at) #datetime display
            print(tl.text)
            UI.hr()
    def get_followers():
        for user in TL().get_followers():
            print(user.screen_name)
            UI.hr()
    def text():
        return input("Enter a tweet:")
    def tag():
        t = ''
        while True:
            put = input("tag(#None):")
            if put == "":
                break
            else:
                t += " #" + put
        return t

def main():
    #UI.post()
    UI.user_timeline()
    UI.get_followers()

if __name__ == '__main__':
    main()

Recommended Posts

Easy Twitter Posting Program
Twitter posting application made with Django
Program for Twitter Trend Analysis (Personal Note)
Program to get favorite images on Twitter