Содержание

Качаем ролики Формулы-1

По сравнению с прошлым годом (см. пост за прошлый год) алгоритм скачивания гоночных роликов изменился: грабить видео с сайта себе на диск стало легче!

Метод "ручками"

Спасибо товарищу slontus за подсказку. Для работы понадобится пакет rtmpdump. Действуем:

rtmpdump --rtmp "rtmpt://nativtv.fcod.llnwd.net:80/a5558/o43/mp4:f1/live/assets_from_mio/assets/03a33bc2-cba4-4022-ab21-f6e4413a8c8a.f4v" -o "13 - FORMULA 1 GRAN PREMIO SANTANDER D'ITALIA 2011.mkv"

Скрипт

Скрипт для скачивания всех послегоночных роликов (спасибо Стасу за помощь в создании XPath-запросов!). Зависимости: rtmpdump, xmlstarlet. Перед запуском надо поменять путь, куда будут сохраняться ролики.

/home/nikolay/bin/f1
#!/bin/bash
#
 
# Exit if any error occurs
set -e
 
# TODO
# check arguments
# print help if any
# check if writeable paths
# check if xmlstarlet is available
# check if rtmpdump is available
# add quiet and normal mode
 
YEAR=2012
VIDEO_PATH="/opt/pub/Видео/f1/$YEAR"
ASSETS_URL="http://cdn.f1.services.nativ-systems.com/xml/2011_assets.xml"
ASSETS_FILE=2011_assets.xml
QUIET="true"
 
mkdir -p "$VIDEO_PATH"
cd "$VIDEO_PATH"
 
# Download assets file
if [ "${QUIET}" = "true" ]
	then
	wget --quiet --output-document="$ASSETS_FILE" "$ASSETS_URL"
	else
	echo "Downloading assets file..."
	wget --output-document="$ASSETS_FILE" "$ASSETS_URL"
	echo "Done"
fi
 
# Set field delimeter to "New Line"
IFS=$'\n'
# Get array of already downloaded files
FILES=( $(ls -1 *.mkv) )
# Strip file names to position only
for i in $(seq 0 $((${#FILES[*]} - 1)))
	do
	FILES[$i]="${FILES[$i]% - *}"
	if [ "${QUIET}" != "true" ]; then echo "Found video file: ${FILES[$i]}"; fi
done
 
if [ "${QUIET}" != "true" ]; then echo "Found video files: ${FILES[@]}"; fi
 
# Set field delimeter to "New Line"
IFS=$'\n'
# Get array of race positions for which race edits available
RACE_EDITS=( $(xmlstarlet sel -T -t -m "/races/race_period[year='${YEAR}']/./assets/asset[@category='RaceEdit']/../.." -v "string(position)" -n $ASSETS_FILE) )
if [ "${QUIET}" != "true" ]; then echo "Race positions with all race edits available: ${RACE_EDITS[@]}"; fi
 
if [ "${QUIET}" != "true" ]
	then
	echo "Race edits available: "
	echo "${RACE_EDITS[@]}"
	echo -n "Total number of all race edits available: "
	echo ${#RACE_EDITS[@]}
fi
 
# Unset (assign to 'null') array items that already downloaded
for x in $(seq 0 $((${#RACE_EDITS[*]} - 1)))
	do
	for y in $(seq 0 $((${#FILES[*]} - 1)))
		do
		if [ "${RACE_EDITS[$x]}" == "${FILES[$y]}" ]
			then
			unset RACE_EDITS[$x]
		fi
	done
done
if [ "${QUIET}" != "true" ]; then echo "New race edits: ${RACE_EDITS[@]}"; fi
 
# Create a new array without 'null' elements 
RACE_DOWNLOAD=( ${RACE_EDITS[@]} )
if [ "${QUIET}" != "true" ]; then echo "New race edits to download: ${RACE_DOWNLOAD[@]}"; fi
 
if [ "${QUIET}" != "true" ]
	then
	echo -n "Race edits not downloaded: "
	echo ${RACE_DOWNLOAD[@]}
	echo -n "Total number of race edits to download: "
	echo ${#RACE_DOWNLOAD[@]}
fi
 
if [ "${#RACE_DOWNLOAD[@]}" = "0" ]
	then
	exit 0
fi
 
# Download new race edits
for i in $(seq 0 $((${#RACE_DOWNLOAD[*]} - 1)))
	do
	FILE_NAME=$(xmlstarlet sel -T -t -m "/races/race_period[year='${YEAR}'][position='${RACE_DOWNLOAD[$i]}']" -v "concat(position, ' - ', circuit)" -n $ASSETS_FILE)
	if [ "${QUIET}" != "true" ]; then echo "File name: ${FILE_NAME}"; fi
	DOWNLOAD_URL=$(xmlstarlet sel -T -t -m "/races/race_period[year='${YEAR}'][position='${RACE_DOWNLOAD[$i]}']/./assets/asset[@category='RaceEdit']/video[width='1920']" -v "string(url)" -n $ASSETS_FILE)
	if [ "${QUIET}" != "true" ]; then echo " Download URL: ${DOWNLOAD_URL}"; fi
	# Perform an actual download
	if [ "${QUIET}" = "true" ]
	        then
		rtmpdump --quiet --rtmp "${DOWNLOAD_URL}" --flv "${FILE_NAME}.mkv" && echo "New race edit found and downloaded: ${FILE_NAME}.mkv"
	        else
	        echo "Downloading race edit..."
		rtmpdump --rtmp "${DOWNLOAD_URL}" --flv "${FILE_NAME}.mkv" && echo "New race edit found and downloaded: ${FILE_NAME}.mkv"
	        echo "Done"
	fi
done
 
exit 0

Источники 1) 2) 3) 4)

Можно запускать этот скрипт через cron, например, каждые две недели. Если будут найдены новые ролики они автоматически скачаются, а на почту администратору будет выслан e-mail (если cron настроен). Пример файла crontab:

# m h  dom mon dow   command
0 23 7,21 * * /home/nikolay/bin/f1