133 lines
3.6 KiB
Python
133 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""pinboard rofi script"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import appdirs
|
|
import click
|
|
import pinboard
|
|
from tabulate import tabulate
|
|
|
|
|
|
class RofiPinboard:
|
|
"""Rofi Pinboard class"""
|
|
|
|
book_marks = None
|
|
config = None
|
|
config_file = None
|
|
pin = None
|
|
|
|
def __init__(self):
|
|
# set config file
|
|
cfg_path = Path(appdirs.user_config_dir(appname="rofi-pinboard"))
|
|
cfg_path.mkdir(exist_ok=True, parents=True)
|
|
self.config_file = cfg_path.joinpath("pinboard.json")
|
|
|
|
def __setup(self):
|
|
"""Setup RofiPinboard"""
|
|
# load api token
|
|
self.load_config()
|
|
|
|
# initialise session
|
|
self.pin = pinboard.Pinboard(self.config["api_token"])
|
|
|
|
def __load_bookmarks(self):
|
|
bookmarks = None
|
|
temp_dir = self.__get_tmpdir()
|
|
|
|
temp_file = Path(os.path.join(str(temp_dir), "bookmarks.pinboard"))
|
|
if temp_file.is_file():
|
|
with open(temp_file) as bfh:
|
|
bookmarks = json.load(bfh)
|
|
temp_file.unlink()
|
|
return bookmarks
|
|
|
|
def __get_tmpdir(self):
|
|
"""Get the tmpdir"""
|
|
temp_dir = None
|
|
if "TMPDIR" in os.environ:
|
|
temp_dir = Path(os.environ["TMPDIR"])
|
|
else:
|
|
temp_dir = Path("/tmp")
|
|
|
|
# check if the temp dir exists
|
|
if not temp_dir.exists() or not temp_dir.is_dir():
|
|
click.echo("Your TMPDIR does not exist.")
|
|
exit(42)
|
|
|
|
return temp_dir
|
|
|
|
def __save_bookmarks(self):
|
|
"""Save bookmarks to tempdir"""
|
|
temp_dir = self.__get_tmpdir()
|
|
|
|
temp_file = Path(os.path.join(str(temp_dir), "bookmarks.pinboard"))
|
|
|
|
# unlink file if it exists
|
|
if temp_file.exists():
|
|
temp_file.unlink()
|
|
|
|
# write bookmarks to file
|
|
with open(temp_file, "w") as foh:
|
|
json.dump(self.book_marks, foh)
|
|
|
|
def load_config(self):
|
|
"""Load the config"""
|
|
if self.config_file.is_file():
|
|
self.config = json.loads(self.config_file.read_text())
|
|
else:
|
|
print("Could not find the config file.")
|
|
exit(42)
|
|
|
|
def get_bookmarks(self):
|
|
"""Get the bookmarks and data from"""
|
|
if not self.config:
|
|
self.__setup()
|
|
|
|
output = []
|
|
bookmarks = self.pin.posts.all()
|
|
|
|
for bookmark in bookmarks:
|
|
pbm = {
|
|
"url": bookmark.url.rstrip(),
|
|
"desc": bookmark.description,
|
|
"tags": ",".join(bookmark.tags),
|
|
"hash": bookmark.hash,
|
|
}
|
|
output.append(pbm)
|
|
|
|
self.book_marks = output
|
|
self.__save_bookmarks()
|
|
|
|
def get_url_from_id(self, url_id=None):
|
|
"""Get the URL for a given ID"""
|
|
bookmarks = self.__load_bookmarks()
|
|
if not bookmarks:
|
|
click.echo("Bookmarks file seems to be empty.")
|
|
exit(42)
|
|
return bookmarks[url_id]["url"]
|
|
|
|
def rofi_text(self):
|
|
rows = []
|
|
for i, bkm in enumerate(self.book_marks):
|
|
tags = bkm["tags"]
|
|
if not tags:
|
|
tags = "notag"
|
|
rows.append([i, bkm["desc"], bkm["url"], tags])
|
|
return tabulate(rows, headers=[], tablefmt="plain")
|
|
|
|
def rofi_print(self):
|
|
"""Print urls and tags for rofi."""
|
|
print(self.rofi_text)
|
|
|
|
def save_config(self, config):
|
|
"""Save the config file."""
|
|
if not isinstance(config, dict):
|
|
print("Config object not of type dict, aborting.")
|
|
exit(42)
|
|
# write config
|
|
with open(self.config_file, "w") as cfg_file:
|
|
json.dump(config, cfg_file)
|