From 74934330e172bc08e6efe3165a54d24ba00697c4 Mon Sep 17 00:00:00 2001
From: Wonderfall <wonderfall@schrodinger.io>
Date: Sun, 4 Jun 2017 19:35:55 +0200
Subject: [PATCH] rtorrent-flood: alpine 3.6, fixes, libzen, chromaprint

---
 rtorrent-flood/Dockerfile  | 38 ++++++++++++++++++++++++++++++--------
 rtorrent-flood/postdl      | 21 +++++++++++++++++++--
 rtorrent-flood/postrm      | 10 +++++++++-
 rtorrent-flood/rtorrent.rc |  4 ++--
 rtorrent-flood/run.sh      | 15 +++++++++++----
 5 files changed, 71 insertions(+), 17 deletions(-)

diff --git a/rtorrent-flood/Dockerfile b/rtorrent-flood/Dockerfile
index 83db06e..a8ca0a7 100644
--- a/rtorrent-flood/Dockerfile
+++ b/rtorrent-flood/Dockerfile
@@ -1,9 +1,11 @@
-FROM alpine:3.5
+FROM alpine:3.6
 
 ARG RTORRENT_VER=0.9.6
 ARG LIBTORRENT_VER=0.13.6
-ARG MEDIAINFO_VER=0.7.95
+ARG MEDIAINFO_VER=0.7.96
 ARG FILEBOT_VER=4.7.9
+ARG CHROMAPRINT_VER=1.4.2
+ARG LIBZEN_VER=0.4.35
 ARG FLOOD_VER=1.0.0
 ARG BUILD_CORES
 
@@ -13,8 +15,15 @@ ENV UID=991 GID=991 \
     RTORRENT_SCGI=0 \
     PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
 
+ENV FILEBOT_RENAME_METHOD="symlink" \
+    FILEBOT_RENAME_MOVIES="{n} ({y})" \
+    FILEBOT_RENAME_SERIES="{n}/Season {s.pad(2)}/{s00e00} - {t}" \
+    FILEBOT_RENAME_ANIMES="{n}/{e.pad(3)} - {t}" \
+    FILEBOT_RENAME_MUSICS="{n}/{fn}"
+
 RUN NB_CORES=${BUILD_CORES-`getconf _NPROCESSORS_CONF`} \
- && BUILD_DEPS=" \
+ && apk -U upgrade \
+ && apk add -t build-dependencies \
     build-base \
     git \
     libtool \
@@ -28,9 +37,8 @@ RUN NB_CORES=${BUILD_CORES-`getconf _NPROCESSORS_CONF`} \
     libressl-dev \
     ncurses-dev \
     curl-dev \
-    binutils" \
- && apk -U upgrade && apk add \
-    ${BUILD_DEPS} \
+    binutils \
+ && apk add \
     ca-certificates \
     curl \
     ncurses \
@@ -43,13 +51,16 @@ RUN NB_CORES=${BUILD_CORES-`getconf _NPROCESSORS_CONF`} \
     su-exec \
     python \
     nodejs \
- && apk add -X http://dl-cdn.alpinelinux.org/alpine/v3.4/community -U openjdk8-jre==8.111.14-r0 openjdk8-jre-base==8.111.14-r0 openjdk8-jre-lib==8.111.14-r0 \
+    nodejs-npm \
+    openjdk8-jre \
+    java-jna-native \
  && cd /tmp && mkdir libtorrent rtorrent \
  && cd libtorrent && wget -qO- https://github.com/rakshasa/libtorrent/archive/${LIBTORRENT_VER}.tar.gz | tar xz --strip 1 \
  && cd ../rtorrent && wget -qO- https://github.com/rakshasa/rtorrent/archive/${RTORRENT_VER}.tar.gz | tar xz --strip 1 \
  && cd /tmp \
  && git clone https://github.com/mirror/xmlrpc-c.git \
  && git clone https://github.com/Rudde/mktorrent.git \
