├── .gitignore ├── LICENSE ├── README.md ├── aggregate_bsky_tabs.py ├── aggregate_youtube_tabs.py ├── aggregate_youtube_tabs_simpl.py ├── close_empty_windows.py ├── close_youtube_home_tabs.py ├── common.py ├── export_youtube_tabs.py ├── heavy_tab_hosts.txt ├── move_to_new_tabs.py └── save_bsky_images.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # nano temp files 132 | *.swp 133 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Arsenijs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dudetab 2 | Scripts using brotab's Python API to do useful things 3 | 4 | Sorted by increasing complexity: 5 | 6 | * ```close_youtube_home_tabs.py``` - closes all YouTube homepage tabs (tabs with the "https://youtube.com" URL) 7 | * ```close_empty_windows.py``` - closes windows only containing empty tabs 8 | * ```move_to_new_tabs.py``` - for each window where a "heavy" tab is currently active, either moves to an empty tab in the same window or opens an empty tab in that window. This helps reduce memory consumption after a Firefox session is restored. 9 | * ```aggregate_bsky_tabs.py``` - closes all Bluesky timeline tabs and moves Bluesky tabs from all windows into a window that contains the most Bluesky tabs 10 | * ```aggregate_youtube_tabs.py``` - closes all YouTube main page tabs. Then, allows the user to sort all YouTube tabs into two windows - one with regular YouTube videos and one with YouTube music videos, so that, in the end, there's only two windows left containing YouTube tabs. 11 | * ```aggregate_youtube_tabs_simpl.py``` - closes all YouTube main page tabs. Then, puts all the YouTube tabs into the window that contains the most YouTube tabs pre-sorting. 12 | * ```export_youtube_tabs.py``` - closes all YouTube main page tabs. Then, fetches metadata for all open YouTube video tabs using `yt-dlp`. Then, lets you sort through the tabs to either close them, ignore them, or append the video URL&name&channel to a text file of your choice. Also lets you quit the script mid-sorting in case you get tired midway. 13 | * ```save_bsky_images.py``` - downloads all open `cdn.bsky.app` tabs into a folder of your choosing, then closes the tabs with successfully downloaded content. 14 | 15 | Other files: 16 | * ```heavy_tab_hosts.txt``` - list of "heavy" hosts for the ```move_to_new_tabs.py``` script 17 | * ```common.py```: common helper functions 18 | 19 | Run with Python 3. Run these scripts as ```python3 -i script.py``` to get a commandline you can further experiment in after the script stops running. 20 | -------------------------------------------------------------------------------- /aggregate_bsky_tabs.py: -------------------------------------------------------------------------------- 1 | import operator 2 | 3 | import common 4 | 5 | cl = common.get_client() 6 | tabs = common.parse_tabs(cl) 7 | 8 | bsky_tabs = [tab for tab in tabs if "https://bsky.app" in tab.url] 9 | 10 | # closing Bluesky Home tabs 11 | 12 | for tab in bsky_tabs: 13 | if tab.url == "https://bsky.app/home": 14 | print("Closing home tab:", tab.get_full_id()) 15 | cl.close_tabs([tab.get_full_id()]) 16 | 17 | # determining a window where the majority of Bluesky tabs are 18 | 19 | bluesky_windows = {} 20 | for tab in bsky_tabs: 21 | if tab.window not in bluesky_windows: 22 | bluesky_windows[tab.window] = 1 23 | else: 24 | bluesky_windows[tab.window] += 1 25 | 26 | largest_window = max(bluesky_windows.items(), key=operator.itemgetter(1))[0] 27 | 28 | # moving all the tabs into that window 29 | # the process is involved since you need to recalculate tab indices - I'd rather rely on the brotab code for that 30 | 31 | old_tabs = common.serialize_tabs(tabs) 32 | 33 | #print(bsky_tabs) 34 | 35 | # first tab is me-specific thing 36 | 37 | first_tab_ignored = False 38 | 39 | for tab in bsky_tabs: 40 | if tab.window == '1': 41 | if not first_tab_ignored: 42 | first_tab_ignored = True 43 | continue 44 | if tab.window != largest_window: 45 | print("Moving tab {} from window {} to window {}".format(tab.get_full_id(), tab.window, largest_window)) 46 | tab.window = largest_window 47 | 48 | new_tabs = common.serialize_tabs(tabs) 49 | # this code recalculates the tab indices and also calls the move command 50 | common.update_tabs(cl, old_tabs, new_tabs) 51 | 52 | tabs = common.parse_tabs(cl) 53 | 54 | # cleaning the bluesky window from anything that's not bluesky 55 | 56 | # TODO - can't open a new window to dump tabs into, so, need to think of other way to clean up the bluesky window 57 | 58 | """ 59 | bluesky_window_tabs = [tab for tab in tabs if tab.window == bluesky_window] 60 | unrelated_tabs = [tab for tab in bluesky_window_tabs if "//bsky.app" not in tab.url] 61 | 62 | for tab in unrelated_tabs: 63 | if tab.url == "https://bsky.app/home": 64 | break 65 | #print("Closing home tab:", tab.get_full_id()) 66 | #cl.close_tabs([tab.get_full_id()]) 67 | """ 68 | 69 | -------------------------------------------------------------------------------- /aggregate_youtube_tabs.py: -------------------------------------------------------------------------------- 1 | import operator 2 | import random 3 | 4 | # This script assumes that the user has a large window full of YouTube tabs with music, and a large window that has non-music tabs. 5 | # The user also opens YouTube tabs in other windows from time to time - music or not music. 6 | # The script assumes there's only one large (>30) all-music window currently open. 7 | # This is, as you can guess, my usage scenario. 8 | 9 | # Before everything, the script closes all the YouTube homepage tabs. 10 | # First, the script goes through each large window and ask users whether tabs in it are music or non-music tabs 11 | # once a window with enough music tabs is found, it's determined to be a music window 12 | # All the other large windows are merged together into whatever non-music window is the largest. 13 | # Then, for each small window (window that doesn't contain a lot of YouTube tabs, no matter the count of other tabs in the window), 14 | # user has to go through every single tab and decide whether it's a music tab or not. 15 | # This way, tabs can be sorted properly, even if there's a large window with both music tabs and non-music tabs. 16 | # Also, after a window is decided to be a music window, the script moves all the tabs marked by user as non-music tabs out of it. 17 | 18 | import common 19 | 20 | cl = common.get_client() 21 | tabs = common.parse_tabs(cl) 22 | 23 | # closing YouTube Home tabs 24 | 25 | youtube_home_tabs = [tab for tab in tabs if tab.url.endswith("youtube.com/")] 26 | 27 | for tab in youtube_home_tabs: 28 | print("Closing YouTube home tab in window {}".format(tab.window)) 29 | cl.close_tabs([tab.get_full_id()]) 30 | 31 | youtube_tabs = [tab for tab in tabs if "youtube.com" in tab.url] 32 | 33 | # determining a window where the majority of Twitter tabs are 34 | 35 | sorting_limit = 30 36 | 37 | youtube_windows = {} 38 | for tab in youtube_tabs: 39 | if tab.window not in youtube_windows: 40 | youtube_windows[tab.window] = 1 41 | else: 42 | youtube_windows[tab.window] += 1 43 | 44 | print(youtube_windows) 45 | 46 | music_tabs_to_check = 5 47 | music_tab_threshold = 3 48 | 49 | music_window = None 50 | large_windows = {} 51 | 52 | def ask_if_music_tab(tab, leave_option=False): 53 | message = 'Is this music: \"{}\"?'.format(tab.name) 54 | if leave_option: 55 | prompt = "[y]es? [N]o [l]eave? [c]lose? >>> " 56 | else: 57 | prompt = "[y]es? [N]o? [c]lose? >>> " 58 | print(message) 59 | result = None 60 | valid_options = "YyNnLlCc" if leave_option else "YyNnCc" 61 | while not result or (result not in valid_options or len(result) > 1): 62 | result = input(prompt) 63 | if result.lower() == 'y': 64 | return True 65 | elif result.lower() == 'l': 66 | return "leave" 67 | elif result.lower() == 'c': 68 | return "close" 69 | return False 70 | 71 | tabs_to_close = [] 72 | # here, we're asking the user to go through 5 tabs and mark them as either music or not music 73 | # if enough tabs in a window are music tabs, we determine the window to be a music window 74 | non_music_tabs = [] 75 | 76 | for window, count in youtube_windows.items(): 77 | if count > sorting_limit: 78 | if music_window: # music window already found, this is a non-music window 79 | large_windows[window] = count 80 | continue 81 | # let's check if this is a music window! 82 | print("Large window {} has {} tabs".format(repr(window), count)) 83 | non_music_tabs = [] 84 | for i in range(music_tabs_to_check): 85 | tab = random.choice(common.filter_tabs_by_window(tabs, window)) 86 | # avoid repetitions 87 | while tab in non_music_tabs: 88 | tab = random.choice(common.filter_tabs_by_window(tabs, window)) 89 | result = ask_if_music_tab(tab) 90 | if result == "close": 91 | tabs_to_close.append(tab) 92 | elif result: 93 | # we're not filtering music tabs from non-music windows yet 94 | pass #music_tabs.append(tab) 95 | else: 96 | non_music_tabs.append(tab) 97 | # we've checked some tabs, now let's see if it's likely that the window is a music window 98 | if music_tabs_to_check-len(non_music_tabs) >= music_tab_threshold: 99 | # this is the music window 100 | music_window = window 101 | else: 102 | large_windows[window] = count 103 | 104 | for tab in tabs_to_close: 105 | print("Closing tab {}".format(tab.get_full_id())) 106 | cl.close_tabs([tab.get_full_id()]) 107 | 108 | tabs_to_close = [] 109 | 110 | if music_window is None: 111 | print("Music window not found! Sorting can't proceed") 112 | 113 | largest_window = max(large_windows.items(), key=operator.itemgetter(1))[0] 114 | 115 | # first, moving any manually-found non-music tabs from the music window into the largest window 116 | # wouldn't want to just leave them in the music window 117 | old_tabs = common.serialize_tabs(tabs) 118 | for tab in non_music_tabs: 119 | print("Moving non-music tab {} from window {} to window {}".format(tab.get_full_id(), tab.window, largest_window)) 120 | tab.window = largest_window 121 | new_tabs = common.serialize_tabs(tabs) 122 | common.update_tabs(cl, old_tabs, new_tabs) 123 | 124 | # regenerate all the variables we'll be using 125 | tabs = common.parse_tabs(cl) 126 | youtube_tabs = [tab for tab in tabs if "youtube.com" in tab.url] 127 | 128 | # merge all large non-music windows together with the largest window 129 | 130 | print(large_windows, largest_window, music_window) 131 | 132 | for window in large_windows: 133 | print("Processing {}".format(window)) 134 | if window == largest_window: 135 | continue 136 | print("Processing {}".format(window)) 137 | old_tabs = common.serialize_tabs(tabs) 138 | window_tabs = common.filter_tabs_by_window(tabs, window) 139 | youtube_tabs = [tab for tab in window_tabs if "youtube.com" in tab.url] 140 | for tab in youtube_tabs: 141 | print("Moving tab {} from window {} to window {}".format(tab.get_full_id(), tab.window, largest_window)) 142 | tab.window = largest_window 143 | new_tabs = common.serialize_tabs(tabs) 144 | # this code recalculates the tab indices and also calls the move command 145 | common.update_tabs(cl, old_tabs, new_tabs) 146 | tabs = common.parse_tabs(cl) 147 | 148 | other_windows = [window for window in youtube_windows.keys() if window not in large_windows and window != music_window] 149 | 150 | if other_windows: 151 | print("Other windows: {}".format(other_windows)) 152 | else: 153 | print("Sorting finished, no small windows found!") 154 | import sys;sys.exit(0) 155 | 156 | # now, prompting the user to sort through small windows one-by-one 157 | 158 | old_tabs = common.serialize_tabs(tabs) 159 | for window in other_windows: 160 | window_tabs = common.filter_tabs_by_window(tabs, window) 161 | for tab in window_tabs: 162 | if tab not in youtube_tabs: 163 | continue 164 | result = ask_if_music_tab(tab, leave_option = True) 165 | do_leave = result is None 166 | if result == "leave": 167 | continue 168 | elif result == "close": 169 | tabs_to_close.append(tab) 170 | continue 171 | elif result: 172 | destination = music_window 173 | else: 174 | destination = largest_window 175 | print("Moving tab {} from window {} to window {}".format(tab.get_full_id(), tab.window, destination)) 176 | tab.window = destination 177 | 178 | for tab in tabs_to_close: 179 | print("Closing tab {}".format(tab.get_full_id())) 180 | cl.close_tabs([tab.get_full_id()]) 181 | 182 | new_tabs = common.serialize_tabs(tabs) 183 | common.update_tabs(cl, old_tabs, new_tabs) 184 | 185 | print("Sorting finished!") 186 | -------------------------------------------------------------------------------- /aggregate_youtube_tabs_simpl.py: -------------------------------------------------------------------------------- 1 | import operator 2 | import random 3 | 4 | import common 5 | 6 | ################################################## 7 | # Variable names are very outdated here istg like 8 | # do not trust the variable names 9 | # they will deceive you 10 | ################################################## 11 | 12 | cl = common.get_client() 13 | tabs = common.parse_tabs(cl) 14 | 15 | # closing YouTube Home tabs 16 | 17 | youtube_home_tabs = [tab for tab in tabs if tab.url.endswith("youtube.com/")] 18 | 19 | for tab in youtube_home_tabs: 20 | print("Closing YouTube home tab in window {}".format(tab.window)) 21 | cl.close_tabs([tab.get_full_id()]) 22 | 23 | youtube_tabs = [tab for tab in tabs if "youtube.com" in tab.url] 24 | 25 | # determining a window where the majority of YouTube tabs are 26 | 27 | youtube_windows = {} 28 | for tab in youtube_tabs: 29 | if tab.window not in youtube_windows: 30 | youtube_windows[tab.window] = 1 31 | else: 32 | youtube_windows[tab.window] += 1 33 | 34 | print(youtube_windows) 35 | 36 | large_windows = {} 37 | 38 | tabs_to_close = [] 39 | 40 | for window, count in youtube_windows.items(): 41 | large_windows[window] = count 42 | print("Large window {} has {} tabs".format(repr(window), count)) 43 | 44 | 45 | largest_window = max(large_windows.items(), key=operator.itemgetter(1))[0] 46 | 47 | # regenerate all the variables we'll be using 48 | tabs = common.parse_tabs(cl) 49 | youtube_tabs = [tab for tab in tabs if "youtube.com" in tab.url] 50 | 51 | # merge all large non-music windows together with the largest window 52 | 53 | print(large_windows, largest_window) 54 | 55 | for window in large_windows: 56 | print("Processing {}".format(window)) 57 | if window == largest_window: 58 | continue 59 | print("Processing {}".format(window)) 60 | old_tabs = common.serialize_tabs(tabs) 61 | window_tabs = common.filter_tabs_by_window(tabs, window) 62 | youtube_tabs = [tab for tab in window_tabs if "youtube.com" in tab.url] 63 | for tab in youtube_tabs: 64 | print("Moving tab {} from window {} to window {}".format(tab.get_full_id(), tab.window, largest_window)) 65 | tab.window = largest_window 66 | new_tabs = common.serialize_tabs(tabs) 67 | # this code recalculates the tab indices and also calls the move command 68 | common.update_tabs(cl, old_tabs, new_tabs) 69 | tabs = common.parse_tabs(cl) 70 | 71 | other_windows = [window for window in youtube_windows.keys() if window not in large_windows] 72 | 73 | if other_windows: 74 | print("Other windows: {}".format(other_windows)) 75 | else: 76 | print("Sorting finished, no small windows found!") 77 | import sys;sys.exit(0) 78 | -------------------------------------------------------------------------------- /close_empty_windows.py: -------------------------------------------------------------------------------- 1 | import common 2 | 3 | cl = common.get_client() 4 | tabs = common.parse_tabs(cl) 5 | windows = common.get_window_ids(tabs) 6 | 7 | # going through tabs and, for each tab that's not empty, filtering out the window that contains it 8 | # after that, the windows left over will only contain empty tabs 9 | for tab in tabs: 10 | if not common.is_empty_tab(tab) and tab.window in windows: 11 | windows.remove(tab.window) 12 | 13 | # closing each tab one-by-one 14 | for tab in tabs: 15 | if common.is_empty_tab(tab) and tab.window in windows: 16 | print("Closing empty tab {}".format(tab.get_full_id())) 17 | cl.close_tabs([tab.get_full_id()]) 18 | -------------------------------------------------------------------------------- /close_youtube_home_tabs.py: -------------------------------------------------------------------------------- 1 | import common 2 | 3 | cl = common.get_client() 4 | tabs = common.parse_tabs(cl) 5 | 6 | # closing all YouTube homepage tabs - tabs with "https://youtube.com/" URL 7 | 8 | youtube_home_tabs = [tab for tab in tabs if tab.url.endswith("youtube.com/")] 9 | 10 | for tab in youtube_home_tabs: 11 | print("Closing YouTube home tab in window {}".format(tab.window)) 12 | cl.close_tabs([tab.get_full_id()]) 13 | -------------------------------------------------------------------------------- /common.py: -------------------------------------------------------------------------------- 1 | from brotab import main as btm 2 | from brotab import api as brapi 3 | 4 | default_browser = "firefox" 5 | 6 | def get_client(requested_browser = None): 7 | if requested_browser is None: 8 | requested_browser = default_browser 9 | clients = btm.create_clients() 10 | if not clients: 11 | raise Exception("No clients found!") 12 | browser_matches = [cl for cl in clients if cl._get_browser() == requested_browser] 13 | if not browser_matches: 14 | raise Exception("No clients found!") 15 | if len(browser_matches) > 1: 16 | raise Exception("Too many clients found, don't know what to do!") 17 | return browser_matches[0] 18 | 19 | 20 | class Tab(): 21 | def __init__(self, prefix, window, id, name, url): 22 | self.prefix = prefix 23 | self.window = window 24 | self.id = id 25 | self.name = name 26 | self.url = url 27 | 28 | def get_full_id(self): 29 | return ".".join([self.prefix, self.window, self.id]) 30 | 31 | def to_string(self): 32 | return "\t".join([self.get_full_id(), self.name, self.url]) 33 | 34 | 35 | def serialize_tabs(tabs): 36 | """ 37 | converts a list of Tab objects into strings, as they were before parsing 38 | this can then be used in update_tabs 39 | """ 40 | return [tab.to_string() for tab in tabs] 41 | 42 | def update_tabs(client, old_tabs, new_tabs): 43 | """ 44 | after moving tabs between windows, pass old and new serialized tabs here so that the changes can actually be applied 45 | tab indices need to be recalculated, that's done by the brotab API call 46 | """ 47 | return brapi.MultipleMediatorsAPI(None)._move_tabs_if_changed(client, old_tabs, new_tabs) 48 | 49 | def get_window_ids(tabs): 50 | """ 51 | gets tab objects and returns a list of window IDs 52 | """ 53 | window_ids = [tab.window for tab in tabs] 54 | window_ids = list(set(window_ids)) 55 | return window_ids 56 | 57 | def parse_tabs(client): 58 | """ 59 | returns a list of Tab objects 60 | """ 61 | tabs = client.list_tabs_safe(None) 62 | for i, tab in enumerate(tabs): 63 | tabs[i] = tab.split('\t') 64 | # sanity-check tab lengths after parsing - result should be {3} 65 | tab_list_lengths = set([len(tab) for tab in tabs]) 66 | try: 67 | assert ( tab_list_lengths == {3}) 68 | except AssertionError: 69 | faulty_tab_strs = [repr(tab) for tab in tabs if len(tab) != 3] 70 | faulty_tabs_str = ", ".join(faulty_tab_strs) 71 | raise ValueError("Tab info parsing problem! Tab list lengths after parsing: {}, faulty tabs: {}".format(repr(tab_list_lengths), faulty_tabs_str)) 72 | for i, tab in enumerate(tabs): 73 | tab[1:1] = tab[0].split('.') 74 | tab.pop(0) 75 | tabs[i] = Tab(*tabs[i]) 76 | return tabs 77 | 78 | def is_empty_tab(tab): 79 | return tab.url == "about:newtab" or tab.url == "about:home" or tab.url == "about:blank" 80 | 81 | def filter_tabs_by_window(tabs, window): 82 | return [tab for tab in tabs if tab.window == window] 83 | 84 | if __name__ == "__main__": 85 | client = get_client() 86 | tabs = parse_tabs(client) 87 | -------------------------------------------------------------------------------- /export_youtube_tabs.py: -------------------------------------------------------------------------------- 1 | import operator 2 | import random 3 | import re 4 | import json 5 | 6 | import common 7 | 8 | import yt_dlp 9 | 10 | out_file = "INSERT FILENAME HERE" 11 | 12 | cl = common.get_client() 13 | tabs = common.parse_tabs(cl) 14 | 15 | def ask_tab_action(tab): 16 | message = "Save tab?" 17 | prompt = "[s]ave? [l]eave? [c]lose? [q]uit? >>> " 18 | print(message) 19 | result = None 20 | valid_options = "slcq" 21 | valid_options += valid_options.upper() 22 | while not result or (result not in valid_options or len(result) > 1): 23 | result = input(prompt) 24 | if result.lower() == 's': 25 | return "save" 26 | elif result.lower() == 'l': 27 | return "leave" 28 | elif result.lower() == 'c': 29 | return "close" 30 | elif result.lower() == 'q': 31 | return "quit" 32 | else: 33 | FunniException = type("FunniException", (Exception,), {}) 34 | raise FunniException 35 | 36 | # closing YouTube Home tabs 37 | 38 | youtube_home_tabs = [tab for tab in tabs if tab.url.endswith("youtube.com/")] 39 | 40 | for tab in youtube_home_tabs: 41 | print("Closing YouTube home tab in window {}".format(tab.window)) 42 | cl.close_tabs([tab.get_full_id()]) 43 | 44 | youtube_tabs = [tab for tab in tabs if "youtube.com/watch" in tab.url] 45 | 46 | tabs_to_close = [] 47 | 48 | # first loop: downloading 49 | 50 | for i, tab in enumerate(youtube_tabs): 51 | ydl_opts = {} 52 | print("Downloading tab info {}/{}".format(i+1, len(youtube_tabs))) 53 | try: 54 | with yt_dlp.YoutubeDL(ydl_opts) as ydl: 55 | info = ydl.extract_info(tab.url, download=False, process=False) 56 | sinfo = ydl.sanitize_info(info) 57 | tab.sinfo = sinfo 58 | duration = tab.sinfo.get("duration", 0) 59 | duration_m = duration // 60 60 | duration_s = duration % 60 61 | print(duration_m, duration_s) 62 | except yt_dlp.utils.DownloadError as e: 63 | print(e) 64 | tab.sinfo = {} 65 | 66 | # DURATION 67 | 68 | for i, tab in enumerate(youtube_tabs): 69 | name = tab.name 70 | pattern = re.compile("^\(\d*\)\s(.*)$") 71 | match = pattern.match(name) 72 | if match: 73 | #print("pattern (NUMBER) matched!") 74 | name = match.groups()[0] 75 | end = ' - YouTube' 76 | if name.endswith(end): 77 | name = name[:-len(end)] 78 | duration = tab.sinfo.get("duration", 0) 79 | duration_m = duration // 60 80 | duration_s = duration % 60 81 | n = "{}/{}".format(i+1, len(youtube_tabs)) 82 | s = "{} - '{}' ({}:{}), from {}".format(tab.url, name, duration_m, duration_s, tab.sinfo.get("uploader", "[UNKNOWN]")) 83 | print(n, s) 84 | result = ask_tab_action(tab) 85 | if result == "close": 86 | tabs_to_close.append(tab) 87 | elif result == "leave": 88 | continue 89 | elif result == "save": 90 | with open(out_file, 'a') as f: 91 | f.write(s+'\n') 92 | tabs_to_close.append(tab) 93 | elif result == "quit": 94 | break 95 | 96 | try: 97 | for tab in tabs_to_close: 98 | print("Closing tab {}".format(tab.get_full_id())) 99 | cl.close_tabs([tab.get_full_id()]) 100 | except: 101 | print(tabs_to_close) 102 | -------------------------------------------------------------------------------- /heavy_tab_hosts.txt: -------------------------------------------------------------------------------- 1 | twitter.com 2 | facebook.com 3 | youtube.com 4 | instagram.com 5 | discord.com 6 | mail.google.com 7 | .slack.com 8 | 9 | -------------------------------------------------------------------------------- /move_to_new_tabs.py: -------------------------------------------------------------------------------- 1 | import common 2 | 3 | cl = common.get_client() 4 | tabs = common.parse_tabs(cl) 5 | windows = common.get_window_ids(tabs) 6 | 7 | active_tabs = cl.get_active_tabs(None) 8 | 9 | with open("heavy_tab_hosts.txt", "r") as f: 10 | lines = f.readlines() 11 | heavy_hosts = [line.strip() for line in lines if line.strip()] 12 | 13 | print(heavy_hosts) 14 | 15 | def is_empty_tab(tab): 16 | return tab.url == "about:newtab" or tab.url == "about:home" 17 | 18 | def is_heavy_tab(tab): 19 | return any([host in tab.url for host in heavy_hosts]) 20 | 21 | for tab in tabs: 22 | if tab.get_full_id() in active_tabs: 23 | print("{}: {} ({})".format(tab.window, tab.name, tab.url)) 24 | if not common.is_empty_tab(tab) and is_heavy_tab(tab): 25 | # tab that's currently open is not empty and is a heavy one to hold in-memory 26 | # so, we need to either switch to an empty tab in the same window or open a new empty tab in the same window 27 | window = tab.window 28 | tabs_by_window = common.filter_tabs_by_window(tabs, window) 29 | # going through the window's tabs in reverse - so we switch to last open empty tab 30 | for tabw in list(reversed(tabs_by_window)): 31 | if is_empty_tab(tabw): 32 | #print('Switching from "{}" to "{}" in window {}'.format(tab.name, tabw.name, window)) 33 | cl.activate_tab([tabw.get_full_id()], False) 34 | break 35 | else: # no empty tab in the window found 36 | print("Opening a blank tab in \"{}\" window ({})".format(tab.name, window)) 37 | cl.open_urls(["about:blank"], window_id=window) 38 | -------------------------------------------------------------------------------- /save_bsky_images.py: -------------------------------------------------------------------------------- 1 | import traceback 2 | import operator 3 | import requests 4 | import common 5 | import shutil 6 | import os 7 | 8 | target_dir = 'DIROFYOURCHOICE' 9 | 10 | 11 | cl = common.get_client() 12 | tabs = common.parse_tabs(cl) 13 | 14 | #https://cdn.bsky.app/ 15 | #https://cdn.bsky.app/img/feed_fullsize/plain/did:plc:uynpich2hsqmryyhr3moz5re/bafkreicg7v7vslvdqsaaogvft77k6ntszobudvnb5z5pybi2263z257d7i@jpeg 16 | 17 | bsky_tabs = [tab for tab in tabs if "https://cdn.bsky.app" in tab.url] 18 | 19 | for tab in bsky_tabs: 20 | last_part = tab.url.rsplit('/', 1)[-1] 21 | print(last_part) 22 | if not '@' in last_part: 23 | print("Can't find @ in ", tab.url) 24 | continue 25 | fn, ext = last_part.rsplit('@', 1) 26 | filename = fn+'.'+ext 27 | print(filename) 28 | path = os.path.join(target_dir, filename) 29 | if os.path.isfile(path): 30 | print("Path {} already has a file; closing".format(path)) 31 | cl.close_tabs([tab.get_full_id()]) 32 | continue 33 | try: 34 | rqq = requests.get(tab.url, stream=True) 35 | if rqq.status_code != 200: 36 | print("Can't download from URL {}: code {}!".format(tab.url, rqq.status_code)) 37 | continue 38 | #breakpoint() 39 | rqq.raw.decode_content = True 40 | with open(path, 'wb') as f: 41 | shutil.copyfileobj(rqq.raw, f) 42 | print("Downloaded URL {}!".format(tab.url)) 43 | except: 44 | print("Can't download from URL {}!".format(tab.url)) 45 | traceback.print_exc() 46 | else: 47 | cl.close_tabs([tab.get_full_id()]) 48 | --------------------------------------------------------------------------------