This commit is contained in:
Vanessa McHale
2017-03-26 00:29:12 -05:00
parent 1fdfa3c9b9
commit f27aeb5d51
8 changed files with 166 additions and 3525 deletions

22
bin/markovbot Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env python
import markovbot
import twitter
import itertools
import random
# Command line parser
args = markovbot.parser()
# Set the coke binge parameter
if args.coke_binge:
coke_binge_num = random.randint(1, 6)
else:
coke_binge_num = 1
text_model=markovbot.build_model(args.text)
# Make the api keys
api = markovbot.make_api_keys(args.filepath)
markovbot.make_tweets(api, args.test, text_model, coke_binge_num)

View File

@ -0,0 +1,29 @@
import argparse
def parser() =
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--config', dest='filepath', metavar='CREDENTIALS', type=str, help='Path to config file')
parser.add_argument('text', metavar='CORPUS', type=str, help='path to the text you wish to mimic') # filepath should be a flag; corpus an argument!
parser.add_argument('--coke-binge', dest='coke_binge', action="store_true", default=False, help='Tweet excessively/emulate our POTUS') # instead of tweeting once, tweet a random number of times.
parser.add_argument('-t', dest='test', action="store_true", default=False, help='Test text generation without tweeting.') # distance on markov chains?? distance on syntax trees? # default to test or default to tweet??
return parser.parse_args()
# Function to help process credentials file
def drop_tag(str):
parsed = itertools.dropwhile(lambda x: x!= ':', str)
str2 = "".join(parsed)
parsed2 = itertools.dropwhile(lambda x: x == ' ' or x == ':', str2)
return "".join(parsed2)
# Process api keys etc.
def make_api_keys(filepath):
# Open file with credentials
with open(filepath) as f:
content = f.readlines()
# Strip irrelevant text
let content = [drop_tag(x.strip()) for x in content]
# create twitter api object
return twitter.Api(consumer_key=content[0],
consumer_secret=content[1],
access_token_key=content[2],
access_token_secret=content[3])

41
build/scripts-3.5/markovbot Executable file
View File

@ -0,0 +1,41 @@
#!python
import markovify
import markovbot
import twitter
import itertools
import random
# Command line parser
args = parser()
# Set the coke binge parameter
if args.coke_binge:
coke_binge_num = random.randint(1, 6)
else:
coke_binge_num = 1
# Get raw text of markov chain as string.
with open(args.text) as f:
text = f.read()
# Build the model.
text_model = markovify.Text(text)
api = make_api_keys(args.filepath)
# Generate three randomly-generated sentences of no more than 140 characters and tweet them.
selected = False
for i in range(0, coke_binge_num):
while selected is False:
str_potentially = text_model.make_short_sentence(140)
if not("http" in str_potentially):
if not(args.test):
# tweet the generated text
status = api.PostUpdate(str_potentially)
print(status.text) # verify it worked
else:
# test mode; just display it
print(str_potentially)
selected = True
selected = False

View File

@ -1,64 +0,0 @@
import markovify
import twitter
import itertools
import argparse
import random
# Command line parser
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('filepath', metavar='CREDENTIALS', type=str, help='an integer for the accumulator')
parser.add_argument('--text', dest='corpus', metavar='CORPUS', type=str, help='path to the text you wish to mimic') # filepath should be a flag; corpus an argument!
parser.add_argument('--coke-binge', dest='coke_binge', action="store_true", default=False, help='path to the text you wish to mimic') # instead of tweeting once, tweet a random number of times.
parser.add_argument('-t', dest='test', action="store_true", default=False, help='Test text generation without tweeting.') # distance on markov chains?? distance on syntax trees? # default to test or default to tweet??
args = parser.parse_args()
# Function to help process credentials file
def drop_tag(str):
parsed = itertools.dropwhile(lambda x: x!= ':', str)
str2 = "".join(parsed)
parsed2 = itertools.dropwhile(lambda x: x == ' ' or x == ':', str2)
return "".join(parsed2)
# Set the
if args.coke_binge:
coke_binge_num = random.randint(1, 6)
else:
coke_binge_num = 1
# Open file with credentials
with open(args.filepath) as f:
content = f.readlines()
# Process api keys etc.
content = [drop_tag(x.strip()) for x in content]
# Get raw text of markov chain as string.
with open(args.corpus) as f:
text = f.read()
# Build the model.
text_model = markovify.Text(text)
# create twitter api object
api = twitter.Api(consumer_key=content[0],
consumer_secret=content[1],
access_token_key=content[2],
access_token_secret=content[3])
# Generate three randomly-generated sentences of no more than 140 characters and tweet them.
selected = False
for i in range(0, coke_binge_num):
while selected is False:
str_potentially = text_model.make_short_sentence(140)
if not("http" in str_potentially):
if not(args.test):
# tweet the generated text
status = api.PostUpdate(str_potentially)
print(status.text) # verify it worked
else:
# test mode; just display it
print(str_potentially)
selected = True
selected = False

57
markovbot/__init__.py Normal file
View File

@ -0,0 +1,57 @@
import markovify
import argparse
import itertools
import twitter
def parser():
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--config', dest='filepath', metavar='CREDENTIALS', type=str, help='Path to config file')
parser.add_argument('text', metavar='CORPUS', type=str, help='path to the text you wish to mimic') # filepath should be a flag; corpus an argument!
parser.add_argument('--coke-binge', dest='coke_binge', action="store_true", default=False, help='Tweet excessively/emulate our POTUS') # instead of tweeting once, tweet a random number of times.
parser.add_argument('-t', dest='test', action="store_true", default=False, help='Test text generation without tweeting.') # distance on markov chains?? distance on syntax trees?
return parser.parse_args()
def build_model(text):
# Get raw text of markov chain as string.
with open(text) as f:
text = f.read()
# Build the model.
return markovify.Text(text)
def make_tweets(api, test, model, coke_binge_num):
# Generate three randomly-generated sentences of no more than 140 characters and tweet them.
selected = False
for i in range(0, coke_binge_num):
while selected is False:
str_potentially = model.make_short_sentence(140)
if not("http" in str_potentially):
if not(test):
# tweet the generated text
status = api.PostUpdate(str_potentially)
print(status.text) # verify it worked
else:
# test mode; just display it
print(str_potentially)
selected = True
selected = False
# Function to help process credentials file
def drop_tag(str):
parsed = itertools.dropwhile(lambda x: x!= ':', str)
str2 = "".join(parsed)
parsed2 = itertools.dropwhile(lambda x: x == ' ' or x == ':', str2)
return "".join(parsed2)
# Process api keys etc.
def make_api_keys(filepath):
# Open file with credentials
with open(filepath) as f:
content = f.readlines()
# Strip irrelevant text
content = [drop_tag(x.strip()) for x in content]
# create twitter api object
return twitter.Api(consumer_key=content[0],
consumer_secret=content[1],
access_token_key=content[2],
access_token_secret=content[3])

Binary file not shown.

17
setup.py Executable file
View File

@ -0,0 +1,17 @@
from setuptools import setup
setup(name='markovbot',
version='0.1',
description='Make a markov chain based twitter bot',
url='http://github.com/vmchale/markov-bot',
author='Vanessa McHale',
author_email='tmchale@wisc.edu',
license='BSD3',
packages=['markovbot'],
scripts=['bin/markovbot'],
install_requires=[
'gitpython',
'markovify',
'python-twitter',
],
zip_safe=False)

File diff suppressed because it is too large Load Diff