I want to extract the URL from the recovery.js file (the file is created by Firefox, it contains the URLs of the windows and tabs in the Firefox session). My goal is to be able to store and classify these URLs in a text file. Then I can for example open a "special" firefox session with only the desired URLs and still store the other URLs. Note: I could do everything manually from Firefox but I find it less convenient.
import re
import os.path
Path to the recovery.js file:
# Using os.path.expanduser because of the tild:
filename = os.path.expanduser('~/Library/Application Support/Firefox/Profiles/g9vvodyo.default/sessionstore-backups/recovery.js')
Extraction of the data:
with open(filename, 'r') as f:
data = f.readlines()
Extract the URLs with a regular expression:
if len(data) == 1:
# regex extract the url (http) untill the first quote (")
urls = re.findall(r'(https?://\S[^"]*)', data[0])
else:
print('The data are not stored in one string. You should adapt the code. Good luck!')
len(urls)
Discard the duplicates in the URLs:
urls = list(set(urls))
len(urls)
urls
Write the urls in a file
filename = 'urls.txt'
# write only if the file does not exist
if not os.path.isfile(filename):
with open(filename, 'w') as f:
for url in urls:
f.write('{0}\n'.format(url))
print('You can now open all the urls with "firefox -n $(cat urls.txt)" on linux or "open /Applications/Firefox.app $(cat urls.txt)" on OS X'.)
print('NOTE: Open firefox before the previous command so firefox will open the URLs in tabs instead of sessions.')
else:
print('The file already exists! Delete the file if you want to create a file with this name.')
At this point, I made a manual classification of the URLs and put the new file in my home directory. The URLs are classified by theme, the theme is preceeded by "#". Then there are the URLs corresponding to that theme and then 2 blank lines.
Then I write a new script to extract the URLs according to their theme.
Open the desired URLs in the browser¶
Extract the theme (the themes are preceeded by "#"):
filename = 'urls.txt'
with open(filename, 'r') as f:
urls = f.readlines()
urls
Find the urls "themes"
# regex start with # and then any character (.) with one or more repetitions (+)
themes = [re.findall(r'^#.+', url) for url in urls]
# Flatten the preceeding list of list:
themes = [val for sublist in themes for val in sublist]
themes
Write the urls in a new file¶
For example I want to open the urls from the "reproducibility" theme:
theme = themes[0]
theme
Find the line number corresponding to the theme we are interested in:
regex = r'' + theme
for cpt, url in enumerate(urls):
# If the theme is found
if re.match(regex, url):
print(cpt)
break
Now, we know the rank (=cpt) corresponding to the theme we are interested in, so we will save the URLs until the first blank line:
regex = r'\n'
filename = 'extracted_urls.txt'
f = open(filename, 'w')
for url in urls[cpt+1:]:
print(url)
f.write(url)
if re.match(regex, url):
break
f.close() # Don't forget to close the file!
You can now open the extracted URLs via this command line:
open /Applications/Firefox.app $(cat extracted_urls.txt)
NOTE: Open firefox before the previous command so firefox will open the URLs in tabs instead of sessions
For this second part ("Open the desired URLs in the browser"), I wrote a Python script "open_urls.py" that can be used from the command line (e.g.: ./open_urls.py urls.txt '# Reproducibility'. It will open all the URLs of the "Reproducibility" theme). You can download the script here.