Connect to Firefox sqlite db and import bookmarks into BukuDb. Parameters ---------- path : str Path to Firefox bookmarks sqlite database. unique_tag : str Timestamp tag in YYYYMonDD format. add_parent_folder_as_tag : bool
(self, path, unique_tag, add_parent_folder_as_tag)
| 2794 | self.add_rec(*item) |
| 2795 | |
| 2796 | def load_firefox_database(self, path, unique_tag, add_parent_folder_as_tag): |
| 2797 | """Connect to Firefox sqlite db and import bookmarks into BukuDb. |
| 2798 | |
| 2799 | Parameters |
| 2800 | ---------- |
| 2801 | path : str |
| 2802 | Path to Firefox bookmarks sqlite database. |
| 2803 | unique_tag : str |
| 2804 | Timestamp tag in YYYYMonDD format. |
| 2805 | add_parent_folder_as_tag : bool |
| 2806 | True if bookmark parent folders should be added as tags else False. |
| 2807 | """ |
| 2808 | |
| 2809 | # Connect to input DB |
| 2810 | conn = sqlite3.connect('file:%s?mode=ro' % path, uri=True) |
| 2811 | |
| 2812 | cur = conn.cursor() |
| 2813 | res = cur.execute('SELECT DISTINCT fk, parent, title FROM moz_bookmarks WHERE type=1') |
| 2814 | # get id's and remove duplicates |
| 2815 | for row in res.fetchall(): |
| 2816 | # get the url |
| 2817 | res = cur.execute('SELECT url FROM moz_places where id={}'.format(row[0])) |
| 2818 | url = res.fetchone()[0] |
| 2819 | if is_nongeneric_url(url): |
| 2820 | continue |
| 2821 | |
| 2822 | # get tags |
| 2823 | res = cur.execute('SELECT parent FROM moz_bookmarks WHERE ' |
| 2824 | 'fk={} AND title IS NULL'.format(row[0])) |
| 2825 | bm_tag_ids = [tid for item in res.fetchall() for tid in item] |
| 2826 | |
| 2827 | bookmark_tags = [] |
| 2828 | for bm_tag_id in bm_tag_ids: |
| 2829 | res = cur.execute('SELECT title FROM moz_bookmarks WHERE id={}'.format(bm_tag_id)) |
| 2830 | bookmark_tags.append(res.fetchone()[0]) |
| 2831 | |
| 2832 | if add_parent_folder_as_tag: |
| 2833 | # add folder name |
| 2834 | parent_id = row[1] |
| 2835 | while parent_id: |
| 2836 | res = cur.execute('SELECT title,parent FROM moz_bookmarks ' |
| 2837 | 'WHERE id={}'.format(parent_id)) |
| 2838 | parent = res.fetchone() |
| 2839 | if parent: |
| 2840 | title, parent_id = parent |
| 2841 | bookmark_tags.append(title) |
| 2842 | |
| 2843 | if unique_tag: |
| 2844 | # add timestamp tag |
| 2845 | bookmark_tags.append(unique_tag) |
| 2846 | |
| 2847 | formatted_tags = [DELIM + strip_delim(tag) for tag in bookmark_tags] |
| 2848 | tags = parse_tags(formatted_tags) |
| 2849 | |
| 2850 | # get the title |
| 2851 | title = row[2] or '' |
| 2852 | |
| 2853 | self.add_rec(url, title, tags, None, 0, True, False) |