-
-
Save Amar1729/fe1f7c4315e353f2013ed5d3c581021d to your computer and use it in GitHub Desktop.
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 | |
''' | |
Import Google Reader starred items to Pocket | |
============================================ | |
This script will read the list of starred items generated by exporting your | |
Reader data using [Google Takeout][]. Generally you will use it like this: | |
python import-to-pocket.py starred.json | |
Where `starred.json` is the file containing your starred items. | |
Authentication | |
============== | |
You will need to populate the file `pocket.yml` with your Pocket OAUTH | |
credentials, like this: | |
consumer_key: YOUR_CONSUMER_KEY | |
access_token: YOUR_ACCESS_TOKEN | |
Getting a consumer key | |
---------------------- | |
You will need to visit <http://getpocket.com/developer/apps/new> and create | |
a new application. | |
Getting an access_token | |
----------------------- | |
Get an OAUTH request token: | |
curl -k -d consumer_key=CONSUMER_KEY \ | |
-d redirect_uri=http://localhost/ \ | |
https://getpocket.com/v3/oauth/request | |
This will return a request token: | |
code=REQUEST_TOKEN | |
Insert that token (everything after `code=`) into the following URL and | |
paste it into your browser: | |
https://getpocket.com/auth/authorize?request_token=REQUEST_TOKEN&redirect_uri=http://localhost/ | |
After authorizing access to your Pocket data, convert the request token | |
into an access token: | |
curl -k -d consumer_key=CONSUMER_KEY \ | |
-d code=REQUEST_TOKEN \ | |
https://getpocket.com/v3/oauth/authorize | |
This will return an access_token: | |
access_token=ACCESS_TOKEN&username=larsks | |
Place this access token and the consumer key into `pocket.yml`. | |
Details of this process are at | |
<http://getpocket.com/developer/docs/authentication>. | |
API Limits | |
========== | |
Note that Pocket [rate limits][] API requests to 320/hour. By default, this script | |
will load items as fast as possible, which means it will ultimately bail | |
out if you have more than 320 starred items. Just wait a bit and start it | |
again and it will pick up where it left off. | |
You can pass the `-R` command line flag to have the script limit requests | |
to stay under the API limit. This should allow it to run unattended until | |
completion, but you'll only get a new item every 10 seconds or so. | |
[google takeout]: https://www.google.com/takeout/ | |
[rate limits]: http://getpocket.com/developer/docs/rate-limits | |
''' | |
import os | |
import argparse | |
import json | |
import requests | |
import time | |
import yaml | |
def parse_args(): | |
p = argparse.ArgumentParser() | |
p.add_argument('--ratelimit', '-r', default=320, | |
help='API rate limit as number of requests/hour') | |
p.add_argument('--config', '-f', default='pocket.yml') | |
p.add_argument('--enforce-limit', '-R', action='store_true') | |
p.add_argument('input') | |
return p.parse_args() | |
def main(): | |
opts = parse_args() | |
with open(opts.config) as fd: | |
cfg = yaml.load(fd) | |
access_token = cfg['access_token'] | |
consumer_key = cfg['consumer_key'] | |
interval = (60*60) / int(opts.ratelimit) | |
with open(opts.input) as fd: | |
starred = json.load(fd) | |
try: | |
with open('restarturl') as fd: | |
restart_at_url = fd.read().strip() | |
print('will restart at', restart_at_url) | |
os.unlink('restarturl') | |
except OSError: | |
restart_at_url = None | |
state=0 | |
for item in starred['items']: | |
url = item['url'] # should fail if no url | |
if restart_at_url and url == restart_at_url: | |
state = 1 | |
data = { | |
'access_token': access_token, | |
'consumer_key': consumer_key, | |
'url': url, | |
'tags': item['tags'], | |
} | |
print(f"{url}") | |
r = requests.post('https://getpocket.com/v3/add', | |
data = json.dumps(data), | |
headers = { | |
'Content-type': 'application/json; charset=UTF-8', | |
'X-accept': 'application/json', | |
}, | |
) | |
try: | |
r.raise_for_status() | |
except: | |
open('restarturl', 'w').write(url) | |
raise | |
if opts.enforce_limit: | |
time.sleep(interval) | |
if __name__ == '__main__': | |
main() |
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 | |
""" | |
Download a full list of your reddit saved and upvoted items and saved into output.json. | |
The JSON format used is expected by ./import-to-pocket.py, so it can be imported to pocket easily. | |
Authentication | |
============== | |
This script uses PRAW, the official Reddit python API, for getting your upvoted and saved posts. | |
To use it, you'll have to create an app: | |
https://github.com/reddit-archive/reddit/wiki/OAuth2-Quick-Start-Example | |
Only need first steps with PRAW (client ID and secret); don't need an auth token. | |
Configuration | |
============= | |
Save the `id`, `sec`, and your `username` and `password` into reddit.yml | |
(using those keys). This script will read the yaml and download all posts to | |
output.json, which can be edited before you run ./import-to-pocket.py. | |
""" | |
import itertools | |
import json | |
import uuid | |
import praw | |
import yaml | |
USER_AGENT = str(uuid.uuid4()) | |
CONFIG = 'reddit.yml' | |
def parse_tags(url): | |
if url.startswith("/r/"): | |
url = url[3:] | |
return url.split('/', 1)[0] | |
def main(): | |
with open(CONFIG) as fd: | |
cfg = yaml.load(fd) | |
username = cfg['username'] | |
password = cfg['password'] | |
_id = cfg['id'] | |
sec = cfg['sec'] | |
reddit = praw.Reddit( | |
client_id=_id, | |
client_secret=sec, | |
password=password, | |
username=username, | |
user_agent=USER_AGENT, | |
) | |
me = reddit.user.me() | |
upvoted = [ | |
{"url": "https://www.reddit.com" + p.permalink, "tags": parse_tags(p.permalink) + ",reddit,upvoted"} | |
for p in me.upvoted() | |
] | |
saved = [ | |
{"url": "https://www.reddit.com" + p.permalink, "tags": parse_tags(p.permalink) + ",reddit,saved"} | |
for p in me.saved() | |
] | |
with open("output.json", "w") as f: | |
json.dump({"items": list(itertools.chain(upvoted, saved))}, f) | |
main() |
Note - for anyone else using this, the script does NOT sort by timestamp of post (in fact, the reddit api will return your posts with most recent first?). So running this sync will result in your oldest reddit saved/upvoted being at the front of your pocket list, unless you sorted(reverse=True)
the list somewhere before pocket API calls.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated to python3, and updated the import script to be able to import reddit posts you've upvoted/saved.
meant to be used as a one-time conversion; I use IFTTT for transferring liked/saved posts on my reddit account to Pocket continuously.