-
Notifications
You must be signed in to change notification settings - Fork 34
/
ff-cli
executable file
·253 lines (226 loc) · 10.8 KB
/
ff-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python
# Imports are scattered because diff commands use different things
import os, sys, pathlib as pl
class adict(dict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self
class astr(str): pass
def get_profile_dir(profile, ff_home):
import configparser
if profile:
if profile.startswith('/'): return pl.Path(profile)
profile = profile.lower() # for ci comparisons
paths, ff_home = list(), pl.Path(ff_home).expanduser()
(conf_list := configparser.ConfigParser()).read(ff_home / 'profiles.ini')
for k, conf in conf_list.items():
if not k.startswith('Profile'): continue
try: p = ff_home / conf['Path']
except KeyError: continue
if ( profile is not None and
conf.get('Name', fallback='').strip().lower() == profile ): return p
if profile: paths.append(p); continue # candidates for string-match
if conf.getboolean('Default'): return p
for p in paths:
if profile in p.name.lower(): return p
def pick_bookmarks( bms_json,
filters_inc, filters_exc, shuffle, limit, js, cycle_file ):
def _bm_recurse(bm, p=pl.Path('')):
bms, title = list(), bm.get('title', '')
if bms_sub := bm.get('children'):
for bm in bms_sub: bms.extend(_bm_recurse(bm, p / title))
elif url := bm.get('uri'):
if js or not url.startswith('javascript:'):
bms.append(bm := astr(f'{p} :: {title} :: {url}'))
bm.path, bm.title, bm.url = p, title, url
return bms
bms = _bm_recurse(bms_json)
bms = list( bm for bm in bms if (bs := bm.lower())
and (not filters_inc or all(s in bs for s in filters_inc))
and (not filters_exc or all(s not in bs for s in filters_exc)) )
if shuffle: import random; random.shuffle(bms)
if cycle_file:
bms = dict((bm.url, bm) for bm in bms)
try: queue = cycle_file.read_text()
except OSError: queue = ''
queue = list(url.strip() for url in queue.splitlines() if url in bms)
n = min(len(bms), limit or len(bms))
urls, queue = dict.fromkeys(queue[:n]), queue[n:]
if n := n - len(urls):
queue = dict.fromkeys(bms)
for url in list(queue):
n -= url not in urls
urls[url] = queue.pop(url)
if n <= 0: break
cycle_file.write_text(''.join(f'{url}\n' for url in queue))
bms = list(bms[url] for url in urls)
elif limit: bms = bms[:limit]
return bms
def mozlz4_open_path(conf, p, to_stdout, to_bytes):
if (fn := p.name.lower()).endswith('lz4'):
if conf.compress:
print( 'WARNING: Specified path already ends with *lz4 suffix,'
f' might end up being compressed twice: {p}', file=sys.stderr )
elif not conf.decompress: conf.decompress = True
elif not (conf.compress or conf.decompress): conf.compress = True
conf.src = p.open('rb')
if to_bytes: return
if to_stdout: conf.dst = os.fdopen(sys.stdout.fileno(), 'wb'); return
import tempfile
if conf.compress: p = p.with_name(p.name + '.mozlz4')
elif fn.endswith('lz4'):
p = p.with_name(p.name[:-7 if fn.endswith('.mozlz4') else -3])
conf.dst = tempfile.NamedTemporaryFile(
delete=False, dir=str(p.parent), prefix=str(p.name)+'.' )
conf.dst_finalize = lambda: pl.Path(conf.dst.name).rename(p)
def mozlz4_open_streams(conf, to_bytes):
conf.src = open(sys.stdin.fileno(), 'rb')
if not to_bytes: conf.dst = open(sys.stdout.fileno(), 'wb')
if not conf.compress:
if not conf.decompress:
conf.decompress = conf.src.peek(8)[:8] == b'mozLz40\0'
if not conf.decompress: conf.compress = True
def mozlz4( p=None, compress=None, decompress=None,
to_stdout=False, to_bytes=False, bs_max=8 * 2**20 ):
import lz4.block # https://python-lz4.readthedocs.io/
conf = adict(
src=None, dst=None, dst_finalize=None,
compress=bool(compress), decompress=bool(decompress) )
try:
if p: mozlz4_open_path(conf, pl.Path(p), to_stdout, to_bytes)
else: mozlz4_open_streams(conf, to_bytes)
assert conf.compress ^ conf.decompress
data, moz_hdr = conf.src.read(bs_max + 1), b'mozLz40\0'
if len(data) >= bs_max: # intended for small json files only
raise MemoryError(f'File size >bs_max limit ({bs_max/2**20:,.1f} MiB)')
if conf.compress: data = moz_hdr + lz4.block.compress(data)
if conf.decompress:
if (hdr := data[:8]) != moz_hdr:
raise ValueError(f'Incorrect file header magic: {hdr}')
data = lz4.block.decompress(data[8:])
if not conf.dst: return data
conf.dst.write(data)
conf.dst.flush()
if conf.dst_finalize:
conf.dst_finalize()
if isinstance(conf.src.name, str): os.unlink(conf.src.name)
finally:
if conf.src: conf.src.close()
if conf.dst:
if isinstance(conf.dst.name, str):
pl.Path(conf.dst.name).unlink(missing_ok=True)
conf.dst.close()
def jsonlz4_read(p):
import json
return json.loads(mozlz4(p, decompress=True, to_bytes=True))
def main(args=None):
import argparse, textwrap, re
dd = lambda text: re.sub( r' \t+', ' ',
textwrap.dedent(text).strip('\n') + '\n' ).replace('\t', ' ')
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description='Tool to interact with firefox browser and its profile data.')
parser.add_argument('-H', '--ff-home', metavar='path', default='~/.mozilla/firefox',
help='Firefox-like browser home directory to use, if needed. Default: %(default)s')
parser.add_argument('-P', '--profile', metavar='name/key/path', help=dd('''
Full firefox profile name, profile directory name/fragment,
or a full path to profile dir (default: find Default=1 profile).'''))
cmds = parser.add_subparsers(title='Commands',
description='Use -h/--help with these to list command-specific options.', dest='call')
cmd = cmds.add_parser('tabs', aliases=['t'],
formatter_class=argparse.RawTextHelpFormatter, help='List currently opened tabs')
cmd = cmds.add_parser('bookmarks', aliases=['bm'],
formatter_class=argparse.RawTextHelpFormatter,
help='Query/open bookmarks', description=dd('''
Read bookmarks from bookmarksbackup dir dump(s).
about:config has controls for whether and how often these files get exported/updated.
browser.bookmarks.autoExportHTML also looks like a good option for these, not supported.
Arguments are simple "must contain all strings" filters, opts can limit/shuffle/open.'''))
cmd.add_argument('filter', nargs='*', help=dd('''
Filter strings to limit resulting list of bookmarks, kinda like "grep"
would, all of which must be included (AND logic) in either bookmark path, title or URL.
Matched against strings like (case-insensitive): some/bookmark/dirs :: bm-title :: bm-url
Can include special "+++" argument (3 pluses) followed by negative matches
to exclude from retured results matched by expressions preceding it (if any).'''))
cmd.add_argument('-r', '--shuffle',
action='store_true', help='Shuffle resulting list randomly.')
cmd.add_argument('-n', '--limit', type=int, metavar='N',
help='Limit number of bookmarks to N entries max (after -r/--shuffle).')
cmd.add_argument('-o', '--open', action='store_true',
help='Open URLs in addition to printing them, using "firefox" binary in PATH.')
cmd.add_argument('--js', action='store_true',
help='Include "javascript:..." bookmarklets in addition to URLs.')
cmd.add_argument('-f', '--fmt', metavar='fmt', default='{bm}', help=dd('''
Format for output lines, using following vars: n, bm, bm.path, bm.title, bm.url
Example: [{n}] {bm.title} - {bm.url}'''))
cmd.add_argument('-c', '--cycle', action='store_true', help=dd('''
Cycle filtered bookmarks in last-returned order, refreshing the queue when it's empty.
-r/--shuffle option affects order of how items get updated in queue.
Intended for cycling through random bookmarked stuff with less repetition.'''))
cmd.add_argument('--cycle-queue-file',
metavar='file', default='~/.cache/ff-cli.bookmark-cycle', help=dd('''
File to store a queue of bookmarks for -c/--cycle option.
Default: %(default)s'''))
cmd = cmds.add_parser('mozlz4', aliases=['lz4'],
formatter_class=argparse.RawTextHelpFormatter,
help='Compress/decompress firefox lz4 files', description=dd('''
Compress/decompress firefox/mozilla\'s *lz4 files.
Works roughly same as all compression tools on linux, e.g. gzip, xz, lz4, zstd.'''))
cmd.add_argument('path', nargs='?', help=dd('''
File(s) to operate on. Used with -c/--compress or -d/--decompress options.
By default, --compress will write a new file with .mozlz4 suffix,
removing the old one, and --decompress will remove .mozlz4 suffix
and compressed file, if present.
If no path is specified, stdin/stdout will be used.'''))
cmd.add_argument('-c', '--compress', action='store_true',
help='Compress specified file or stdin stream. Default for paths that dont end with *lz4.')
cmd.add_argument('-d', '--decompress', action='store_true',
help='Decompress specified file or stdin stream. Default for stdin with mozLz40 header.')
cmd.add_argument('-o', '--stdout', action='store_true',
help='Compress/decompress to stdout, even when file path is specified.')
opts = parser.parse_args(sys.argv[1:] if args is None else args)
def _profile_path():
if not (p := get_profile_dir(opts.profile, opts.ff_home)):
parser.error( 'Failed to find default/specified -P/--profile'
f' [ {opts.profile} ] in -H/--ff-home dir [ {opts.ff_home} ]' )
return p
if opts.call in ['tabs', 't']:
p = _profile_path() / (fn := 'sessionstore-backups/recovery.jsonlz4')
if not p.exists(): parser.error(f'Missing profile session-file: {fn}')
n, session = 0, jsonlz4_read(p)
for win in session['windows']:
for tab in win.get('tabs', list()):
if tab.get('hidden'): continue
if not (entries := tab.get('entries')): continue
last_entry = entries[tab['index'] - 1] # index starts from 1 there
title, url = (last_entry.get(k) for k in ['title', 'url'])
n += 1; print(f'\n[Tab {n}] {title}\n {url}')
if n: print()
elif opts.call in ['bookmarks', 'bm']:
try:
if not (ps := list((_profile_path() / 'bookmarkbackups').iterdir())): return
except OSError: return
fexc, finc = list(), list(s.lower() for s in opts.filter if s)
try:
n = finc.index('+++')
finc, fexc = finc[:n], finc[n+1:]
except ValueError: pass
bms = pick_bookmarks(
jsonlz4_read(sorted(ps, key=lambda p: p.name)[-1]),
filters_inc=finc, filters_exc=fexc,
shuffle=opts.shuffle, limit=opts.limit, js=opts.js,
cycle_file=opts.cycle and pl.Path(opts.cycle_queue_file).expanduser() )
if open_chk := opts.open: import subprocess as sp
for n, bm in enumerate(bms, 1):
print(opts.fmt.format(n=n, bm=bm))
if open_chk and sp.run(['firefox', bm.url]).returncode: open_chk = bm
if open_chk and open_chk is not True:
print(f'Firefox URL-open command failed for: {open_chk.url}', file=sys.stderr)
return 1
elif opts.call in ['mozlz4', 'lz4']:
if opts.compress and opts.decompress:
parser.error('Both --compress and --decompress at the same time make no sense.')
mozlz4(opts.path, opts.compress, opts.decompress, opts.stdout)
elif opts.call is None: parser.error('No command specified')
else: parser.error(f'Unrecognized command: {opts.call}')
if __name__ == '__main__': sys.exit(main())