Skip to content

Instantly share code, notes, and snippets.

@joegross
Last active September 14, 2016 03:13
Show Gist options
  • Save joegross/56adef847c8ce310fbcd16586501bddf to your computer and use it in GitHub Desktop.
Save joegross/56adef847c8ce310fbcd16586501bddf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# I tweet for swag
# ./tweet.py --learn
# ./tweet.py --markov
# ./tweet.py --markov --tweet
import argparse
from collections import Counter
from ConfigParser import ConfigParser
import markovify
import twitter
class TweetCorpus(object):
def __init__(self, args):
self.args = args
config = ConfigParser()
config.read('config.ini')
self.api = twitter.Api(
consumer_key=config.get('secrets', 'consumer_key'),
consumer_secret=config.get('secrets', 'consumer_secret'),
access_token_key=config.get('secrets', 'access_token_key'),
access_token_secret=config.get('secrets', 'access_token_secret'),
)
self.corpus = []
self.searches = [
'PDSummit16',
'pagerduty',
'pagerduty devops',
'automated infrastructure',
'infrastructure automation',
'continuous delivery',
'pagerduty site reliability',
'site reliability',
'pagerduty oncall',
'pagerduty slack',
'devops',
]
self.wordcount = Counter()
self.mentions = Counter()
self.corpus_file = 'corpus.txt'
def parse_results(self, results):
for status in results:
with open(self.corpus_file, 'a') as out:
self.corpus.append(status.text)
out.write(status.text.encode("UTF-8"))
out.write('\n'.encode("UTF-8"))
for word in status.text.split():
self.wordcount[word] += 1
for mention in status.user_mentions:
self.mentions[mention.screen_name] += 1
def learn(self):
# statuses = api.GetUserTimeline(screen_name='pagerduty')
for search in self.searches:
print '----%s----' % search
results = self.api.GetSearch(search, count=100, result_type='mixed')
print len(results)
self.parse_results(results)
for (mention, rank) in self.mentions.most_common(50):
print(mention)
results = self.api.GetUserTimeline(screen_name=mention)
self.parse_results(results)
print(len(self.corpus))
print(len(self.wordcount), self.wordcount.most_common(50))
print(len(self.mentions), self.mentions.most_common(50))
def markov(self):
with open(self.corpus_file) as f:
text = f.read()
text_model = markovify.Text(text)
tweets = {}
for i in range(50):
tweet = text_model.make_short_sentence(130)
tweet = tweet.replace('@', '')
tweet = tweet.replace('#', '')
tweet += ' #PDSummit16'
print tweet
if self.args.tweet:
if not tweets.get(tweet):
try:
self.api.PostUpdate(tweet)
except twitter.error.TwitterError:
continue
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--learn', action='store_true')
parser.add_argument('--markov', action='store_true')
parser.add_argument('--tweet', action='store_true')
args = parser.parse_args()
corpus = TweetCorpus(args)
if args.learn:
corpus.learn()
if args.markov:
corpus.markov()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment