Skip to content

Instantly share code, notes, and snippets.

@nma-io
Created October 2, 2017 17:26
Show Gist options
  • Select an option

  • Save nma-io/ce438fb647acdb9ec2a162b0e7472d87 to your computer and use it in GitHub Desktop.

Select an option

Save nma-io/ce438fb647acdb9ec2a162b0e7472d87 to your computer and use it in GitHub Desktop.
twitter_to_syslog.py
#!/usr/bin/env python
"""Twitter Monitoring Tool."""
import time
import socket
import tweepy
import logging
from config import tw_access_token, tw_access_secret, tw_consumer_key, tw_consumer_secret
# logging.basicConfig(format='%(message)s', level=logging.INFO) # Uncomment to see stuff.
logging.getLogger('requests').setLevel(logging.ERROR)
log = logging.getLogger(__name__)
__author__ = 'Nicholas Albright'
auth = tweepy.OAuthHandler(tw_consumer_key, tw_consumer_secret)
auth.set_access_token(tw_access_token, tw_access_secret)
api = tweepy.API(auth)
queryfile = 'twitmon_queries.txt' # What you want to monitor Twitter for, one query per line
logfile = 'past_tweets.txt'
SYSLOG_IP = '127.0.0.1' # UDP SYSLOG
SYSLOG_PORT = 514
def push(message):
"""Send alert to our system of choice."""
remote = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
remote.sendto(message.rstrip(), (SYSLOG_IP, int(SYSLOG_PORT)))
except Exception as err:
log.exception(err)
if __name__ == '__main__':
past_tweets = open(logfile, 'a+')
tweets_in_ram = past_tweets.read()
getlast = sorted([x.split('\t-=-\t')[1] for x in tweets_in_ram.split('\n') if '-=-' in x])[-1]
last_tweetid = getlast[-1].rstrip() if getlast else '1345678909876543'
while True:
query_text = open(queryfile).read().splitlines()
for query in query_text:
if not query: continue
num_tweets = 5
tl = api.search(query, rpp=num_tweets, since_id=last_tweetid)
for tweet in tl:
url = tweet.entities['urls'][0]['expanded_url']
text = tweet.text
screen_name = tweet._json['user']['screen_name']
newid = tweet.id
if newid >= last_tweetid:
last_tweetid = newid
log.info(newid)
past_tweets.write('{}\t{}\t{}\t-=-\t{}\n'.format(screen_name, text, url, newid))
past_tweets.truncate()
if text not in tweets_in_ram:
tweets_in_ram += ('{}\t{}\t{}\t-=-\t{}\n'.format(screen_name, text, url, newid))
push('[TWEETMON ALERT] twitter_user={user} tweet_text={text} tweet_id={id}'.format(user=screen_name, text=text, id=newid))
time.sleep(900)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment