Skip to content

Instantly share code, notes, and snippets.

@fmartingr
Forked from dchakro/pocket2shiori.sh
Last active May 29, 2025 05:41
Show Gist options
  • Save fmartingr/88a258bfad47fb00a3ef9d6c38e5699e to your computer and use it in GitHub Desktop.
Save fmartingr/88a258bfad47fb00a3ef9d6c38e5699e to your computer and use it in GitHub Desktop.
script to import pocket entries into shiori with text, images etc. instead of plain bookmarks.
#!/bin/sh
# Extracting URLs from the exported HTML from getpocket.com/export
# In the following line $1 is the exported HTML from pocket
# which will be passed to this script as a CLI parameter
grep -Eoi '<a [^>]+>' $1 | grep -Eo 'href="[^\"]+"' | cut -d'=' -f 2- | tr -d '"' | tac > pocket2shiori.txt
# Reading the URLs one by one and adding to shiori
while IFS= read -r line; do
shiori add $line
done < pocket2shiori.txt
rm pocket2shiori.txt
exit 0
@hedonsec
Copy link

hedonsec commented Oct 4, 2022

This is a little slower, but I have used it to add tags from the export as well.

#!/bin/sh
# Extracting URLs from the exported HTML from getpocket.com/export
# In the following line $1 is the exported HTML from pocket 
# Which will be passed to this script as a CLI parameter

grep -Eoi '<a [^>]+>' $1 | tac > pocket2shiori.txt

# Reading the URLs one by one and adding to shiori, with tags, if they exist
while IFS= read -r line; do
    tags=$(echo "$line" | grep -Eoi 'tags="[^\"]+"'  | cut -d'=' -f 2- | tr -d '"')
    link=$(echo "$line" | grep -Eo 'href="[^\"]+"' | cut -d'=' -f 2- | tr -d '"' | sed 's/ /,/g')
    if [ -n "$tags" ]
    then
    shiori add "$link" -t "$tags"
    else
    shiori add "$link"
    fi

done < pocket2shiori.txt

rm pocket2shiori.txt
exit 0

@Australis86
Copy link

Australis86 commented May 29, 2025

Pocket exports are now a CSV file, so inspired by the above I came up with a working solution using csvkit to prepare the data:

#!/bin/bash
# Extract URL, title and tags from getpocket.com/export CSV export
# and pass to shiori for import ($1 is the CSV file)
# Requires csvkit

# Use csvkit to reorder the columns to something more convenient
csvcut -c "url,tags,title" $1 > pocket2shiori.csv

# Remove the header row so it's not processed by the while loop
sed -i '1d' pocket2shiori.csv

# Track errors
fail=0

# Now we can use cut
while IFS= read -r line; do
	# Extract the relevant fields
	link=$(echo "$line" | cut -d ',' -f1)
	tags=$(echo "$line" | cut -d ',' -f2)
	title=$(echo "$line" | cut -d ',' -f3-)

	# Remove any quotes around the title, since they will be added at the import step anyway
	title="${title%\"}"
	title="${title#\"}"

	# Convert tags to comma-separated since the Pocket export uses | instead
	tags=${tags//|/,}

	if [ -n "$tags" ]; then
		if [ "$link" == "$title" ]; then
			shiori add "$link" -t "$tags"
			ec=$?
		else
			shiori add "$link" -t "$tags" -i "$title"
			ec=$?
		fi
	else
		if [ "$link" == "$title" ]; then
			shiori add "$link"
			ec=$?
		else
			shiori add "$link" -i "$title"
			ec=$?
		fi
	fi
	
	# Check if the import was successful
	if [ $ec -ne 0 ]; then
		fail=1
		echo "Failed to import $link"
		echo "$link" >> pocket2shiori.failed
	fi
done < pocket2shiori.csv

if [ $fail -eq 0 ]; then
	rm pocket2shiori.csv
fi
exit $fail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment