Browse Source

extended tweet parsing, parser fixes, cleanup

We can now parse and dump the entire 280 characters of a tweet!

Also added: a -d flag for running the bot in debug mode.
pull/1/head
Albert Sanchez 7 months ago
parent
commit
b95e56dd0d
  1. 13
      pseudbot/cli.py
  2. 50
      pseudbot/tweet_bot.py
  3. 11
      pseudbot/util.py

13
pseudbot/cli.py

@ -19,6 +19,12 @@ def parse_args(args: [str], name: str):
+ 'Has no affect unless "action" is meant to be directed at a specific '
+ "ID.",
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
help="Run Pseudbot in debug mode.",
)
parser.add_argument(
"-s",
"--screen-name",
@ -66,9 +72,9 @@ def main(args: [str], name: str) -> int:
tcfg = j.loads(opts.cfg_json.read())
if opts.action == "run_bot":
pb = PseudBot(tcfg=tcfg, proxy_url=opts.proxy_url)
pb = PseudBot(tcfg=tcfg, proxy_url=opts.proxy_url, debug=opts.debug)
elif opts.action == "list_actions":
pb = PseudBot(tcfg=tcfg, quiet=True)
pb = PseudBot(tcfg=tcfg, quiet=True, debug=opts.debug)
elif opts.action in ("pasta_tweet", "dump_tweet"):
if opts.reply_to_id is not None:
pb = PseudBot(
@ -76,6 +82,7 @@ def main(args: [str], name: str) -> int:
custom_welcome=custom_welcome,
last_id=opts.reply_to_id,
proxy_url=opts.proxy_url,
debug=opts.debug,
)
else:
print(
@ -91,6 +98,7 @@ def main(args: [str], name: str) -> int:
custom_welcome=custom_welcome,
target_screen_name=opts.screen_name,
proxy_url=opts.proxy_url,
debug=opts.debug,
)
else:
print(
@ -103,5 +111,6 @@ def main(args: [str], name: str) -> int:
tcfg=tcfg,
custom_welcome=custom_welcome,
proxy_url=opts.proxy_url,
debug=opts.debug,
)
callm(pb, opts.action)

50
pseudbot/tweet_bot.py

@ -10,7 +10,13 @@ import typing
from .exceptions import *
from .media import MEDIA
from .pastas import PASTAS
from .util import get_timestamp_s, jdump, log_t_by_sname, surl_prefix
from .util import (
get_timestamp_s,
jdump,
get_tweet_text,
log_t_by_sname,
surl_prefix,
)
class PseudBot:
@ -24,7 +30,9 @@ class PseudBot:
target_screen_name: str = None,
proxy_url: str = None,
quiet: bool = False,
debug: bool = False,
):
self.debug = debug
tauth = t.OAuthHandler(tcfg["consumer"], tcfg["consumer_secret"])
tauth.set_access_token(tcfg["tok"], tcfg["tok_secret"])
@ -76,15 +84,6 @@ class PseudBot:
def _log_tweet(self, msg, tstat) -> None:
print('[TWEET]: "{}" ({})'.format(msg, self.url_prefix + str(tstat.id)))
def _log_tweet_by_sname(self, tweet):
print(
'[@{}]: "{}" ({})'.format(
tweet.user.screen_name,
tweet.text,
surl_prefix(tweet.user.screen_name) + str(tweet.id),
)
)
def _check_sname_exists(self):
if self.target_screen_name is None:
raise PseudBotNoTargetScreenName
@ -103,6 +102,7 @@ class PseudBot:
self.tapi.user_timeline,
screen_name=self.target_screen_name,
since_id=1,
tweet_mode="extended",
).items():
log_t_by_sname(tweet)
tweets_j.append(tweet._json)
@ -122,7 +122,7 @@ class PseudBot:
"""
Get and dump recent tweets from your Pseudbot account's home timeline.
"""
home_tl = self.tapi.home_timeline()
home_tl = self.tapi.home_timeline(tweet_mode="extended")
jsons = []
for tweet in home_tl:
jsons.append(tweet._json)
@ -211,7 +211,9 @@ class PseudBot:
tweets_j = []
for tweet in t.Cursor(
self.tapi.mentions_timeline, since_id=start_id
self.tapi.mentions_timeline,
since_id=start_id,
tweet_mode="extended",
).items():
if tweet.user.screen_name == self.screen_name:
continue
@ -233,7 +235,9 @@ class PseudBot:
Dump the JSON data dictionary of a specific tweet.
If called from the CLI, requires ``-i`` to be set.
"""
tweets = self.tapi.lookup_statuses([self.last_id])
tweets = self.tapi.lookup_statuses(
[self.last_id], tweet_mode="extended"
)
jtweets = []
for tweet in tweets:
log_t_by_sname(tweet)
@ -251,9 +255,11 @@ class PseudBot:
pasta = random.choice(PASTAS)
print("[INFO]: Replying to {}...".format(self.last_id))
tweets = self.tapi.lookup_statuses([self.last_id])
tweets = self.tapi.lookup_statuses(
[self.last_id], tweet_mode="extended"
)
for tweet in tweets:
self._send_pasta_chain(tweet)
self._parse_mention(tweet)
def _get_reply_parent(self, tweet) -> (int, str):
if tweet.in_reply_to_screen_name is not None:
@ -285,8 +291,16 @@ class PseudBot:
"""
(parent_id, parent_screen_name) = self._get_reply_parent(tweet)
for command_string in tweet.text.split("|"):
text = get_tweet_text(tweet)
for command_string in text.split("|"):
words = re.split(r'[\s.;():"]+', command_string)
if len(words[-1]) < 1:
words.pop()
if self.debug is True:
print(words)
media = []
do_pasta = True
@ -338,7 +352,9 @@ class PseudBot:
copypasta chain.
"""
for tweet in t.Cursor(
self.tapi.mentions_timeline, since_id=self.last_id
self.tapi.mentions_timeline,
since_id=self.last_id,
tweet_mode="extended",
).items():
if tweet.user.screen_name == self.screen_name:
continue

11
pseudbot/util.py

@ -31,11 +31,20 @@ def jdump(itms, echo: bool = False, extra_tag: str = None):
df.close()
def get_tweet_text(tweet):
if tweet.retweeted is True:
text = tweet.retweeted_status.full_text
else:
text = tweet.full_text
return text
def log_t_by_sname(tweet):
print(
'[@{}]: "{}" ({})'.format(
tweet.user.screen_name,
tweet.text,
get_tweet_text(tweet),
surl_prefix(tweet.user.screen_name) + str(tweet.id),
)
)

Loading…
Cancel
Save