Skip to content

Instantly share code, notes, and snippets.

@vulpes-vulpeos
Created September 30, 2023 07:24
Show Gist options
  • Save vulpes-vulpeos/a4670cc36c4b4e2903c16f50671bd2f4 to your computer and use it in GitHub Desktop.
Save vulpes-vulpeos/a4670cc36c4b4e2903c16f50671bd2f4 to your computer and use it in GitHub Desktop.
Simple bash script to sync freetube watch history with youtube using yt-dlp.
# !/usr/bin/env bash
# 1. Restart freetube app.
# 2. Get youtube cookies with "Get cookies.txt LOCALLY" browser extension
# and put "www.youtube.com_cookies.txt" file near this script.
# Use fresh cookies every sync.
# 3. Make script executable with chmod +x "/path/to/script" command.
# 4. Launch script.
# 2100+ videos where marked in 45+ min.
# Change PATH_TO_HISTORY_DB to your path:
# - macOS: $HOME/Library/Application Support/FreeTube/history.db
# - linux: $HOME/.config/FreeTube/history.db
# - flatpack: $HOME/.var/app/io.freetubeapp.FreeTube/config/FreeTube/history.db
PATH_TO_HISTORY_DB="$HOME/Library/Application Support/FreeTube/history.db"
SCRIPT_DIR=`dirname "$0"`
PATH_TO_COOKIES="$SCRIPT_DIR/www.youtube.com_cookies.txt"
PATH_TO_MARKED_LIST="$SCRIPT_DIR/marked_list.txt"
MARKED_LIST=()
MARKED_COUNTER=0
# Check if marked_list.txt already exist in script folder
if [[ -f "${PATH_TO_MARKED_LIST}" ]]
then # Fill MARKED_LIST array if exist
MARKED_LIST=($(cat $PATH_TO_MARKED_LIST))
else # Create marked_list.txt if it doesn't exist.
touch $PATH_TO_MARKED_LIST
fi
# Reading history.db file
while read line; do
line=${line:12:11} # Get video id (11 chars after removing first 12 chars)
# Check if MARKED_LIST doesn't contain video id
if [[ ! ${MARKED_LIST[@]} =~ $line ]]
then # Not found
MARKED_LIST+=($line) # Add video id to array
echo "Marking watched: $line"
yt-dlp --cookies "${PATH_TO_COOKIES}" --mark-watched --simulate "https://www.youtube.com/watch?v=$line" | grep -i "ERROR"
echo "$line" >> $PATH_TO_MARKED_LIST # Append video id into marked_list.txt
((++MARKED_COUNTER))
fi
done < $PATH_TO_HISTORY_DB
echo "$MARKED_COUNTER video(s) marked as watched."
@milindpatel63
Copy link

script says "xxx video marked as watched.", but when i go to youtube website, it's still unwatched.

@milindpatel63
Copy link

Here's a modified script.

This one uses oauth2 instead of cookies (since cookies were creating an issue where they needed to be fresh every time)

You will need to install oauuth2 plugin for yt-dlp from here: https://github.com/coletdjnz/yt-dlp-youtube-oauth2 and login into it.

Also, i noticed, if i imported my watched history in freetube from exporting it via youtube, the history.db file has a little different format on each line. The title is first instead of videoId, so the above wway to get videoId was not working. I'm using a different method in this script.

# !/usr/bin/env bash

# 1. Restart freetube app.
# 2. Install yt-dlp oauth2 plugin from here: https://github.com/coletdjnz/yt-dlp-youtube-oauth2
# 3. Make script executable with chmod +x "/path/to/script" command.
# 4. Launch script. 
# 5. On first run, yt-dlp will login via oauth2. Enter the provided code at https://www.google.com/device
#    2100+ videos where marked in 45+ min.

# Change PATH_TO_HISTORY_DB to your path:
# - macOS: $HOME/Library/Application Support/FreeTube/history.db
# - linux: $HOME/.config/FreeTube/history.db
# - flatpack: $HOME/.var/app/io.freetubeapp.FreeTube/config/FreeTube/history.db
PATH_TO_HISTORY_DB="$HOME/.var/app/io.freetubeapp.FreeTube/config/FreeTube/history.db"

SCRIPT_DIR=`dirname "$0"`
PATH_TO_MARKED_LIST="$SCRIPT_DIR/marked_list.txt"
MARKED_LIST=()
MARKED_COUNTER=0

# Check if marked_list.txt already exist in script folder
if [[ -f "${PATH_TO_MARKED_LIST}" ]]
then # Fill MARKED_LIST array if exist
	MARKED_LIST=($(cat $PATH_TO_MARKED_LIST))
else # Create marked_list.txt if it doesn't exist.
	touch $PATH_TO_MARKED_LIST
fi

# Reading history.db file
while read line; do
	video_id=$(echo "$line" | grep -o '"videoId":"[^"]*' | awk -F ':"' '{print $2}') # Get video id
	# Check if MARKED_LIST doesn't contain video id
	if [[ ! ${MARKED_LIST[@]} =~ $video_id ]]
	then # Not found
		MARKED_LIST+=($video_id) # Add video id to array
		echo "Marking watched: $video_id"
		yt-dlp --username oauth2 --password '' --mark-watched --simulate  "https://www.youtube.com/watch?v=$video_id" | grep -i "ERROR"
		echo "$video_id" >> $PATH_TO_MARKED_LIST # Append video id into marked_list.txt
		((++MARKED_COUNTER))
	fi
done < $PATH_TO_HISTORY_DB

echo "$MARKED_COUNTER video(s) marked as watched."

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