Created
August 14, 2018 15:39
-
-
Save Varal7/c6ba652d1a78e8a5ed6ea4a2e0cd435a to your computer and use it in GitHub Desktop.
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
from bs4 import BeautifulSoup | |
from fire import Fire | |
def bookmarks_to_links(html): | |
soup = BeautifulSoup(html, 'lxml') | |
links = [x.attrs['href'] for x in soup.findAll('a')] | |
return links | |
def links_to_bookmarks(links): | |
start = """<H1>Bookmarks</H1> | |
<DL><p> | |
<DT><H3 ADD_DATE="0" LAST_MODIFIED="0" PERSONAL_TOOLBAR_FOLDER="true">Bookmarks Bar</H3> | |
<DL><p> | |
</DL><p> | |
<DT><H3 ADD_DATE="0" LAST_MODIFIED="0">Links</H3> | |
<DL><p> | |
""" | |
end = """ | |
</DL><p></DL><p>""" | |
return start + "\n".join('<DT><A HREF="' + x + '">' + x + '</A>' for x in links) | |
def main(input_filename, output_filename): | |
with open(input_filename) as f: | |
with open(output_filename, 'w') as w: | |
if input_filename[-4:] == "html": | |
html = '\n'.join(f.readlines()) | |
links = bookmarks_to_links(html) | |
w.write('\n'.join(links)) | |
else: | |
links = f.readlines() | |
html = links_to_bookmarks(links) | |
w.write(html) | |
if __name__ == "__main__": | |
Fire(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment