Get delay information on Twitter and tweet

This is the first post.

It's been two months since I met python. It was late to meet. And I'm a beginner in programming, so I'm weak! Code. Please forgive me. I made it because I wanted a prediction before the official announcement such as delay information of each railway line. Just self-satisfaction!

The environment uses python 2 series.

Allow you to tweet from python

If you don't enable tweeting with python first, nothing will start. Please set from Twitter Application and get the key.

#Set the key
CONSUMER_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXX"
CONSUMER_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
ACCESS_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
ACCESS_TOKEN_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

#Create an API instance
api = tweepy.API(auth)

This is the preparation.

Search for applicable words from tweets

First you have to get the tweet. I referred to other posts.

Get 100 tweets

A fragment program that retrieves tweets and outputs them to a file. I don't need to output to a file at all, but I just wanted to do file I / O. Try, except so that there is no error when the characters are garbled. It feels like a beginner.

for tweet in api.search(q=query, count=100):
		try:
			f.write(tweet.text.encode("utf-8"))
			f.write("\n")
		except:
			pass

I am inputting what I output here.

#Codecs open to prevent garbled files
f_in = codecs.open('XXXXXXX.txt','r','utf-8')

lines = f_in.readlines()

Search string

keywords =[u'delay', u'delay']
query = ' OR '.join(keywords)

Get tweets of things that were caught in the OR search for "delay" and "delay" as keywords.

I would like a tweet like this if possible.

** What? In addition, Tato is late, but w delay is the default w **

** Somehow the delay of Toyoko Inn is somehow **

It's a wonderfully useless and good tweet. Such tweets will not eliminate the delay, but they can be used to obtain information.

How many routes to search

In my case, Tokyu is famous for delays, so I will investigate it. We also created a dictionary for each route, including routes that operate directly through each other.

train = {
		u"Tato" : { "count" : 0 , "flag" : 0},
		u"Toyoko" : { "count" : 0 , "flag" : 0},
		u"Oi" : { "count" : 0 , "flag" : 0},
		u"Meguro" : { "count" : 0 , "flag" : 0},
		u"Hanzomon" : { "count" : 0 , "flag" : 0},
		u"Tobu Sky Tree" : { "count" : 0 , "flag" : 0},
		u"Subcenter" : { "count" : 0 , "flag" : 0},
		u"Tojo" : { "count" : 0 , "flag" : 0},
		u"Seibu Ikebukuro" : { "count" : 0 , "flag" : 0},
		u"Mita" : { "count" : 0 , "flag" : 0}
		}

count is the number of route name hits from 100 tweets. For flag, enter 1 when tweeting. (= To avoid continuous tweets)

As those who understand it will understand, the Namboku Line and Saitama Kosoku Railway, which are directly connected to the Meguro Line, have disappeared for some reason. ~~ I omitted it. I thought I wouldn't be so late.

Search for and predict these.

function

I created a function for counting and tweeting. count This function counts the number of route hits from tweets.

def count_tweet(lines, word):
	count = 0
	for line in lines:
		if line.find(word) > -1:
			count = count + 1
	return count

lines is a file input.

train[u"Tato"]["count"] = count_tweet(lines, u"Tato")

Use it like this. tweet A function that lets you tweet.

def tweet(count, word, flag):
	if count > 10 and flag == 0:
		api.update_status(status=str(now.hour)+u"Time"+str(now.minute)+u"Get minute information"+word+u"Possibility of delay")
		flag = 1
	else:
		flag = 0
	return flag

In this function, it is judged that there is a possibility of delay when 11 counts or more.

If I tweet, I set a flag and make sure to jump to else the next time I refer to this function. This will prevent consecutive tweets. I think there is actually a different way ...

train[u"Tato"]["flag"] = tweet(train[u"Tato"]["count"], 	u'Tato', train[u"Tato"]["flag"])

Use it like this.

Execution result

The display will be displayed on the console as well.

(capture.PNG)

The Tobu Sky Tree Line is long, so I chose the Isesaki Line. Tweets will be made on the Tobu Sky Tree Line.

Tweet result

It will be like this. キャプチャ2.PNG

Details

Use sleep in the while loop, stop for 300 seconds (= 5 minutes), and then turn it again.

By putting time in the tweet, we are trying to prevent the same tweet.

I'll write the code here.

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

import tweepy
import codecs
import datetime
from time import sleep

#Set various keys
CONSUMER_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXX"
CONSUMER_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
ACCESS_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
ACCESS_TOKEN_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

#Create an API instance
api = tweepy.API(auth)
#Search content
keywords =[u'delay', u'delay']
query = ' OR '.join(keywords)

#Search content count
def count_tweet(lines, word):
	count = 0
	for line in lines:
		if line.find(word) > -1:
			count = count + 1
	return count

#Tweet judgment
def tweet(count, word, flag):
	if count > 5 and flag == 0:
		api.update_status(status=str(now.hour)+u"Time"+str(now.minute)+u"Get minute information"+word+u"Possibility of delay")
		flag = 1
	else:	
		flag = 0
	return flag

train = {
		u"Tato" : { "count" : 0 , "flag" : 0},
		u"Toyoko" : { "count" : 0 , "flag" : 0},
		u"Oi" : { "count" : 0 , "flag" : 0},
		u"Meguro" : { "count" : 0 , "flag" : 0},
		u"Hanzomon" : { "count" : 0 , "flag" : 0},
		u"Tobu Sky Tree" : { "count" : 0 , "flag" : 0},
		u"Subcenter" : { "count" : 0 , "flag" : 0},
		u"Tojo" : { "count" : 0 , "flag" : 0},
		u"Seibu Ikebukuro" : { "count" : 0 , "flag" : 0},
		u"Mita" : { "count" : 0 , "flag" : 0}
		}

while 1:
	f = open('XXXXXX.txt','w')
	print "--------------------------------------------------------------------------------"
	#Get the current time
	now = datetime.datetime.today()
	
	#Display of current time
	print(str(now.hour)+u'Time'+str(now.minute)+u'Minutes'+str(now.second)+u'Get second information')
	
	#File input 100 tweets about the latest query search
	for tweet in api.search(q=query, count=100):
		try:
			f.write(tweet.text.encode("utf-8"))
			f.write("\n")
		except:
			pass
	f.close()

	#Codecs open to prevent garbled files
	f_in = codecs.open('XXXXXX.txt','r','utf-8')
	#Get line by line
	lines = f_in.readlines()

	train[u"Tato"]["count"] = count_tweet(lines, u"Tato")
	train[u"Toyoko"]["count"] = count_tweet(lines, u"Toyoko")
	train[u"Oi"]["count"] = count_tweet(lines, u"Oi")
	train[u"Meguro"]["count"] = count_tweet(lines, u"Meguro")
	train[u"Hanzomon"]["count"] = count_tweet(lines, u"Hanzomon")
	train[u"Tobu Sky Tree"]["count"] = count_tweet(lines, u"Tobu Sky Tree")
	train[u"Subcenter"]["count"] = count_tweet(lines, u"Subcenter")
	train[u"Tojo"]["count"] = count_tweet(lines, u"Tojo")
	train[u"Seibu Ikebukuro"]["count"] = count_tweet(lines, u"Seibu Ikebukuro")
	train[u"Mita"]["count"] = count_tweet(lines, u"Mita")


	print(u'Denen-toshi Line:'+ str(train[u"Tato"]["count"])    +' counts')
	print(u'Toyoko Line:'+ str(train[u"Toyoko"]["count"])    +' counts')
	print(u'Oimachi Line:'+ str(train[u"Oi"]["count"])    +' counts')
	print(u'Meguro Line:'+ str(train[u"Meguro"]["count"])    +' counts')
	print(u'Hanzomon Line:'+ str(train[u"Hanzomon"]["count"])  +' counts')
	print(u'Tobu Isesaki Line:'+ str(train[u"Tobu Sky Tree"]["count"])+' counts')
	print(u'Fukutoshin Line:'+ str(train[u"Subcenter"]["count"])  +' counts')
	print(u'Tobu Tojo Line:'+ str(train[u"Tojo"]["count"])    +' counts')
	print(u'Seibu Ikebukuro Line:'+ str(train[u"Seibu Ikebukuro"]["count"]) +' counts')
	print(u'Toei Mita Line:'+ str(train[u"Mita"]["count"])    +' counts')


	train[u"Tato"]["flag"] = tweet(train[u"Tato"]["count"], 	u'Tato', 	train[u"Tato"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Toyoko"]["count"], 	u'Toyoko', 	train[u"Toyoko"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Oi"]["count"], 	u'Oi町', 	train[u"Oi"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Meguro"]["count"], 	u'Meguro線', 	train[u"Meguro"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Hanzomon"]["count"],	u'Hanzomon', 	train[u"Hanzomon"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Tobu Sky Tree"]["count"], u'Tobu Sky Treeライン', train[u"Tobu Sky Tree"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Subcenter"]["count"], 	u'Vice capital', 	train[u"Subcenter"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Tojo"]["count"], 	u'Tojo', 	train[u"Tojo"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Seibu Ikebukuro"]["count"], u'Seibu Ikebukuro', train[u"Meguro"]["flag"])
	train[u"Tato"]["flag"] = tweet(train[u"Mita"]["count"], 	u'Mita', 	train[u"Meguro"]["flag"])
	
	
	print "--------------------------------------------------------------------------------"
	f_in.close()
	sleep(300)
