initial commit
This commit is contained in:
		
							
								
								
									
										30
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
Copyright Vanessa McHale (c) 2017
 | 
			
		||||
 | 
			
		||||
All rights reserved.
 | 
			
		||||
 | 
			
		||||
Redistribution and use in source and binary forms, with or without
 | 
			
		||||
modification, are permitted provided that the following conditions are met:
 | 
			
		||||
 | 
			
		||||
    * Redistributions of source code must retain the above copyright
 | 
			
		||||
      notice, this list of conditions and the following disclaimer.
 | 
			
		||||
 | 
			
		||||
    * Redistributions in binary form must reproduce the above
 | 
			
		||||
      copyright notice, this list of conditions and the following
 | 
			
		||||
      disclaimer in the documentation and/or other materials provided
 | 
			
		||||
      with the distribution.
 | 
			
		||||
 | 
			
		||||
    * Neither the name of Vanessa McHale nor the names of other
 | 
			
		||||
      contributors may be used to endorse or promote products derived
 | 
			
		||||
      from this software without specific prior written permission.
 | 
			
		||||
 | 
			
		||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 | 
			
		||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 | 
			
		||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 | 
			
		||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 | 
			
		||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 | 
			
		||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 | 
			
		||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
			
		||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
			
		||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
			
		||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 | 
			
		||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
							
								
								
									
										20
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
# Trump Markov Chain Twitter Bot
 | 
			
		||||
 | 
			
		||||
Written in python with the aid of markovify and python-twitter.
 | 
			
		||||
 | 
			
		||||
Put your API keys in a file (e.g. .api-keys-trump) like so:
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
api-key: API_KEY
 | 
			
		||||
api-sec: API_SECRET_KEY
 | 
			
		||||
tok:     TOKEN
 | 
			
		||||
tok-sec: SECRET_TOKEN
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Then run:
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
python markov.py PATH_TO_API_KEYS --text PATH_TO_CORPUS.txt
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
to send one tweet at a time. 
 | 
			
		||||
							
								
								
									
										47
									
								
								markov.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										47
									
								
								markov.py
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
import markovify
 | 
			
		||||
import twitter
 | 
			
		||||
import itertools
 | 
			
		||||
import argparse
 | 
			
		||||
 | 
			
		||||
## 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 analyze')
 | 
			
		||||
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)
 | 
			
		||||
 | 
			
		||||
## 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
 | 
			
		||||
while selected == False:
 | 
			
		||||
    str_potentially = text_model.make_short_sentence(140)
 | 
			
		||||
    if not("http" in str_potentially):
 | 
			
		||||
        status = api.PostUpdate(str_potentially)
 | 
			
		||||
        print(status.text) # verify it worked
 | 
			
		||||
        selected = True
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3461
									
								
								trumptweets.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3461
									
								
								trumptweets.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user