For some reason I do not understand, Pocket seems to have no support for batch adding articles to the reading list. After looking around quite a bit it doesn’t even seem possible to get a simple add url so you can add them with a little batch script…
Since I still wanted to have my articles added I have written a little script to do so. It requires the Pocket library (pip install pocket) and works out of the box beyond that.
Save as add_to_pocket.py and make sure you replace the consumer_key in the code. You can get the key from http://getpocket.com/developer/apps/new
[python]#!/usr/bin/env python
import sys
from pocket import Pocket
consumer_key = ‘18520-5994fa9c319241166deaf6b4′
request_token = Pocket.get_request_token(
consumer_key=consumer_key,
redirect_uri=’http://wol.ph/’,
)
auth_url = Pocket.get_auth_url(
code=request_token,
redirect_uri=’http://wol.ph/’,
)
print(‘Please authorize the app using the following url and press ENTER here’)
print(auth_url)
input()
access_token = Pocket.get_access_token(
consumer_key=consumer_key,
code=request_token,
)
print(‘Got authenticated request token’)
print(request_token)
def add_url(url):
print(‘adding’, url)
print(pocket_instance.add(url=url))
pocket_instance = Pocket(consumer_key, access_token)
for arg in sys.argv[1:]:
if arg.startswith(‘http’):
add_url(arg)
else:
print(‘Reading urls from %r’ % arg)
for line in open(arg):
add_url(line)
[/python]
Usage is quite simple:
[bash]python add_to_pocket.py http://w.wol.ph/2013/09/18/batch-adding-data-to-pocket/[/bash]
Or if you have a list of urls:
[bash]python add_to_pocket.py urls.txt[/bash]
If people are interested I can make it into a web-app, but for the time being it solved my issue 🙂
I can’t believe they don’t have this available somewhere in the interface…
Hi there,
I tried your code, but got some error message that I don’t really understand. Can you help please? Thanks.
Here is the error message from the terminal.
Traceback (most recent call last):
File “add_to_pocket.py”, line 23, in
code=request_token,
File “/Library/Python/2.7/site-packages/pocket.py”, line 326, in get_access_token
return cls._make_request(url, payload, headers)[0][‘access_token’]
File “/Library/Python/2.7/site-packages/pocket.py”, line 141, in _make_request
‘%s. %s’ % (error_msg, extra_info)
pocket.RateLimitException: User was authenticated, but access denied due to lack of permission or rate limiting. User rejected code.
Hey Paul. I think you haven’t created a consumer key and a request token. The code doesn’t work directly. You’re supposed to add your credentials, if you haven’t already.
Did you ever get this sorted out, Paul?
@Paul: as the error states, you are being rate limited. Ido not know what the limits are but I assumed there would be some. Just try again after a few hours.
Hey, thanks!
To any other python noobs out there, don’t try to save this as “pocket.py”… oops. That makes it try to import itself.
I’m having the same error as Paul with this code, but the rate limit business makes zero sense to me, as I have added nothing with the script yet!
Hi Ethan, I’ve just tried to create a new application through the developer apps site and added the consumer-key in the code and it works like a charm for me.
Perhaps the difference is in the checkboxes when creating the app. When creating the app (http://getpocket.com/developer/apps/new) I checked the following:
Permissions: add
Platforms: Desktop (other)
Let me know if you have more problems, you can also mail me at wolph[at]wol.ph
Hi, I used your code to create my own version of adding new links to my pocket, which I intend to run automatically on a weekly basis. But I wonder – is there a way how to authorize my app automatically without manual confirmation?
If you store the request_token instead of requesting it every time you can skip the authentication