print("finish.")

Summary

There are various factors in railway delays. If you do not drive due to personal injury, signal trouble, natural disaster, etc., the timetable will be disturbed for a long time. I'm not thinking about this.

It's quite possible that you're 10 minutes late in the morning rush hour, and if you notice it at home rather than after arriving at the station, your mind will be much different. It is absolutely impossible for us to return to the regular timetable that the train is late. What you can do is what you do. Universal truth.

I have this program running on my Raspberry Pi 2 at home all the time.

I wrote it for a long time, but this is the end. If you have any mistakes or suggestions, please leave a comment.

Recommended Posts

Get delay information on Twitter and tweet
Get celebrity tweet history from twitter
Get only image tweets on twitter
[Memo] Tweet on twitter with python
Get Twitter bookmarks on CentOS using Selenium
Program to get favorite images on Twitter
See file and folder information on python
Get images from specific users on Twitter
Use twitter API (API account registration and tweet acquisition)
twitter on python3
Get Twitter Trends
Get information on the 100 most influential tech Twitter users in the world with python.
Get Twitter userData
delays --Information on the various kernel delay / sleep mechanisms
Steps to measure coverage and get badges on codecov.io
Automatic follow on Twitter with python and selenium! (RPA)
Get comments on youtube Live with [python] and [pytchat]!
Tweet the triple forecast of the boat race on Twitter
Get twitter tweets, followers, followers, likes, with urllib and beautifulSoup
[Python] Get user information and article information with Qiita API
[Linux] Get Linux resource information
Character count on Twitter
[google-oauth] Get user information
[Go language] Use OpenWeatherMap and Twitter API to regularly tweet weather information from Raspberry Pi
Web scraping of comedy program information and notification on LINE
Beginners get Qiita tag information and visualize and consider the TOP10.
I tried to get Web information using "Requests" and "lxml"
Streamline information gathering with the Twitter API and Slack bots
Beginners of Google Maps API and Twitter API made "tweet map"
Collecting information from Twitter with Python (MySQL and Python work together)
[Python x Zapier] Get alert information and notify with Slack