Created
July 19, 2022 16:02
-
-
Save BastiTee/5c09f3b160ad7c4dedab29671316f9e3 to your computer and use it in GitHub Desktop.
A vanilla py-script to download all Pocket.com data via API.
This file contains 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
"""A vanilla py-script to download all Pocket.com data via API. | |
Read before: | |
- https://blog.getpocket.com/2012/11/introducing-the-new-pocket-api-for-developers-and-publishers/ | |
- https://getpocket.com/developer/docs/authentication | |
""" | |
import json | |
from subprocess import run | |
from typing import Tuple | |
import requests | |
POCKET_API_BASE = 'https://getpocket.com' | |
# Your app's consumer key | |
CONSUMER_KEY = '...' | |
def __auth(consumer_key: str) -> Tuple[str, str]: | |
response = requests.post(f'{POCKET_API_BASE}/v3/oauth/request', { | |
'consumer_key': consumer_key, | |
'redirect_uri': 'pocketapp1234:authorizationFinished' | |
}) | |
assert response.status_code == 200 | |
print(response.text) | |
request_token = response.text.split('=')[1] | |
auth_url = ( | |
f'{POCKET_API_BASE}/auth/authorize?' | |
+ f'request_token={request_token}' | |
+ '&redirect_uri=pocketapp1234:authorizationFinished' | |
) | |
run(['open', auth_url], check=True) | |
input('Press key once you have approved the app in the browser...') | |
response = requests.post(f'{POCKET_API_BASE}/v3/oauth/authorize', { | |
'consumer_key': consumer_key, | |
'code': request_token | |
}) | |
assert response.status_code == 200 | |
access_token = response.text.split('&')[0].split('=')[1] | |
user = response.text.split('&')[1].split('=')[1] | |
print(access_token, user) | |
return access_token, user | |
def __call( | |
access_token: str, | |
consumer_key: str, | |
output_filepath: str | |
) -> None: | |
offset = 0 | |
count = 10 | |
index = 0 | |
full_list = [] | |
while True: | |
response = requests.post(f'{POCKET_API_BASE}/v3/get', { | |
'consumer_key': consumer_key, | |
'access_token': access_token, | |
'state': 'all', | |
'count': count, | |
'offset': offset, | |
'sort': 'newest', | |
'detailType': 'complete' | |
}) | |
assert response.status_code == 200 | |
r_json = json.loads(response.text) | |
if not r_json['list']: | |
break | |
for article_key in r_json['list'].keys(): | |
article = r_json['list'][article_key] | |
index += 1 | |
full_list.append(article) | |
print(index, article['resolved_title'], article['resolved_url']) | |
offset += count | |
with open(output_filepath, 'w+') as ofh: | |
json.dump(full_list, ofh, indent=2) | |
if __name__ == '__main__': | |
access_token, user = __auth(CONSUMER_KEY) | |
__call(access_token, CONSUMER_KEY, 'pocket_archive.json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment