Skip to content

Instantly share code, notes, and snippets.

@conorsg
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conorsg/2ada66d135c7fffc1a51 to your computer and use it in GitHub Desktop.
Save conorsg/2ada66d135c7fffc1a51 to your computer and use it in GitHub Desktop.
counts how many unread articles you have in your pocket queue each day. see: http://conorgaffney.com/2015/04/12/pocket-shame/
#!/usr/bin/python
import json
import urllib3
import certifi
from datetime import datetime, date, timedelta
from time import strftime
simple_data = []
record = []
# connect to pocket
consumer_key = 'YOUR_CONSUMER_KEY'
access_token = 'YOUR_ACCESS_TOKEN'
url = 'https://getpocket.com/v3/get' + '?consumer_key=' + consumer_key + '&access_token=' + access_token + '&state=all&detailType=simple'
pool = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where()
)
response = pool.urlopen('GET', url)
data = json.loads(response.data)
# make a simplified list of all your items
for k, v in data['list'].iteritems():
added = datetime.fromtimestamp(int(v['time_added'])).strftime('%Y-%m-%d')
if int(v['time_read']) == 0:
read = None
else:
read = datetime.fromtimestamp(int(v['time_read'])).strftime('%Y-%m-%d')
entry = {'added' : added, 'read': read}
simple_data.append(entry)
# count read and unread by date
start_date = min([d['added'] for d in simple_data])
start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
today = date.today()
delta = today - start_date
delta = timedelta(delta.days + 1)
date_range = [start_date + timedelta(days = i) for i in range(delta.days)]
for d in date_range:
date = d.strftime('%Y-%m-%d')
count = 0
for s in simple_data:
if s['added'] <= date and s['read'] > date or s['read'] == None:
count = count + 1
entry = {'date' : date, "unread" : count}
record.append(entry)
# save to json file
with open('record.json', 'r+') as record_file:
record_file.seek(0)
json.dump(record, record_file)
record_file.truncate()
record_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment