<jupyter_start><jupyter_text>Pocket Article Downloader ---- Want to know how many articles you've read and have to read in Pocket? This code provides setup and authorization to Pocket's API. We then pull your read and unread articles and export them to CSV. For more info and additional configuration, See [Pocket API Documentation](https://getpocket.com/developer/docs/overview). -----  Authentication and Pocket Developer Setup Note: This setup may take a few minutes. Code is indebted to [What’s in your Pocket? Visualizing your Reading List with Python](https://www.twilio.com/blog/2017/09/whats-in-your-pocket-visualizing-your-reading-list-with-python.html). If any issues, refer to that article for screenshots and more details on setup.  Step 1: Intial Developer Setup * Create an app on Pocket's Developer API Portal: https://getpocket.com/developer/apps/new* Ensure you add retrieval permission* Copy your Consumer Key and add to either option 1 or 2* Option 1 (Easiest but less secure): Copy keys and store in notebook   * Option 2 (More Secure since not stored in notebook): Copy sample-credentials.json, create credentials and add keys<jupyter_code># Option 1 (Easiest but less secure):  
# Copy your keys here after each step

# CONSUMERKEY = 'add code here'
# REQUESTCODE = 'add code here'
# ACCESSTOKEN = 'add code here'
# Option 2 (More Secure since not stored in notebook): 
# Copy sample-credentials.json, create credentials 

# Uncomment lines below and add code after each step
# Copy your keys here after each step

# import json

# with open("credentials.json", "r") as file:
#    credentials = json.load(file)
#    pocket_cr = credentials['pocket']
#    CONSUMERKEY = pocket_cr['CONSUMERKEY'] # step 1 your consumer key
#    REQUESTCODE = pocket_cr['REQUESTCODE'] # step 2 your request token
#    ACCESSTOKEN = pocket_cr['ACCESSTOKEN'] # step 4 your access token
# Step 2

# Uncomment lines below and add code after each step
# Copy and update your keys after each step

# import requests
# pocket_api = requests.post('https://getpocket.com/v3/oauth/request', 
#                           data = {'consumer_key': CONSUMERKEY, 
#                                   'redirect_uri':'https://google.com'})

# uncomment line below to see your request code
# pocket_api.text
# Step 3: 

# After modify URL to add your code and visit: 
# Visit: https://getpocket.com/auth/authorize?request_token=[Your-Request-Code]&redirect_uri=https://google.com

# Copy your request code
# Step 4:

# Uncomment lines below and add code after each step
# Copy and update your keys after each step

#import requests
#pocket_auth = requests.post('https://getpocket.com/v3/oauth/authorize', 
#                            data = {'consumer_key': CONSUMERKEY, 
#                                    'code': REQUESTCODE})

# uncomment line below to see your access token code
# pocket_auth.text<jupyter_output><empty_output><jupyter_text>------  Get and Export Current, Unread Articles<jupyter_code>from pocket import Pocket, PocketException
import json
import numpy as np
import pandas as pd
# If first time running script, please read "Authentication and Pocket Developer Setup" 
# and follow steps above to update your keys and tokens

with open("credentials.json", "r") as file:
   credentials = json.load(file)
   pocket_cr = credentials['pocket']
   CONSUMERKEY = pocket_cr['CONSUMERKEY'] # step 1 your consumer key
   REQUESTCODE = pocket_cr['REQUESTCODE'] # step 2 your request token
   ACCESSTOKEN = pocket_cr['ACCESSTOKEN'] # step 4 your access token
# Setup Pocket Object
p = Pocket(
 consumer_key=CONSUMERKEY,
 access_token=<PASSWORD>
)
# Retrieve all unread
articles_dict = {}
more_articles = True
offset = 0

# Get initial 5000 articles 
lis = p.get(state="unread", count=5000)
articles_dict.update(lis[0]['list'])

unread_articles = pd.DataFrame.from_dict(articles_dict, orient='index')
# convert unix time to datetime
unread_articles['time_added'] = pd.to_datetime(unread_articles['time_added'], unit='s')
unread_articles['time_updated'] = pd.to_datetime(unread_articles['time_updated'], unit='s')

# replace zeros with nan
unread_articles.loc[unread_articles['time_favorited'] == '0','time_favorited'] = np.nan
unread_articles['time_favorited'] = pd.to_datetime(unread_articles['time_favorited'], unit='s')
# unread count
len(unread_articles)
# unread_articles.head()
# export to csv
unread_articles.to_csv('data/pocket_unread_articles.csv', index=False)<jupyter_output><empty_output><jupyter_text>----  Get and Export Read Articles<jupyter_code># Get Your Oldest Article in Pocket

oldest_date = ''
oldest_art = p.get(state="archive", count=1, sort='oldest')
oldest_article = oldest_art[0]['list']
for i in oldest_article:
    oldest_date = oldest_article[str(i)]['time_added']

# print(oldest_date) 
# oldest_article
# Retrieve all readings since oldest date

articles_dict = {}
more_articles = True
offset = 0

# Get initial 1000 articles 
print("Getting first 1000 read articles in Pocket...")
lis = p.get(since=oldest_date, state="archive", count=1000, sort='oldest')
articles_dict.update(lis[0]['list'])

# Let's Loop through additional pocket articles
while more_articles == True:
    if lis[0]['list'] == []:
        print("Completed. No More Read Articles to pull.")
        more_articles = False
        break
    else:
        offset = offset + 1000
        print("Getting an additional 1000 read articles...")
        lis = p.get(since=oldest_date, state="archive", count=1000, sort='oldest', offset=offset)
        articles_dict.update(lis[0]['list'])

# create dataframe
read_articles = pd.DataFrame.from_dict(articles_dict, orient='index')
# read_articles.columns
# read_articles.info()
# read_articles.head()
# convert unix time to datetime
read_articles['time_added'] = pd.to_datetime(read_articles['time_added'], unit='s')
read_articles['time_updated'] = pd.to_datetime(read_articles['time_updated'], unit='s')

# replace zeros with nan
read_articles.loc[read_articles['time_favorited'] == '0','time_favorited'] = np.nan
read_articles['time_favorited'] = pd.to_datetime(read_articles['time_favorited'], unit='s')
read_articles['time_read'] = pd.to_datetime(read_articles['time_read'], unit='s')
# total  articles read
len(read_articles)
# save to csv
read_articles.to_csv("data/pocket_read_articles.csv", index=False, encoding='utf-8')
print("Read Articles Exported to CSV")<jupyter_output>Read Articles Exported to CSV
{'fraction_non_alphanumeric': 0.11059407476345587, 'fraction_numerical': 0.010392430587870326, 'mean_word_length': 4.767441860465116, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 8, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '564559', 'license': 'MIT', 'n_tokens_mistral': 2090, 'n_tokens_neox': 1915, 'n_words': 597, 'path': 'pocket/pocket_downloader.ipynb', 'repo_name': 'Zackhardtoname/qs_ledger', 'size': '6448'}