Created
January 16, 2024 08:19
-
-
Save yusufhm/edb5c91a6dc70ea1edf39baf0763aa41 to your computer and use it in GitHub Desktop.
Migrate Wallabag entries to Nextcloud bookmarks
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
requests |
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
import os, requests | |
wallabag_url = os.environ.get('WALLABAG_URL') | |
wallabag_username = os.environ.get('WALLABAG_USERNAME') | |
wallabag_password = os.environ.get('WALLABAG_PASSWORD') | |
wallabag_client_id = os.environ.get('WALLABAG_CLIENT_ID') | |
wallabag_client_secret = os.environ.get('WALLABAG_CLIENT_SECRET') | |
nextcloud_url = os.environ.get('NEXTCLOUD_URL') | |
nextcloud_username = os.environ.get('NEXTCLOUD_USERNAME') | |
nextcloud_password = os.environ.get('NEXTCLOUD_PASSWORD') | |
nextcloud_folder_id = os.environ.get('NEXTCLOUD_FOLDER_ID') | |
def wallabag_get_access_token(): | |
resp = requests.post( | |
wallabag_url + '/oauth/v2/token', | |
data={ | |
'grant_type': 'password', | |
'username': wallabag_username, | |
'password': wallabag_password, | |
'client_id': wallabag_client_id, | |
'client_secret': wallabag_client_secret, | |
} | |
) | |
data = resp.json() | |
return data['access_token'] | |
def wallabag_get_entries(url=None): | |
if not url: | |
return | |
access_token = wallabag_get_access_token() | |
resp = requests.get( | |
url, | |
headers={ | |
'Authorization': 'Bearer ' + access_token, | |
} | |
) | |
data = resp.json() | |
return (data.get('_embedded', {}).get('items', []), | |
data.get('_links', {}).get('next', {'href': None}).get('href', None)) | |
def nextcloud_bookmark_exists(url): | |
resp = requests.get( | |
nextcloud_url + '/index.php/apps/bookmarks/public/rest/v2/bookmark', | |
auth=(nextcloud_username, nextcloud_password), | |
params={'url': url} | |
) | |
js_data = resp.json() | |
return len(js_data['data']) > 0 | |
def nextcloud_create_bookmark(entry): | |
requests.post( | |
nextcloud_url + '/index.php/apps/bookmarks/public/rest/v2/bookmark', | |
auth=(nextcloud_username, nextcloud_password), | |
json={ | |
'url': entry['url'], | |
'title': entry['title'], | |
'folders': [nextcloud_folder_id], | |
'tags': entry['tags'], | |
} | |
) | |
def main(): | |
next = wallabag_url + '/api/entries' | |
while next: | |
entries, next = wallabag_get_entries(next) | |
for entry in entries: | |
tags = [] | |
for tag in entry['tags']: | |
if tag['label'] == 'pocket': | |
continue | |
tags.append(tag['label']) | |
nc_entry = { | |
'title': entry['title'], | |
'url': entry['url'], | |
'tags': tags, | |
} | |
if nextcloud_bookmark_exists(nc_entry['url']): | |
print(f'skipping {nc_entry["url"]}') | |
continue | |
else: | |
print(f'adding {nc_entry["url"]}') | |
nextcloud_create_bookmark(nc_entry) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment