Skip to content

Instantly share code, notes, and snippets.

@cnicodeme
Created April 4, 2020 17:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cnicodeme/b4c04dd95d9964fd574489569b2ea65f to your computer and use it in GitHub Desktop.
Save cnicodeme/b4c04dd95d9964fd574489569b2ea65f to your computer and use it in GitHub Desktop.
Update your reading list by running this local script.
#!/usr/bin/python3
"""
This the CLI script to add tags to your reading list
You will need a Pocket application. You can create one at:
https://getpocket.com/developer/apps/new
/!\ Don't forget to set the variables defined after.
Requires Python3 and the "requests" library.
--------------------------------------------------------------------------------
MIT License
Copyright (c) 2020 minute-pocket
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--------------------------------------------------------------------------------
/!\ PLEASE FILL THE FOLLOWING VARIABLES
consumer_key = This the key you will get when creating a new APP at GetPocket.com
You can follow this link to create a new app:
https://getpocket.com/developer/apps/new
access_token = Once you have run the script the first time, you will have the access_token's value
Update it here to be able to update your reading list.
"""
consumer_key = "consumer_key obtained when creating your App"
access_token = None
import requests, json, time, urllib.parse
durations = (2, 5, 10, 15, 20, 30, 45, 60)
def err(msg):
print('\033[91mError: {0}\033[0m'.format(msg))
print('')
exit(1)
def main():
if not access_token:
oauth = requests.post('https://getpocket.com/v3/oauth/request', json={
'consumer_key': consumer_key,
'redirect_uri': 'https://pastebin.com/BgGEVrNe'
}, headers={'X-Accept': 'application/json'})
if oauth.status_code != 200:
err(oauth.content.decode('utf-8'))
oauth_code = oauth.json().get('code')
print('Open the following URL in your browser, then update the variable \"is_already_accepted\" to True')
print("https://getpocket.com/auth/authorize?request_token={0}&redirect_uri={1}".format(oauth_code, urllib.parse.quote('https://pastebin.com/BgGEVrNe', safe='')))
input('Please hit enter once you have accepted the connection on the browser.')
oauth = requests.post('https://getpocket.com/v3/oauth/authorize', json={
'consumer_key': consumer_key,
'code': oauth_code
}, headers={'X-Accept': 'application/json'})
if oauth.status_code != 200:
err(oauth.content)
result = oauth.json()
print('Your access token is: \033[1m\033[92m{0}\033[0m\033[0m'.format(result.get('access_token')))
print('Save it as a variable in this script and run it again.')
exit()
offset = 0
updated = 0
while True:
print('Loading items {0}-{1}.'.format(offset, offset + 100))
result = requests.post('https://getpocket.com/v3/get', data={
'consumer_key': consumer_key,
'access_token': access_token,
'state': 'unread',
'sort': 'newest',
'detailType': 'complete',
'contentType': 'article',
'count': 100,
'offset': offset
})
if result.headers['x-limit-key-remaining'] == 0:
print('> Rate limited. Waiting for {0} seconds.'.format(result.headers['x-limit-key-reset']))
time.sleep(int(result.headers['x-limit-key-reset']) + 1)
continue
result.raise_for_status()
items = result.json()
if len(items['list']) == 0:
break
print('> Found {0} items. Processing them...'.format(len(items['list'])))
actions = []
found_tags = 0
for key in items.get('list'):
item = items['list'].get(key)
found_tag = False
if item.get('tags', None):
for tag in item.get('tags'):
if tag.find('minutes') > -1:
found_tag = True
break
if found_tag:
found_tags = found_tags + 1
continue
duration = (int(item.get('word_count')) / 275) * 60
if item.get('image', None) is not None:
duration = duration + (len(item.get('images')) * 12)
duration = int(duration / 60)
tag_name = None
for d in durations:
if duration <= d:
tag_name = '{0} minutes'.format(d)
break
if tag_name is None:
tag_name = '60+ minutes'
actions.append({
'action': 'tags_add',
'item_id': item.get('item_id'),
'tags': tag_name
})
if len(actions) > 0:
print('> Applying tag for {0} items.'.format(len(actions)))
tag_request = requests.post('https://getpocket.com/v3/send', json={
'consumer_key': consumer_key,
'access_token': access_token,
'actions': actions
})
if tag_request.headers['x-limit-key-remaining'] == 0:
print('> Rate limited. Waiting for {0} seconds.'.format(result.headers['x-limit-key-reset']))
time.sleep(int(tag_request.headers['x-limit-key-reset']) + 1)
tag_request = requests.post('https://getpocket.com/v3/send', json={
'consumer_key': consumer_key,
'access_token': access_token,
'actions': actions
})
tag_request.raise_for_status()
updated = updated + len(actions)
if found_tags == 100:
break
offset = offset + 100
print('')
print('\033[92mUpdated {0} items.\033[0m'.format(updated))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment