Last active
August 29, 2015 14:16
-
-
Save flavioamieiro/186a30407e410e7c2092 to your computer and use it in GitHub Desktop.
This script gets urls from tabs in a firefox session.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
def get_session_from_file(filename): | |
with open(filename, 'r') as fp: | |
session = json.load(fp) | |
return session | |
def get_tabs_from_session(session): | |
return session['windows'][0]['tabs'] | |
def get_group_info_from_session(session): | |
groups = [] | |
group_data = json.loads(session['windows'][0]['extData']['tabview-group']) | |
for id, data in group_data.items(): | |
groups.append((id, data['title'])) | |
return groups | |
def get_urls_from_tabs(tabs): | |
return map(lambda t: t['entries'][0]['url'], tabs) | |
def get_urls_from_group_tab(tabs, group_id): | |
return get_urls_from_tabs(filter(lambda t: | |
t['extData']['tabview-tab'].find('{{"groupID":{}'.format(group_id)) != -1, tabs)) | |
def print_usage_and_exit(): | |
sys.stdout.write( | |
"usage: {} [-g] <path_to_session_file>\n".format(sys.argv[0])) | |
sys.exit(1) | |
if __name__ == "__main__": | |
import sys | |
import pprint | |
# I should use argparse, but who cares? | |
if len(sys.argv) < 2: | |
print_usage_and_exit() | |
elif '-g' in sys.argv and len(sys.argv) <3: # fuck pep8 for the sake of ASCII | |
print_usage_and_exit() | |
path = sys.argv[-1] | |
session = get_session_from_file(path) | |
tabs = get_tabs_from_session(session) | |
if '-g' in sys.argv: | |
groups = get_group_info_from_session(session) | |
for group_id, group_name in groups: | |
group_urls = get_urls_from_group_tab(tabs, group_id) | |
print('[{}]'.format(group_name if group_name else 'unnamed group')) | |
list(map(print, group_urls)) | |
else: | |
urls = get_urls_from_tabs(tabs) | |
list(map(print, urls)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment