├── README.md └── roam_image_backup.py /README.md: -------------------------------------------------------------------------------- 1 | # demo-roam-image-bak 2 | 3 | Backup your Roam Research Images to local drive 4 | 5 | usage: 6 | 7 | - Download this repo; 8 | - Install Anaconda; 9 | - run `python roam_image_backup.py [your-roam-json-file] [image-storage-dir]` 10 | 11 | Your images will be downloaded to the directory you specified. 12 | 13 | Enjoy! -------------------------------------------------------------------------------- /roam_image_backup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | import os 5 | import sys 6 | import re 7 | from pathlib import Path 8 | import datetime 9 | import requests 10 | import shutil 11 | import json 12 | 13 | def str_formatted_current_time(): 14 | current_time = datetime.datetime.now() 15 | date_format = '%Y-%m-%d-%H-%M-%S-%f' 16 | timestr = current_time.strftime(date_format) 17 | return timestr 18 | 19 | def download_firebase_img(remote_img, image_storage_dir, firebase_local_records): 20 | if remote_img not in firebase_local_records: #not downloaded 21 | current_time_str = str_formatted_current_time() 22 | download_image_filename = image_storage_dir/f"{current_time_str}.png" 23 | r = requests.get(remote_img, stream=True) 24 | with open(download_image_filename, 'wb') as f: 25 | shutil.copyfileobj(r.raw, f) 26 | firebase_local_records[remote_img] = str(download_image_filename) 27 | else: 28 | print("Already downloaded!") 29 | 30 | 31 | if len(sys.argv) < 3: 32 | print("usage: python roam_image_backup.py [your-roam-json-file] [image-storage-dir]") 33 | exit() 34 | 35 | myjson = Path(sys.argv[1]).expanduser().resolve() 36 | image_storage_dir = Path(sys.argv[2]).expanduser().resolve() 37 | 38 | if not image_storage_dir.exists(): 39 | image_storage_dir.mkdir() 40 | 41 | firebase_download_record_json = image_storage_dir/"firebase_local_records.json" 42 | 43 | with open(myjson) as f: 44 | data = f.read() 45 | 46 | # get all remote images link 47 | 48 | def get_image_links(data): 49 | return list(re.findall(r'\!\[.*?\]\((.*?)\)', data)) 50 | 51 | image_links = get_image_links(data) 52 | 53 | 54 | # get google fire base storage images 55 | 56 | googlefirebaseimages = [] 57 | for image_link in image_links: 58 | if image_link.find("firebasestorage")>0: 59 | googlefirebaseimages.append(image_link) 60 | 61 | 62 | 63 | # download firebase storage images to local 64 | 65 | if firebase_download_record_json.exists(): 66 | with open(firebase_download_record_json) as f: 67 | firebase_local_records = json.load(f) 68 | else: 69 | firebase_local_records = {} 70 | 71 | for remote_img in googlefirebaseimages: 72 | download_firebase_img(remote_img, image_storage_dir, firebase_local_records) 73 | 74 | with open(firebase_download_record_json, 'w') as f: 75 | json.dump(firebase_local_records, f) 76 | 77 | --------------------------------------------------------------------------------