diff --git a/screenless/bureau/publicrelations/publicrelations.py b/screenless/bureau/publicrelations/publicrelations.py index 99dc3e2..e0b6974 100644 --- a/screenless/bureau/publicrelations/publicrelations.py +++ b/screenless/bureau/publicrelations/publicrelations.py @@ -8,6 +8,7 @@ import urllib from escpos import printer import facebook +from mastodon import Mastodon import PIL import requests import twitter @@ -48,6 +49,17 @@ class PublicRelations(Bureau): self.auth = twitter.OAuth(oauth_token, oauth_secret, CK, CS) self.t.t = twitter.Twitter(auth=self.auth) + MASTO_CREDS = os.path.expanduser('~/.screenless/masto_creds') + if not os.path.exists(MASTO_CREDS): + Mastodon.create_app('screenless', + api_base_url='https://mastodon.social', + to_file=MASTO_CREDS) + self.masto = Mastodon(client_id=MASTO_CREDS, + api_base_url='https://mastodon.social') + masto_user = self.config["mastodon"]["user"] + masto_pass = self.config["mastodon"]["password"] + self.masto.log_in(masto_user, masto_pass) + # setup DBs to map short codes to tweet ids self.tweetdb = self.dbenv.open_db(b"tweetdb") @@ -206,8 +218,10 @@ class PublicRelations(Bureau): """ Reply to a tweet. """ - # TODO: look up tweet id - #self.t.t.statuses.update(status="@AUTHOR_ID", media_ids=id_img1) + shortcode, _ = data.split(".") + tweet_id = self.get_tweet_id(shortcode) + # TODO: do the actual reply + # maybe this should be an optional arg for tweet_pic? return @add_command("twlk", "Like a Tweet") @@ -219,6 +233,87 @@ class PublicRelations(Bureau): tweet_id = self.get_tweet_id(shortcode) self.t.t.favorites.create(_id=tweet_id) + @add_command("mare", "Boost a toot") + def boost_toot(self, data): + """ + Boost a toot (or whatever kind of Fediverse content) + """ + shortcode, _ = data.split(".") + toot_id = self.get_toot_id(shortcode) + self.masto.status_reblog(toot_id) + + @add_command("mafv", "Favorite a toot") + def fav_toot(self, data): + """ + favorite a toot (or other kind of Fediverse content) + """ + shortcode, _ = data.split(".") + toot_id = self.get_toot_id(shortcode) + self.masto.status_favorite(toot_id) + + @add_command("tootpic", "Post a Document Camera Image to the Fediverse") + def toot_pic(self): + """ + Takes a photograph using the document camera and posts it to the Fediverse (Mastodon, Pleroma, etc.) + """ + photo = self.send("PX", "photo")["photo"] + media = self.masto.media_post(photo) + post = self.masto.status_post("", media_ids=[media]) + self.debug(post) + + @add_command("tootline", "Print Recent Toots") + def tootline(self, data=None): + """ + Print some recent entries from your Fediverse timeline. Default is 10. + """ + if data: + try: + count = data["count"] + except KeyError as err: + print("You need to specify how many toots you want!") + else: + count = 10 + + prn = printer.Usb(0x416, 0x5011, in_ep=0x81, out_ep=0x03) + prn.codepage = "cp437" + + # TODO: add fancier formatting i.e. inverted text for username/handle + toots = self.masto.timeline(limit=count) + out = "" + for t in toots: + prn.set(text_type="U") + username = t.account.name.encode("cp437", "ignore") + prn._raw(username) + prn.text("\r\n") + prn.set(text_type="NORMAL") + ttext = html.unescape(t.content) + t_wrapped = textwrap.fill(ttext, width=48) + "\r\n" + t_enc = t_wrapped.encode("cp437", "ignore") + prn._raw(t_enc) + + if len(t.media_attachments) > 0: + img = None + t.media_attachments.reverse() + for media in t.media_attachments: + if media.type == "image": + img = media + if img: + filename = img.url.rsplit('/',1)[1] + filename = "/tmp/" + filename + print("fetching", img.url, filename) + urllib.request.urlretrieve(img.url, filename) + im = PIL.Image.open(filename) + if im.mode in ("L", "RGB", "P"): + im = PIL.ImageOps.equalize(im) + im.thumbnail((576, 576), PIL.Image.ANTIALIAS) + prn.image(im, impl="bitImageColumn") + + tw_shortcode = self.short_tweet_id(t.id) + prn.barcode("PRmad." + tw_shortcode, "CODE128", function_type="B") + prn.text("\r\n\r\n") + + prn.cut() + def main(): pr = PublicRelations()