This commit is contained in:
Timothy Allen 2015-06-22 23:38:04 +02:00
commit 8be979a8d8
2 changed files with 100 additions and 2 deletions

View File

@ -1,5 +1,17 @@
#!/usr/bin/env python
# Back up the state of MPD (current playlist and so on)
#
# Back up the state of MPD (current playlist and so on); this allows you
# to recover when you accidentally clear your playlist.
#
# This uses the python-mpd2 library from <https://pypi.python.org/pypi/python-mpd2>:
# sudo pip3 install python-mpd2
#
# I run it from cron every 20 minutes or so (as user mpd, but you can
# run it as any user, passing in the backup directory with -d):
# cat /etc/cron.d/mpd_backup
# */20 * * * * mpd [ -x /var/lib/mpd/backup_mpd_playlist.py ] && python /var/lib/mpd/backup_mpd_playlist.py
#
# By Timothy Allen <tim@treehouse.org.za>
import ast
import datetime
@ -129,7 +141,7 @@ else:
prev = strip.sub('', backup_output)
# Only copy if the current playlist has changed
if current_output != backup_output:
if curr != prev:
new_backup_file = os.path.join(backup_dir, 'mpd_state' + "-" + now)
# Write backup
new_backup = open(new_backup_file, 'w')

86
ttbin2mysports.sh Executable file
View File

@ -0,0 +1,86 @@
#!/bin/sh
#
# Upload a TTBIN file (after converting to TCX) to the
# TomTom MySports website, using http://www.mapmyfitness.com/
#
# Set the USERNAME and PASSWORD variables to your MapMyFitness
# credentials to log in to the MapMyFitness site
USERNAME="username@mapmyfitness"
PASSWORD="password@mapmyfitness"
#
# Set TTWATCH_DIR to the absolute location of your ttwatch data. This
# directory should contain a directory named for your device (one
# directory per device), which in turn should contain directories of the
# form "YYYY-MM-DD", which should hold tcx or ttbin files
TTWATCH_DIR=~/ttwatch
COOKIE_JAR=${TTWATCH_DIR}/mapmyfitness-cookies.txt
#
LOGIN_PAGE=https://www.mapmyfitness.com/auth/login/
UPLOAD_PAGE=https://www.mapmyfitness.com/device/file-upload
verbose=0
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts "d:v?h" opt; do
case "$opt" in
h|\?)
echo "Usage: $0 [-v] [-d <watch name>]"
exit 0
;;
v) verbose=1
curl_args="-v"
;;
d) device=$OPTARG
;;
esac
done
if [ ! -d "${TTWATCH_DIR}"/ ]; then
echo "The directory ${TTWATCH_DIR} does not exist."
exit;
fi
if [ "${device}" ] && [ -d "${TTWATCH_DIR}/${device}" ]; then
DEVICE_DIR="${TTWATCH_DIR}/${device}"
elif [ "${device}" ]; then
echo "Invalid device name ${device}"
exit;
elif [ "$( find "${TTWATCH_DIR}" -mindepth 1 -maxdepth 1 -type d | wc -l )" -eq 1 ]; then
DEVICE_DIR="$( find "${TTWATCH_DIR}" -mindepth 1 -maxdepth 1 -type d )"
else
echo "Multiple devices; please pass one using the -d <watch name> option"
exit;
fi
for dir in "${DEVICE_DIR}"/*/; do
for ttbin_file in "${dir}"/*.ttbin; do
file=$(basename "${ttbin_file}" .ttbin)
tcx_file=${file}.tcx
if [ ! -f "${dir}/.${file}-uploaded_to_mysports" ]; then
if [ ! -f "${dir}/${file}.tcx" ]; then
[ ${verbose} -eq 1 ] && echo "Converting ${file}.ttbin to ${file}.tcx..."
( cd "${dir}" && ttbincnv -t "${dir}/${file}.ttbin" )
fi
# Initialise the cookie, as the MapMyFitness website will return 400 otherwise
curl -c ${COOKIE_JAR} -b ${COOKIE_JAR} -s -o /dev/null \
${LOGIN_PAGE}
[ $? -eq 0 ] || exit;
curl ${curl_args} -c ${COOKIE_JAR} -b ${COOKIE_JAR} --max-redirs 10 \
--form-string "csrfmiddlewaretoken=" \
--form-string "email=${USERNAME}" \
--form-string "password=${PASSWORD}" \
${LOGIN_PAGE}
[ $? -eq 0 ] || exit;
curl ${curl_args} -c ${COOKIE_JAR} -b ${COOKIE_JAR} --max-redirs 10 \
--form "file_to_upload=@\"${dir}/${tcx_file}\"" \
${UPLOAD_PAGE}
if [ $? -eq 0 ]; then
[ ${verbose} -eq 1 ] && echo "Creating ${dir}/.${file}-uploaded_to_mysports"
date > "${dir}/.${file}-uploaded_to_mysports"
fi
else
[ "${verbose}" -eq 1 ] && echo "${tcx_file} has already been uploaded from $(basename "$dir")"
fi
done
done
exit 0;