2017-04-19 12:47:30 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import sys
|
2017-05-08 07:09:20 +00:00
|
|
|
import re
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2018-06-07 08:27:11 +00:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from datetime import datetime
|
|
|
|
from itertools import chain
|
|
|
|
from itertools import zip_longest
|
|
|
|
from textwrap import wrap, TextWrapper
|
|
|
|
|
2019-02-14 14:47:40 +00:00
|
|
|
from toot.utils import format_content, get_text
|
|
|
|
from toot.wcstring import pad
|
|
|
|
|
2017-12-29 13:26:40 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
START_CODES = {
|
2017-12-30 12:14:37 +00:00
|
|
|
'red': '\033[31m',
|
|
|
|
'green': '\033[32m',
|
|
|
|
'yellow': '\033[33m',
|
|
|
|
'blue': '\033[34m',
|
2017-05-08 07:09:20 +00:00
|
|
|
'magenta': '\033[35m',
|
2017-12-30 12:14:37 +00:00
|
|
|
'cyan': '\033[36m',
|
2017-05-08 07:09:20 +00:00
|
|
|
}
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
END_CODE = '\033[0m'
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
START_PATTERN = "<(" + "|".join(START_CODES.keys()) + ")>"
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
END_PATTERN = "</(" + "|".join(START_CODES.keys()) + ")>"
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
def start_code(match):
|
|
|
|
name = match.group(1)
|
|
|
|
return START_CODES[name]
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
def colorize(text):
|
|
|
|
text = re.sub(START_PATTERN, start_code, text)
|
|
|
|
text = re.sub(END_PATTERN, END_CODE, text)
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
return text
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
def strip_tags(text):
|
|
|
|
text = re.sub(START_PATTERN, '', text)
|
|
|
|
text = re.sub(END_PATTERN, '', text)
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
return text
|
2017-04-19 12:47:30 +00:00
|
|
|
|
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
USE_ANSI_COLOR = "--no-color" not in sys.argv
|
2018-06-15 07:02:19 +00:00
|
|
|
QUIET = "--quiet" in sys.argv
|
2017-04-19 12:47:30 +00:00
|
|
|
|
2017-05-08 07:09:20 +00:00
|
|
|
|
|
|
|
def print_out(*args, **kwargs):
|
2018-06-15 07:02:19 +00:00
|
|
|
if not QUIET:
|
|
|
|
args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
|
|
|
|
print(*args, **kwargs)
|
2017-05-08 07:09:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_err(*args, **kwargs):
|
|
|
|
args = ["<red>{}</red>".format(a) for a in args]
|
|
|
|
args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
2017-12-29 13:26:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_instance(instance):
|
|
|
|
print_out("<green>{}</green>".format(instance['title']))
|
|
|
|
print_out("<blue>{}</blue>".format(instance['uri']))
|
|
|
|
print_out("running Mastodon {}".format(instance['version']))
|
|
|
|
print_out("")
|
|
|
|
|
|
|
|
description = instance['description'].strip()
|
|
|
|
if not description:
|
|
|
|
return
|
|
|
|
|
|
|
|
lines = [line.strip() for line in format_content(description) if line.strip()]
|
|
|
|
for line in lines:
|
|
|
|
for l in wrap(line.strip()):
|
2018-06-15 07:02:19 +00:00
|
|
|
print_out(l)
|
|
|
|
print_out()
|
2017-12-29 13:42:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_account(account):
|
|
|
|
print_out("<green>@{}</green> {}".format(account['acct'], account['display_name']))
|
|
|
|
|
|
|
|
note = get_text(account['note'])
|
|
|
|
|
|
|
|
if note:
|
|
|
|
print_out("")
|
|
|
|
print_out("\n".join(wrap(note)))
|
|
|
|
|
|
|
|
print_out("")
|
|
|
|
print_out("ID: <green>{}</green>".format(account['id']))
|
|
|
|
print_out("Since: <green>{}</green>".format(account['created_at'][:19].replace('T', ' @ ')))
|
|
|
|
print_out("")
|
|
|
|
print_out("Followers: <yellow>{}</yellow>".format(account['followers_count']))
|
|
|
|
print_out("Following: <yellow>{}</yellow>".format(account['following_count']))
|
|
|
|
print_out("Statuses: <yellow>{}</yellow>".format(account['statuses_count']))
|
|
|
|
print_out("")
|
|
|
|
print_out(account['url'])
|
|
|
|
|
|
|
|
|
|
|
|
def print_search_results(results):
|
|
|
|
accounts = results['accounts']
|
|
|
|
hashtags = results['hashtags']
|
|
|
|
|
|
|
|
if accounts:
|
|
|
|
print_out("\nAccounts:")
|
|
|
|
for account in accounts:
|
|
|
|
print_out("* <green>@{}</green> {}".format(
|
|
|
|
account['acct'],
|
|
|
|
account['display_name']
|
|
|
|
))
|
|
|
|
|
|
|
|
if hashtags:
|
|
|
|
print_out("\nHashtags:")
|
|
|
|
print_out(", ".join(["<green>#{}</green>".format(t) for t in hashtags]))
|
|
|
|
|
|
|
|
if not accounts and not hashtags:
|
|
|
|
print_out("<yellow>Nothing found</yellow>")
|
2018-06-07 08:27:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_timeline(items):
|
|
|
|
def _print_item(item):
|
|
|
|
def wrap_text(text, width):
|
|
|
|
wrapper = TextWrapper(width=width, break_long_words=False, break_on_hyphens=False)
|
|
|
|
return chain(*[wrapper.wrap(l) for l in text.split("\n")])
|
|
|
|
|
|
|
|
def timeline_rows(item):
|
2018-06-12 09:36:24 +00:00
|
|
|
display_name = item['account']['display_name']
|
|
|
|
username = "@" + item['account']['username']
|
2018-06-07 08:27:11 +00:00
|
|
|
time = item['time'].strftime('%Y-%m-%d %H:%M%Z')
|
|
|
|
|
2018-06-12 09:36:24 +00:00
|
|
|
left_column = [display_name]
|
|
|
|
if display_name != username:
|
|
|
|
left_column.append(username)
|
|
|
|
left_column.append(time)
|
|
|
|
if item['reblogged']:
|
|
|
|
left_column.append("Reblogged @{}".format(item['reblogged']))
|
2018-06-07 08:27:11 +00:00
|
|
|
|
2019-01-19 17:38:17 +00:00
|
|
|
if item['reply_to_toot'] is not None:
|
|
|
|
left_column.append('[RE]')
|
|
|
|
|
|
|
|
left_column.append("id: {}".format(item['id']))
|
|
|
|
|
2018-06-12 09:36:24 +00:00
|
|
|
right_column = wrap_text(item['text'], 80)
|
2018-06-07 08:27:11 +00:00
|
|
|
|
|
|
|
return zip_longest(left_column, right_column, fillvalue="")
|
|
|
|
|
|
|
|
for left, right in timeline_rows(item):
|
2019-01-02 12:22:06 +00:00
|
|
|
print_out("{} │ {}".format(pad(left, 30), right))
|
2018-06-07 08:27:11 +00:00
|
|
|
|
|
|
|
def _parse_item(item):
|
|
|
|
content = item['reblog']['content'] if item['reblog'] else item['content']
|
2018-06-12 09:36:24 +00:00
|
|
|
reblogged = item['reblog']['account']['username'] if item['reblog'] else None
|
2018-06-07 08:27:11 +00:00
|
|
|
|
2019-01-01 21:55:49 +00:00
|
|
|
soup = BeautifulSoup(content.replace(''', "'"), "html.parser")
|
|
|
|
text = soup.get_text()
|
2018-06-07 08:27:11 +00:00
|
|
|
time = datetime.strptime(item['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
|
|
|
|
|
return {
|
2019-01-19 17:38:17 +00:00
|
|
|
"id": item['id'],
|
2018-06-12 09:36:24 +00:00
|
|
|
"account": item['account'],
|
2018-06-07 08:27:11 +00:00
|
|
|
"text": text,
|
|
|
|
"time": time,
|
|
|
|
"reblogged": reblogged,
|
2019-01-19 17:38:17 +00:00
|
|
|
"reply_to_toot": item['in_reply_to_id']
|
2018-06-07 08:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print_out("─" * 31 + "┬" + "─" * 88)
|
|
|
|
for item in items:
|
|
|
|
_print_item(_parse_item(item))
|
|
|
|
print_out("─" * 31 + "┼" + "─" * 88)
|