Skip to content

Instantly share code, notes, and snippets.

@akatrevorjay
Last active March 9, 2018 16:19
Show Gist options
  • Select an option

  • Save akatrevorjay/3fb5bc753edd42b2abd0accb4dde4cdc to your computer and use it in GitHub Desktop.

Select an option

Save akatrevorjay/3fb5bc753edd42b2abd0accb4dde4cdc to your computer and use it in GitHub Desktop.
Download all github repos that you have open in firefox tabs
#!/usr/bin/env python3
"""
_____________
tabs-to-repos -- Download all github repos that you have open in firefox tabs (quick hack; verify and pipe to sh)
@trevorj 12/2017
https://gist.github.com/akatrevorjay/3fb5bc753edd42b2abd0accb4dde4cdc
"""
import glob
import logging
import os
import re
import sys
import lz4.block # bug in firefox_tabs requires this, it only imports bare `lz4`
import firefox_tabs
log = logging.getLogger(__name__)
FF_SS_REC_GLOB = os.path.expanduser('~/.mozilla/firefox/*/sessionstore-backups/recovery.jsonlz4')
RE_GITHUB_REPO = re.compile(
r'(?P<scheme>https?)://(www\.|)github.com/(?P<user>[-_0-9a-zA-Z]+)/(?P<repo>[-_0-9a-zA-Z]+)/?$',
re.IGNORECASE,
)
class AttrDict(dict):
"""
>>> m = AttrDict(omg=True, whoa='yes')
"""
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def _latest_file_in_glob(glob_match):
mtime = lambda p: os.stat(p).st_mtime
paths = glob.glob(glob_match)
sorted_paths = sorted(paths, key=mtime)
if not sorted_paths:
raise ValueError('Could not find Firefox session store backup. What OS are you on? Check the glob.')
return sorted_paths[0]
def _load_firefox_data(path):
path = os.path.expanduser(path)
session_data = firefox_tabs.load_data(path)
return session_data
def _iter_firefox_tabs(session_data, container_factory=AttrDict):
for window_idx, window in enumerate(session_data['windows']):
for tab in window['tabs']:
current_tab = tab['entries'][-1]
tab = container_factory(
url=current_tab['url'],
title=current_tab['title'],
window=window_idx,
)
yield tab
def main(re_url=RE_GITHUB_REPO):
path = _latest_file_in_glob(FF_SS_REC_GLOB)
log.info('Loading from firefox session store backup: %r', path)
session_data = _load_firefox_data(path)
tabs = list(_iter_firefox_tabs(session_data))
log.info('Loaded session data: num_tabs=%d', len(tabs))
for tab in tabs:
log.debug('Checking tab: url=%s', tab.url)
m = re_url.match(tab.url)
if not m:
continue
md = AttrDict(m.groupdict())
# so hacky
print('# %s' % tab.url)
print('git clone https://github.com/%s/%s.git' % (md.user, md.repo))
# avoids gc overhead at exit.
sys.exit(0)
if __name__ == '__main__':
logging.basicConfig(
level=logging.DEBUG,
# Match squid log output
format='%(asctime)s| %(name)s/%(process)d: %(message)s '
'@%(funcName)s:%(lineno)d #%(levelname)s',
datefmt='%Y/%m/%d %H:%M:%S'
)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment