Practical Programming

Practical Automation: Deleting 15,000+ Pocket Entries by Tag with Python

Python, HTTP GET/POST, and Pocket API team up to do what the GUI could never do.

Braden Wong
Optim

--

Today marks my first self-directed project of 2020 — using Python, JSON, and HTTP GET/POST requests to tackle the ancient Pocket API (last updated in 2012).

Last Updated 2012, ancient by today’s standards.

Background to my situation:

  • My IFTTT recipe sent top posts from r/todayilearned to Pocket daily for over 3 years, generating over 5,000 entries
  • Pocket is unusable, especially with their clunky bulk edit interface (there is no “select all” button!)
  • I wish to move all entries tagged “TIL” to Google Sheets and delete them from Pocket
The goal is to delete all of this (over 5,000 entries).

Biggest Takeaways:

  • In about three hours, I went from zero experience with API’s and HTTP POST/GET to a tangible working Python script
  • Project-based learning is the most satisfying approach to learn coding
  • Automation with Python is way easier than you think

How To Use

Source code may be found here: https://github.com/bmw02002turbo/Pocket-Scraping.

Prerequisites: Set up a consumer key under Pocket Developer, install requests Python Module through pip.

  1. Modify “0. Pocket Web TIL Scraper.json” to match the properties of Pocket entries that you want to delete.
  2. Run “1. Post Request to Retrieve Pocket.py” to generate JSON Data of matching Pocket entries as “2. JSON Data (Pocket Articles Tagged TIL).json”.
  3. Run “3. Generate Deletion Commands.py” to generate delete commands for Pocket entries as “4. JSON Delete Commands (Pocket Articles Tagged TIL).json”.
  4. Run “5. Post Request to Delete Pocket.py” to delete and generate server output as “6. Post Request Output.txt”.
  5. Hopefully your output file should generate “action_results”:[true,true,…]∎! Enjoy!

What I did:

First, copy favorite Pocket entries to a Google Sheet:

  1. Sent the Following HTTP Get Request:

Request URL: https://getpocket.com/v3/get
Header: “Content-Type: application/json”
Body:

{“consumer_key”:”<consumer_key>",“access_token”:"<access_token>",“favorite”:”1",“tag”:”til”,“detailType”:”simple”}

2. Received JSON data from aforementioned request

3. Scraped the “resolved_title” (and any other desired property) off each object in “list” and copied them to a Google Sheet. ∎

Second and finally, delete all Pocket entries tagged “TIL”:

  1. Sent the HTTP Get Request (same as above, but removed “favorite” line)
  2. Received JSON data from aforementioned request
  3. Created a Python script to generate delete commands for each entity in 2), outputting commands to a JSON file

4. Received JSON delete commands outputted from Python script in 3)

5. Created a Python script to use delete commands in 4) and generate success/failure in an output file

6. Enjoy when the output file is generated with “action_results”:[true,true,…]∎

Since completing Harvard’s CS50, this has been the most satisfying coding project I have done yet.

Conclusion

If you know Python, seriously consider using it with API’s/modules to to automate your tasks. Doing so saves you time with that tasks and teaches you Python along the way.

--

--

Braden Wong
Optim
Editor for

I’m a writer with an ambition: to share the lessons I have learned for all who may listen, one step at a time. You have to start somewhere.