rrl (1602B)
1 #!/usr/bin/env python 2 # ██ 3 # ░██ 4 # ██████ ██████ ░██ 5 # ░░██░░█░░██░░█ ░██ 6 # ░██ ░ ░██ ░ ░██ 7 # ░██ ░██ ░██ 8 # ░███ ░███ ███ 9 # ░░░ ░░░ ░░░ 10 # random readlist 11 12 import requests 13 import random 14 from subprocess import check_output 15 import argparse 16 17 AUTH_TOKEN = check_output("pass websites/linkding/api_token", shell=True).splitlines()[0].decode("utf-8") 18 LINKDING_URL = "http://linkding" 19 LINKDING_PORT = "9090" 20 ENDPOINT = "/api/bookmarks" 21 QUERY = "?q=!unread+%23readlist+" 22 23 api_url = LINKDING_URL + ":" + LINKDING_PORT + ENDPOINT 24 25 # mark item as read 26 # requires 'id' 27 def mark_read(id): 28 requests.patch(api_url + '/481/', {'unread': False}, headers={'Authorization': 'Token ' + AUTH_TOKEN}) 29 30 # select one random url 31 # from unread list 32 def random_unread(): 33 response = requests.get(api_url + QUERY, headers={'Authorization': 'Token ' + AUTH_TOKEN}) 34 json_data = response.json() if response and response.status_code == 200 else None 35 random_item = random.sample(json_data['results'], 1) 36 37 print("%s %s" % (random_item[0]['id'], random_item[0]['url'])) 38 39 def main(): 40 parser = argparse.ArgumentParser() 41 parser.add_argument("-r", "--read", help = "Mark read", required = False) 42 args = parser.parse_args() 43 44 # if id provided mark item as read 45 if args.read: 46 id = args.read 47 mark_read(id) 48 # otherwise print a random url 49 else: 50 random_unread() 51 52 main()