+ && git clone https://github.com/acoustid/chromaprint.git \
  && cd /tmp/mktorrent && make -j ${NB_CORES} && make install \
  && cd /tmp/xmlrpc-c/stable && ./configure && make -j ${NB_CORES} && make install \
  && cd /tmp/libtorrent && ./autogen.sh && ./configure && make -j ${NB_CORES} && make install \
@@ -57,8 +68,12 @@ RUN NB_CORES=${BUILD_CORES-`getconf _NPROCESSORS_CONF`} \
  && cd /tmp \
  && wget -q http://mediaarea.net/download/binary/mediainfo/${MEDIAINFO_VER}/MediaInfo_CLI_${MEDIAINFO_VER}_GNU_FromSource.tar.gz \
  && wget -q http://mediaarea.net/download/binary/libmediainfo0/${MEDIAINFO_VER}/MediaInfo_DLL_${MEDIAINFO_VER}_GNU_FromSource.tar.gz \
+ && wget -q https://github.com/MediaArea/ZenLib/archive/v${LIBZEN_VER}.tar.gz -O libzen.tar.gz \
  && tar xzf MediaInfo_DLL_${MEDIAINFO_VER}_GNU_FromSource.tar.gz \
  && tar xzf MediaInfo_CLI_${MEDIAINFO_VER}_GNU_FromSource.tar.gz \
+ && tar xzf libzen.tar.gz \
+ && cd /tmp/ZenLib-${LIBZEN_VER}/Project/GNU/Library \
+ && ./autogen.sh && ./configure --prefix=/usr --enable-shared --disable-static && make && make install \
  && cd /tmp/MediaInfo_DLL_GNU_FromSource && ./SO_Compile.sh \
  && cd /tmp/MediaInfo_DLL_GNU_FromSource/ZenLib/Project/GNU/Library && make install \
  && cd /tmp/MediaInfo_DLL_GNU_FromSource/MediaInfoLib/Project/GNU/Library && make install \
@@ -68,11 +83,18 @@ RUN NB_CORES=${BUILD_CORES-`getconf _NPROCESSORS_CONF`} \
  && strip -s /usr/local/bin/mktorrent \
  && strip -s /usr/local/bin/mediainfo \
  && mkdir /filebot && cd /filebot \
+ && wget https://github.com/acoustid/chromaprint/releases/download/v${CHROMAPRINT_VER}/chromaprint-fpcalc-${CHROMAPRINT_VER}-linux-x86_64.tar.gz \
+ && tar xvf chromaprint-fpcalc-${CHROMAPRINT_VER}-linux-x86_64.tar.gz \
+ && mv chromaprint-fpcalc-${CHROMAPRINT_VER}-linux-x86_64/fpcalc /usr/local/bin \
+ && strip -s /usr/local/bin/fpcalc \
  && wget -q https://netcologne.dl.sourceforge.net/project/filebot/filebot/FileBot_${FILEBOT_VER}/FileBot_${FILEBOT_VER}-portable.tar.xz \
  && tar xJf FileBot_${FILEBOT_VER}-portable.tar.xz && rm FileBot_${FILEBOT_VER}-portable.tar.xz \
  && mkdir /usr/flood && cd /usr/flood && wget -qO- https://github.com/jfurrow/flood/archive/v${FLOOD_VER}.tar.gz | tar xz --strip 1 \
  && npm install --production \
- && apk del ${BUILD_DEPS} \
+ && ln -sf /usr/local/lib/libmediainfo.so.0.0.0 /filebot/lib/x86_64/libmediainfo.so \
+ && ln -sf /usr/local/lib/libzen.la /filebot/lib/x86_64/libzen.so \
+ && ln -sf /usr/local/bin/mediainfo /usr/bin/mediainfo \
+ && apk del build-dependencies \
  && rm -rf /var/cache/apk/* /tmp/*
 
 COPY config.js /usr/flood/
diff --git a/rtorrent-flood/postdl b/rtorrent-flood/postdl
index 6061cf0..47a0718 100644
--- a/rtorrent-flood/postdl
+++ b/rtorrent-flood/postdl
@@ -5,6 +5,23 @@
 
 TORRENT_PATH="$1"
 TORRENT_NAME="$2"
-TORRENT_LABEL="$3"
+TORRENT_LABEL="$(echo $3 | tr '[:upper:]' '[:lower:]')"
 
-/filebot/filebot.sh -script fn:amc --output "/data/Media" --action symlink --conflict skip -non-strict --log-file amc.log --def excludeList=amc.excludes unsorted=y music=y "seriesFormat=/data/Media/TV/{n}/Season {s.pad(2)}/{s00e00} - {t}" "animeFormat=/data/Media/Animes/{n}/{e.pad(3)} - {t}" "movieFormat=/data/Media/Movies/{n}" "musicFormat=/data/Media/Music/{n}/{fn}" "ut_dir=$TORRENT_PATH" "ut_kind=multi" "ut_title=$TORRENT_NAME" "ut_label=$TORRENT_LABEL" &
+
+case $TORRENT_LABEL in
+    films|movies|film|movie)
+        /filebot/filebot.sh --db TheMovieDB -script fn:amc --output "/data/Media" --action <FILEBOT_RENAME_METHOD> --conflict skip -non-strict --log-file amc.log --def excludeList=amc.excludes unsorted=y "movieFormat=/data/Media/Movies/<FILEBOT_RENAME_MOVIES>" "ut_dir=$TORRENT_PATH" "ut_kind=multi" "ut_title=$TORRENT_NAME" "ut_label=Movie" &
+    ;;
+    music|musics|musique|musiques)
+        /filebot/filebot.sh --db ID3 -script fn:amc --output "/data/Media" --action <FILEBOT_RENAME_METHOD> --conflict skip -non-strict --log-file amc.log --def excludeList=amc.excludes unsorted=y music=y "musicFormat=/data/Media/Music/<FILEBOT_RENAME_MUSICS>" "ut_dir=$TORRENT_PATH" "ut_kind=multi" "ut_title=$TORRENT_NAME" "ut_label=Music" &
+    ;;
+    tv|"tv show"|"tv shows"|series|serie)
+        /filebot/filebot.sh --db TheTVDB -script fn:amc --output "/data/Media" --action <FILEBOT_RENAME_METHOD> --conflict skip -non-strict --log-file amc.log --def excludeList=amc.excludes unsorted=y "seriesFormat=/data/Media/TV/<FILEBOT_RENAME_SERIES>" "ut_dir=$TORRENT_PATH" "ut_kind=multi" "ut_title=$TORRENT_NAME" "ut_label=TV Show" &
+    ;;
+    anime|animes)
+        /filebot/filebot.sh --db AniDB -script fn:amc --output "/data/Media" --action <FILEBOT_RENAME_METHOD> --conflict skip -non-strict --log-file amc.log --def excludeList=amc.excludes unsorted=y "animeFormat=/data/Media/Animes/<FILEBOT_RENAME_ANIMES>" "ut_dir=$TORRENT_PATH" "ut_kind=multi" "ut_title=$TORRENT_NAME" "ut_label=Anime" &
+    ;;
+    *)
+        /filebot/filebot.sh -script fn:amc --output "/data/Media" --action <FILEBOT_RENAME_METHOD> --conflict skip -non-strict --log-file amc.log --def excludeList=amc.excludes unsorted=y music=y "seriesFormat=/data/Media/TV/<FILEBOT_RENAME_SERIES>" "animeFormat=/data/Media/Animes/<FILEBOT_RENAME_ANIMES>" "movieFormat=/data/Media/Movies/<FILEBOT_RENAME_MOVIES>" "musicFormat=/data/Media/Music/<FILEBOT_RENAME_MUSICS>" "ut_dir=$TORRENT_PATH" "ut_kind=multi" "ut_title=$TORRENT_NAME" "ut_label=$TORRENT_LABEL" &
+    ;;
+esac
diff --git a/rtorrent-flood/postrm b/rtorrent-flood/postrm
index 95490f2..67c8721 100644
--- a/rtorrent-flood/postrm
+++ b/rtorrent-flood/postrm
@@ -1,3 +1,11 @@
 #!/bin/sh
 
-/filebot/filebot.sh -script fn:cleaner /data/Media
+(sleep 10; find /data/Media -type l | while read in; do
+    file "$in" | grep "broken symbolic link"
+    if [ $? -ne 1 ]; then
+        echo "Delete $in"
+        rm -f "$in"
+    fi
+done
+
+find /data/Media/ -type d -empty -exec rm -r {} \;) >> /tmp/postrm.log &
diff --git a/rtorrent-flood/rtorrent.rc b/rtorrent-flood/rtorrent.rc
index 01c1e78..4b0c5bb 100644
--- a/rtorrent-flood/rtorrent.rc
+++ b/rtorrent-flood/rtorrent.rc
@@ -6,7 +6,7 @@ port_random = no
 check_hash = no
 directory = /data/torrents
 session = /data/.session
-encryption = require,require_RC4,allow_incoming,try_outgoing
+encryption = allow_incoming, try_outgoing, enable_retry
 use_udp_trackers = yes
 dht = off
 peer_exchange = no
@@ -17,7 +17,7 @@ min_peers_seed = 1
 max_peers_seed = 50
 max_uploads = 15
 
-network.xmlrpc.size_limit.set = 2000000
+network.xmlrpc.size_limit.set = 10000000
 
 schedule = watch_directory,1,1,"load_start=/data/.watch/*.torrent"
 schedule = untied_directory,5,5,"stop_untied=/data/.watch/*.torrent"
diff --git a/rtorrent-flood/run.sh b/rtorrent-flood/run.sh
index 239a27a..c6deb35 100644
--- a/rtorrent-flood/run.sh
+++ b/rtorrent-flood/run.sh
@@ -8,15 +8,22 @@ mkdir -p /data/Media/TV
 mkdir -p /data/Media/Animes
 mkdir -p /data/Media/Music
 
-sed -i -e "s|<FLOOD_SECRET>|$FLOOD_SECRET|g" /usr/flood/config.js
-sed -i -e "s|<CONTEXT_PATH>|$CONTEXT_PATH|g" /usr/flood/config.js
+sed -i -e "s|<FLOOD_SECRET>|$FLOOD_SECRET|g" \
+       -e "s|<CONTEXT_PATH>|$CONTEXT_PATH|g" /usr/flood/config.js
+
+
+sed -i -e 's#<FILEBOT_RENAME_MOVIES>#'"$FILEBOT_RENAME_MOVIES"'#' \
+       -e 's#<FILEBOT_RENAME_METHOD>#'"$FILEBOT_RENAME_METHOD"'#' \
+       -e 's#<FILEBOT_RENAME_MUSICS>#'"$FILEBOT_RENAME_MUSICS"'#' \
+       -e 's#<FILEBOT_RENAME_SERIES>#'"$FILEBOT_RENAME_SERIES"'#' \
+       -e 's#<FILEBOT_RENAME_ANIMES>#'"$FILEBOT_RENAME_ANIMES"'#' /usr/bin/postdl
 
 rm -f /data/.session/rtorrent.lock
-mv /usr/flood /usr/fix && mv /usr/fix /usr/flood # fix strange bug
+
 chown -R $UID:$GID /data /home/torrent /tmp /filebot /usr/flood /flood-db /etc/s6.d
 
 if [ ${RTORRENT_SCGI} -ne 0 ]; then
-    sed -i -e 's|^scgi_local.*$|scgi_port = 0.0.0.0:'${RTORRENT_SCGI}'|' /home/torrent/.rtorrent.rc 
+    sed -i -e 's|^scgi_local.*$|scgi_port = 0.0.0.0:'${RTORRENT_SCGI}'|' /home/torrent/.rtorrent.rc
     sed -i -e 's|socket: true,|socket: false,|' -e 's|port: 5000,|port: '${RTORRENT_SCGI}',|' /usr/flood/config.js
 fi