View Single Post
Old 03-19-2023, 01:18 PM   #21
CasualBookworm
Junior Member
CasualBookworm doesn't litterCasualBookworm doesn't litter
 
Posts: 3
Karma: 168
Join Date: Mar 2023
Device: Kobo Aura Edition 2
First off, I want to extend a huge thank you for this script! I discovered this thread a few weeks back and, as an avid Pocket user, it has been a game-changer!

Also as an avid Pocket user, I've been saving articles since back when the service was called Read It Later and I only read maybe 70% of what I save, so I have a pretty ridiculous backlog. Running the script against every article wasn't seeming ideal for me, so I made a few tweaks that I wanted to share back:

Code:
#!/bin/sh

usage()
{
cat << EOF
usage: $0 [ -d DAYS ]

Pocket Image Fix:
Converts gif, png, svg, and webp images in downloaded Pocket articles to jpeg
so that they render correctly in the Kobo reader

OPTIONS:
  -d DAYS          only process articles downloaded in the last DAYS days
  -h               prints this help message
EOF
}

# Setup
ARTICLES="/mnt/onboard/.kobo/articles"
POCKET="/mnt/onboard/.adds/pocket"

CONVERT="${POCKET}/magick convert -limit time 60 -font ${POCKET}/fonts/DejaVuSans.ttf"
IDENTIFY="${POCKET}/magick identify -limit time 60 -quiet"

LD_LIBRARY_PATH="${POCKET}/lib:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH

# Prepare the directory that flags articles as already processed
FLAGS="${POCKET}/processed_articles"
(mkdir -p "${FLAGS}")

# Parse arguments
unset ARG_DAYS

while getopts 'd:h' c
do
  case $c in
    d) 
        if ! [ "$OPTARG" -gt 0 ] 2> /dev/null; then
          echo "Invalid arg for -d option! Expected a positive integer, got: $OPTARG"
          usage
          exit 1
        fi
        ARG_DAYS=$OPTARG
        ;;
    *)
        usage
        exit 0
        ;;
  esac
done

mtime=""
if [ -n "$ARG_DAYS" ]; then
  mtime="-mtime -$ARG_DAYS"
  echo "Processing articles downloaded in the last $ARG_DAYS day(s)"
else
  echo "Processing all articles; this may take a long time"
fi

# Counters
skippedd=0
processedd=0

skippedf=0
convertedf=0
failedf=0

# Main Loop
echo "Started: $(date)"

for d in $(find $ARTICLES -mindepth 1 -type d $mtime); do
  dir=$(basename $d)
  if [ -f "${FLAGS}/$dir" ] ; then
    skippedd=`expr $skippedd + 1`
  else
    for i in $(find $d -type f -not -iname "*.html"); do
      FORMAT=$($IDENTIFY -format "%m" "$i")
      if [ "$FORMAT" == "GIF" ] ||
         [ "$FORMAT" == "PNG" ] ||
         [ "$FORMAT" == "SVG" ] ||
         [ "$FORMAT" == "WEBP" ]; then
        $CONVERT "$i" "$i.jpg"
        if [ $? -eq 0 ]; then
          mv "$i.jpg" "${i%.jpg}"
          convertedf=`expr $convertedf + 1`
        else
          failedf=`expr $failedf + 1`
        fi
      else
        skippedf=`expr $skippedf + 1`
      fi
    done
    processedd=`expr $processedd + 1`
    touch "${FLAGS}/$dir"
  fi
done

# Summary
echo "Finished: $(date)"
echo
echo "Articles Processed: $processedd"
echo "Articles Skipped: $skippedd"
echo
echo "Files Converted: $convertedf"
echo "Files Not Converted: $skippedf"
echo "Files Failed: $failedf"
The main changes are:
  • After processing an article, it creates an empty file in /mnt/onboard/.adds/pocket/processed_articles as a flag and on subsequent runs it will check that directory and skip over any articles that are flagged as already having been processed
  • It accepts a command line argument that limits it to looking at articles downloaded in the last X days
  • It outputs a little summary when finished

Here's how I'm running it from my NickelMenu config:

Code:
menu_item  :library  :Pocket Images > Convert  :cmd_spawn   :quiet:/mnt/onboard/.adds/pocket/fix.sh -d 7 > /mnt/onboard/.adds/pocket/log
  chain_success                                :dbg_toast   :Started processing articles
menu_item  :library  :Pocket Images > Status   :cmd_output  :1000:tail /mnt/onboard/.adds/pocket/log
The "Convert" item starts processing anything downloaded in the last 7 days and writes the output to a log file. The "Status" item displays that log file so you can check and see if/when it finished along with details on what all it did

Two things I've considered adding but haven't really needed yet are:
  • A way to clear out old items in the processed_articles folder so it doesn't keep growing as new articles get added
  • A way to limit the overall number of articles processed in a given run (because the first time you sync a new device everything will be downloaded in the last X days)
CasualBookworm is offline   Reply With Quote