Skip to content

Instantly share code, notes, and snippets.

@InsiderPhD
Created October 17, 2022 11:46
Show Gist options
  • Save InsiderPhD/1898b9054c7458c27617c5763b6f0129 to your computer and use it in GitHub Desktop.
Save InsiderPhD/1898b9054c7458c27617c5763b6f0129 to your computer and use it in GitHub Desktop.
import os
import re
import shutil
import requests
token = 'PASTE YOUR TOKEN HERE'
perpage = 50
# get all the items
items = list()
gotThemAll = False
page = 0
debug = 0
location = 'Some/Path/Vault/Literature'
template = 'Some/Path/Vault/_templates/raindrop_literature_note.md'
while(not gotThemAll):
allHighlights = requests.get('https://api.raindrop.io/rest/v1/highlights', headers = {"Authorization": "Bearer " + token}, params={"perpage": perpage, "page":page})
highlightResult = allHighlights.json()
if len(highlightResult.get('items')) < perpage:
gotThemAll = True
else:
page = page+1
items.extend(highlightResult.get('items'))
for highlight in items:
# have we already processed this highlight?
if not ('_debug_processed' in highlight['tags']):
# file name but remove any special characters OneDrive is opposed to
fileName = location+'/'+re.sub('\W+',' ', highlight['title']).strip()+'.md'
# copy a literature example into a new file, and replace all the examples
if not os.path.exists(fileName):
shutil.copyfile(template,fileName)
#add metadata
f = open(fileName, 'r')
filedata = f.read()
f.close()
# Replace the placeholders with various values
filedata = filedata.replace('{{Title}}', highlight['title'])
filedata = filedata.replace('{{created}}', highlight['created'])
filedata = filedata.replace('{{tags}}', " ".join("%s " % s for s in highlight['tags']))
filedata = filedata.replace('{{url}}', highlight['link'])
f = open(fileName, 'w')
f.write(filedata)
f.close()
# add the highlight to the end
with open(fileName, 'a+') as f:
f.write("\n > " + highlight['text']+ "\n")
#updates the tag if the application is not in debug mode
if debug==0:
highlight['tags'].append('_debug_processed')
updateTags = requests.put('https://api.raindrop.io/rest/v1/raindrop/'+str(highlight['raindropRef']),
headers={"Authorization": "Bearer " + token},
json={"tags": highlight['tags']})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment