From 2fb36b58147b77f59e263be7e2e5ac67c24fd286 Mon Sep 17 00:00:00 2001
From: David Claeys <dcl@centexbel.be>
Date: Wed, 10 Jul 2024 11:30:41 +0200
Subject: [PATCH] docker build

---
 Dockerfile   | 40 ++++++++++++++++++++++++++++++++++++++++
 README.md    | 25 ++++++++++++++++++++++++-
 channels.xml |  5 +++++
 serve.json   |  4 ++++
 start.sh     | 23 +++++++++++++++++++++++
 5 files changed, 96 insertions(+), 1 deletion(-)
 create mode 100644 Dockerfile
 create mode 100644 channels.xml
 create mode 100644 serve.json
 create mode 100644 start.sh

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..62a782f
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,40 @@
+FROM node:21-alpine
+ARG GIT_REPO=https://github.com/iptv-org/epg.git
+ARG GIT_BRANCH=master
+ENV CRON_SCHEDULE="0 0,12 * * *"
+ENV DAYS=14
+ENV MAX_CONNECTIONS=10
+ARG BIN_FOLDER=/bin
+ARG EPG_FOLDER=epg
+ARG START_SCRIPT_ARG=$BIN_FOLDER/$EPG_FOLDER/start.sh
+ENV WORKDIR=${BIN_FOLDER}/${EPG_FOLDER}
+ENV START_SCRIPT=$START_SCRIPT_ARG
+COPY channels.xml /config/channels.xml
+RUN apk update \
+    && apk upgrade --available \
+	  && apk add curl \
+    && apk add git \
+    && apk add tzdata \
+    && apk add bash \
+    && npm install -g npm@latest \
+    && npm install pm2 -g \
+    && mkdir $(echo "${BIN_FOLDER}/${EPG_FOLDER}") -p \
+    && git -C $(echo "${BIN_FOLDER}") clone --depth 1 -b $(echo "${GIT_BRANCH} ${GIT_REPO}") \
+    && cd $WORKDIR cat && npm install && npm update \
+    && rm .eslintrc.json \
+    && rm -rf .github \
+    && rm -rf .git \
+    && rm .gitignore  \
+    && rm CONTRIBUTING.md  \
+    && rm LICENSE \
+    && rm README.md \
+    && rm SITES.md \
+    && ln -s /config/channels.xml $(echo "${WORKDIR}/channels.xml") \
+    && mkdir /public
+COPY start.sh $WORKDIR
+COPY serve.json $WORKDIR
+RUN chmod +x "$START_SCRIPT" \
+  && apk del git curl \
+  && rm -rf /var/cache/apk/*
+ENTRYPOINT bash $START_SCRIPT chron-schedule="$CRON_SCHEDULE" work-dir="$WORKDIR" days="$DAYS" max_connections="$MAX_CONNECTIONS"
+EXPOSE 3000
\ No newline at end of file
diff --git a/README.md b/README.md
index e33c1e1..efbd5af 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,27 @@
 # epg-info-docker
 
-This repo builds and Dokcer image of [iptv-org/epg](https://github.com/iptv-org/epg)
+This repo builds and Docker image of [iptv-org/epg](https://github.com/iptv-org/epg).
+The purpose is to make the deployment easier and more suitable for different environments.
+
+
+## Dependencies
+[Node](https://nodejs.org/en)<br>
+[pm2](https://www.npmjs.com/package/pm2)<br>
+[serve](https://www.npmjs.com/package/serve)<br>
+
+## Docker image
+
+### Paths
+
+An example `channels.xml` is included by default in the image.
+However if you want to configure your own channels you need to provide your own configuration file.
+You can do this by creating a mapping in the `/config` folder.
+
+### Environment Variables
+
+| Variable                      | Description                                                                | Default          |
+|-------------------------------|----------------------------------------------------------------------------|------------------|
+| CRON_SCHEDULE                 | CRON expression describing the recurrence for epg retrieval.               | 0 0,12 * * *     |            
+| DAYS                          | Describes the desired amount of days in the future for for epg retrieval.  | 14               |
+| MAX_CONNECTIONS               | The maximum amount of parallel connections that can be established         | 10               |
 
diff --git a/channels.xml b/channels.xml
new file mode 100644
index 0000000..2f4c8bd
--- /dev/null
+++ b/channels.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<channels>
+    <!--This is a sample of a channel you can add look at https://github.com/iptv-org/epg/tree/master/sites for the complete list-->
+	<channel site="movistarplus.es" lang="es" xmltv_id="24Horas.es" site_id="24H">24 Horas</channel>
+</channels>
\ No newline at end of file
diff --git a/serve.json b/serve.json
new file mode 100644
index 0000000..aeaa882
--- /dev/null
+++ b/serve.json
@@ -0,0 +1,4 @@
+{
+    "symlinks": true,
+    "public": "/public"
+}
\ No newline at end of file
diff --git a/start.sh b/start.sh
new file mode 100644
index 0000000..831aa5d
--- /dev/null
+++ b/start.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+# Loop through arguments
+for arg in "$@"; do
+   case "$arg" in
+      chron-schedule=*) chron_schedule="${arg#*=}" ;;
+      work-dir=*) work_dir="${arg#*=}" ;;
+      days=*) days="${arg#*=}" ;;
+      max_connections=*) max_connections="${arg#*=}" ;;
+   esac
+done
+
+echo "chron_schedule : ${chron_schedule}"
+cd $work_dir
+echo "working dir : " $(pwd)
+echo "days : ${days}"
+echo "max_connections : ${max_connections}"
+
+pm2 --name epg start npm -- run serve
+npm run grab -- --channels=channels.xml --maxConnections=$max_connections --days=$days --gzip
+ln -s $work_dir/guide.xml /public/guide.xml
+ln -s $work_dir/guide.xml.gz /public/guide.xml.gz
+npm run grab -- --channels=channels.xml --cron="$chron_schedule" --maxConnections=$max_connections --days=$days --gzip
\ No newline at end of file