Created
February 6, 2019 06:42
-
-
Save bucknerns/acd5323c0d6711a150ec80e797a42a55 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
""" | |
Copyright <2019> <Danny Antaki> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
of the Software, and to permit persons to whom the Software is furnished to do | |
so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
""" | |
__version__ = "0.0.1" | |
import argparse | |
import datetime | |
import json | |
import os | |
import sys | |
import textwrap | |
import tweepy | |
def access_twitter(conf=None): | |
""" | |
requires a JSON file with your API keys | |
logins into the API and returns the tweepy API object | |
""" | |
with open(conf, "r") as f: | |
data = json.load(f) | |
auth = tweepy.OAuthHandler(data["consumer_key"], data["consumer_secret"]) | |
auth.set_access_token(data["access_token"], data["access_token_secret"]) | |
api = tweepy.API(auth, wait_on_rate_limit_notify=True) | |
return api | |
def load_history(): | |
with open("tweets/marcus_aurelius.log", "r") as f: | |
return [l.rstrip() for l in f] | |
def write_history(tweets): | |
with open("tweets/marcus_aurelius.log", "a") as f: | |
f.write("\n".join(tweets)) | |
f.write("\n") | |
def check_tweet(tweets, log): | |
return any(t in log for t in tweets) | |
def get_tweets(users, api, n_tweets): | |
usr_twt = {} | |
ln = "_" * 30 | |
ts = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") | |
ofh = "tweets/maurelius.{0}.tweets.txt".format(ts) | |
with open(ofh, "a") as out: | |
sys.stdout.write("\n{}\nCollecting tweets...\n".format(ln)) | |
for u in users: | |
sys.stdout.write(" {0}\n".format(u)) | |
tweets = api.user_timeline( | |
screen_name=u, count=n_tweets, tweet_mode="extended") | |
for tw in tweets: | |
usr_twt.setdefault(tw.id, []).append(tw.full_text) | |
for wrd in tw.full_text.split(" "): | |
out.write("{0}\t@{1}\t{2}\n".format(tw.id, u, wrd)) | |
sys.stdout.write("\n{0}\n".format(ln)) | |
return ofh, usr_twt | |
def load_tweets(fh): | |
kw_dict, twt_dict, tid_dict = {}, {}, {} | |
with open(fh) as f: | |
for l in f: | |
ind, kw, _id, tw = l.rstrip().split("\t") | |
kw_dict[kw] = _id | |
twt_dict.setdefault(ind, []).append(tw) | |
tid_dict.setdefault(kw, []).append(ind) | |
return kw_dict, twt_dict, tid_dict | |
def pick_tweets(tid, twt_dict, tw_buff): | |
ln = "_" * 45 | |
sys.stdout.write( | |
"\nNumber of Tweets: {}\nDisplaying first {} tweets...\n\n".format( | |
len(tid), tw_buff)) | |
chosen = "y" | |
for i, e in enumerate(tid): | |
if chosen in tid: | |
break | |
if i == 0: | |
i += 1 | |
if i % tw_buff == 0: | |
chosen = input( | |
"Please select tweet according to tweet number...\n") | |
if chosen not in tid: | |
ask = input(( | |
"\n{0} Not a correct tweet number.\n " | |
"Display more tweets? [y/n]\n").format(chosen)) | |
if ask and ask != "y": | |
sys.stdout.write("\nExiting!\n") | |
sys.exit(0) | |
tws = " * ".join(twt_dict[e]) | |
sys.stdout.write("\n{0}\n{1}\n{2}\n{0}\n".format( | |
ln, e, textwrap.fill(tws, 45))) | |
if chosen not in tid: | |
chosen = input("Please select tweet according to tweet number...\n") | |
if chosen not in tid: | |
sys.stdout.write( | |
"{0} Not a correct tweet number.\nExiting!\n".format(chosen)) | |
sys.exit(0) | |
return chosen | |
def show_keywords(kws): | |
ln = "_" * 60 | |
qn = "_" * 40 | |
sys.stdout.write( | |
"KEYWORDS\n{0}\n{1}\n{0}\n".format( | |
ln, textwrap.fill(" * ".join(kws), 60))) | |
chosen_kw = input("\n{0}\nPick a keyword... \n{0}\n".format(qn)) | |
if chosen_kw not in kws: | |
sys.stderr.write("ERROR: {0} is not a keyword!\nExiting!\n".format( | |
chosen_kw)) | |
sys.exit(1) | |
return chosen_kw | |
def tweet_it(tweets, _id): | |
ln = "_" * 45 | |
write_history(tweets) | |
sys.stdout.write("\n{0}\nT W E E T I N G ...\n".format(ln)) | |
last_status = None | |
for tweet in tweets: | |
sys.stdout.write("\n{0}\n{1}\n{0}\n".format( | |
ln, textwrap.fill(tweet, 45))) | |
if last_status is None: | |
last_status = api.update_status( | |
status=tweet, in_reply_to_status_id=_id) | |
else: | |
last_status = api.update_status( | |
status=tweet, in_reply_to_status_id=last_status.id) | |
sys.stdout.write("\n") | |
if __name__ == "__main__": | |
usage = r""" | |
___ ____ ______ _____ _____ ______ _____ ______ | |
/ _ )/ __ \/_ __/ / _ | | / / _ \/ __/ / / _/ | / / __/ | |
/ _ / /_/ / / / / __ | |/ / , _/ _// /___/ / | |/ /\ \ | |
/____/\____/ /_/ /_/ |_|___/_/|_/___/____/___/ |___/___/ | |
----------------------------------------------------------- | |
Version {0} | |
Author: Danny Antaki dantaki at ucsd dot edu | |
python botaur.py -u <user> -n <n_tweets> | |
-b <n_display>""".format(__version__) | |
parser = argparse.ArgumentParser(usage=usage) | |
mining = parser.add_argument_group("mining arguments") | |
mining.add_argument( | |
"-u", default=[], action="append", required=True, | |
help="users, flag can be passed multiple times") | |
mining.add_argument( | |
"-n", type=int, default=1, required=False, | |
help="number of tweets to mine. default: 1") | |
display = parser.add_argument_group("display arguments") | |
display.add_argument( | |
"-b", type=int, default=3, required=False, | |
help="number of tweets to display. default: 3") | |
args = parser.parse_args() | |
users, n_tweets, tw_buff = args.u, args.n, args.b | |
ln = "-" * 60 | |
qn = "_" * 40 | |
log = load_history() | |
api = access_twitter("tweet.json") | |
ofh, usr_twt = get_tweets(users, api, n_tweets) | |
sys.stdout.write( | |
"\n{0}\n M E D I T A T I N G \n{0}\n" | |
.format(ln)) | |
t = os.system("perl parse_tweets.pl {0}".format(ofh)) | |
twts = ofh.replace(".txt", ".meditations.txt") | |
kw_dict, twt_dict, tid_dict = load_tweets(twts) | |
kws = sorted(list(set(kw_dict.keys()))) | |
chosen_kw = show_keywords(kws) | |
while chosen_kw in kw_dict: | |
sys.stdout.write( | |
"\n{0}\nResponding to ...\n\n{1}\n".format(qn, textwrap.fill( | |
"".join(usr_twt[int(kw_dict[chosen_kw])]), 30))) | |
cont = input("\n\nContinue? [y/n]\n{}\n".format(qn)) | |
while cont == "n": | |
chosen_kw = show_keywords(kws) | |
sys.stdout.write("\n{0}\nResponding to ...\n{1}\n{0}\n".format( | |
qn, textwrap.fill( | |
"".join(usr_twt[int(kw_dict[chosen_kw])]), 30))) | |
cont = input("\n{0}\nContinue? [y/n]\n{0}\n".format(qn)) | |
tweets = twt_dict[pick_tweets( | |
sorted(set(tid_dict[chosen_kw])), twt_dict, tw_buff)] | |
if check_tweet(tweets, log): | |
sys.stdout.write( | |
"ERROR: {0}\n\nhas been tweeted before!\nSkipping... ".format( | |
textwrap.fill(" ".join(tweets), 30))) | |
else: | |
tweet_it(tweets, kw_dict[chosen_kw]) | |
ask = input( | |
"\n{0}\nTweet again with the same keyword? [y/n]\n{0}\n" | |
.format(qn)) | |
while ask == "y" or ask == "": | |
tweets = twt_dict[pick_tweets( | |
sorted(set(tid_dict[chosen_kw])), twt_dict, tw_buff)] | |
if check_tweet(tweets, log): | |
sys.stdout.write( | |
"ERROR: {0}\n\nhas been tweeted before!\nSkipping... " | |
.format(textwrap.fill(" ".join(tweets), 30))) | |
else: | |
tweet_it(tweets, kw_dict[chosen_kw]) | |
ask = input( | |
"\n{0}\nTweet again with the same keyword? [y/n]\n{0}\n" | |
.format(qn)) | |
ask = input("\n{0}\nPick a new keyword? [y/n]\n{0}\n".format(qn)) | |
if ask == "y" or ask == "": | |
chosen_kw = show_keywords(kws) | |
else: | |
sys.stdout.write("\nExiting!\n") | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment