Created
October 19, 2016 09:05
-
-
Save QuantumFractal/34a27976f20dea49655eb936ec8f0f5d to your computer and use it in GitHub Desktop.
cpre 184x demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import twitter | |
import re | |
import html | |
import sys | |
from secrets import keys | |
from twitter.error import TwitterError | |
regex = re.compile(r"(#[A-Za-z]*)|(http(s|):\/\/[A-Za-z./0-9]*)|(@[A-Za-z]*)") | |
def main(): | |
api = twitter.Api(**keys) | |
if len(sys.argv) > 1: | |
username = sys.argv[1] | |
else: | |
username = "donttrythis" | |
if len(sys.argv) == 3: | |
amount = int(sys.argv[2]) | |
else: | |
amount = 400 | |
print("Trying to collect {} tweets from @{}...".format(amount ,username)) | |
tweets = collect(username, api, amount) | |
print("Collected {} tweets from @{}.".format(len(tweets), username)) | |
def get_tweets(username, api, last=None, count=200): | |
try: | |
raw_tweets = api.GetUserTimeline(screen_name=username, | |
include_rts=False, | |
count=count, | |
max_id=last) | |
return raw_tweets | |
except TwitterError as err: | |
print("Twitter Error! {}".format(err)) | |
def collect(username, api, maximum): | |
try: | |
tweets = get_tweets(username, api, count=maximum) | |
while len(tweets) < maximum: | |
tweets += get_tweets(username, api, last=get_last_id(tweets)) | |
return tweets | |
except TwitterError as err: | |
print("I tried my best!") | |
return tweets | |
def get_last_id(tweets): | |
if len(tweets) > 1: | |
return tweets[-1].id | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import markovify | |
import sys | |
import code | |
def main(): | |
if len(sys.argv) == 2: | |
username = sys.argv[1] | |
else: | |
username = 'donttrythis' | |
try: | |
with open(username+'.txt', encoding="utf-8") as f: | |
text = f.read() | |
except FileNotFoundError: | |
print("Cannot find saved tweets for user: {}".format(username)) | |
return | |
text_model = markovify.Text(text) | |
for i in range(5): | |
print(text_model.make_short_sentence(140)) | |
if '-i' in sys.argv: | |
code.interact(local=locals()) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment