Sunday, 15 January 2023

console client for Mastodon ?

 I hanker after a console Mastodon client, so initially have tinkered a home timeline reader, bit rough so far. Run with ./tl_json.py 1|less -r  (the -r on less make the ansi colours work) to get the latest, or ./tl_json.py|less -r to read the current tmp file (/tmp/tl.json). 

I imagine a stand alone that you could do likes, and pop-ups in a browser and push I to view jpegs and such, we'll see.  

Getting the timeline could have been done with urllib, but what with urllib, urllib2 and urllib3 all being a bit different, I couldn't get the Authorization in the header to work, so I squibbed it and just used curl.

#============================

#!/usr/bin/python3
import json , os , html2text ,sys

#ansi colours

LIGHT_CYAN="\033[1;36m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
GREEN="\033[0;32m"
PURPLE="\033[0;35m"
LIGHT_GREEN="\033[1;32m"
BOLD="\033[1m"
RED="\033[0;31m"
NORMAL="\033[0m"

APIKEY="you need your own apikey"

masto_url="curl -k https://mastodon.social/api/v1/timelines/home?limit=40 -H 'Authorization: Bearer "+ APIKEY +" ' >/tmp/tl.json"
if len(sys.argv) > 1 :
    print ("Refreshing")
    os.system(masto_url)

f = open("/tmp/tl.json")
d = f.read()
dj = json.loads(d)
z = 0
#clean the text
text_maker = html2text.HTML2Text()
text_maker.images_to_alt = True
text_maker.ignore_links = True

while z < 39 :
    print (LIGHT_CYAN+str(z)+" "+dj[z]['account']['display_name']+" ["+dj[z]['account']['username']+"]")
    print (BLUE+dj[z]['created_at'])
    try:
        text =  dj[z]['content']
        print (NORMAL  + text_maker.handle(text).strip() )
       

        print (PURPLE+" "+dj[z]['url']+" .")
        print (PURPLE+"@"+dj[z]['account']['acct'])
       
    except:
        print("")
    try:     #is there any media ?
         y=0
         while  dj[z]['media_attachments'][y]  :
              print (BOLD+"\nDescription: < "+ dj[z]['media_attachments'][y]['description']+" >"+NORMAL )
              print (PURPLE+" "+ dj[z]['media_attachments'][y]['url']+NORMAL )
              y = y + 1
    except:
         print ("")

    # boosted posts
    try:
        text = dj[z]['reblog']['content']
        print (YELLOW + "Boost "+ dj[z]['reblog']['id'] )
        print (YELLOW +" "+  dj[z]['reblog']['url'] )
        print (YELLOW + text_maker.handle(text).strip() + NORMAL )
        print (" ")
    except:
        print ("")    
    z=z+1

#============================


No comments: