diff options
| -rwxr-xr-x | iptv.sh | 289 |
1 files changed, 148 insertions, 141 deletions
@@ -1,6 +1,43 @@ #!/usr/bin/env bash +step () { echo "[-] $1"; } + +fatal () { echo "[!] $1"; exit 1; } + +ensure_curl_is_installed () { + if ! command -v curl &>/dev/null; then + fatal "The cURL package that handles HTTP requests is not installed." + fi +} + +ensure_jq_is_installed () { + if ! command -v jq &>/dev/null; then + fatal "The jq package that handles HTTP JSON response is not installed." + fi +} + +ensure_openssl_is_installed () { + if ! command -v openssl &>/dev/null; then + fatal "The OpenSSL package that handles token computation is not installed." + fi +} + +categorize_by_channel_name () { + result="720P" + + case "$1" in + *4K*) result="4K" ;; + *"超清"*|*"高清"*) result="1080P" ;; + esac + + echo "$result" +} + make_epg () { + # Check dependencies + ensure_curl_is_installed + ensure_jq_is_installed + # Default variables output_file="epg.xml" endpoint="http://120.87.12.38:8083/epg/api/page/biz_59417088.json" @@ -15,12 +52,10 @@ make_epg () { esac done - echo "[-] Request channel index" - + step "Request channel index" response=$(curl --silent $curl_args "$endpoint") - echo "[-] Create the EPG XML file" - + step "Create the EPG XML file" echo '<?xml version="1.0" encoding="UTF-8"?>' > $output_file echo '<!DOCTYPE tv SYSTEM "xmltv.dtd">' >> $output_file echo "<tv date=\"$(date +"%Y%m%d%H%M%S %z")\">" >> $output_file @@ -31,7 +66,7 @@ make_epg () { data_link=$(echo "$item" | jq -r '.dataLink') if [[ "$item_type" == "channel" ]]; then - echo "[-] Process: $item_title" + step "Process: $item_title" response=$(curl --silent --interface eth0 "$data_link") channel_icon=$(echo "$response" | jq -r '.channel.icon') @@ -57,176 +92,148 @@ make_epg () { echo '</tv>' >> $output_file - echo "[-] EPG built sucessfully!" + step "EPG built sucessfully!" exit 0 } -# Default values -ip_address="127.0.0.1" -udpxy_endpoint="http://127.0.0.1:4022" -output_file="iptv.M3U8" -curl_args="" -endpoint="http://eds1.unicomgd.com:8082" - -while [ $# -gt 0 ]; do - case "$1" in - -u | --user) user_id="$2"; shift 2;; - -p | --password) password="$2"; shift 2;; - -d | --device) device_id="$2"; shift 2;; - --ip) ip_address="$2"; shift 2;; - --mac) mac_address="$2"; shift 2;; - --udpxy) udpxy_endpoint="$2"; shift 2;; - -o | --output) output_file="$2"; shift 2;; - --curl) curl_args="$2"; shift 2;; - make:epg) shift 1; make_epg "$@";; - *) echo "Unknown option: $1"; exit 1;; - esac -done +make_playlist () { + # Check dependencies + ensure_curl_is_installed + ensure_jq_is_installed + ensure_openssl_is_installed -## -## Validate user input -## + # Default values + ip_address="127.0.0.1" + udpxy_endpoint="http://127.0.0.1:4022" + output_file="playlist.M3U8" + curl_args="" + endpoint="http://eds1.unicomgd.com:8082" -if [[ -z "$user_id" ]]; then - echo "Requires an IPTV user ID with -u or --user" - exit 1 -fi + while [ $# -gt 0 ]; do + case "$1" in + -u | --user) user_id="$2"; shift 2;; + -p | --password) password="$2"; shift 2;; + -d | --device) device_id="$2"; shift 2;; + --ip) ip_address="$2"; shift 2;; + --mac) mac_address="$2"; shift 2;; + --udpxy) udpxy_endpoint="$2"; shift 2;; + -o | --output) output_file="$2"; shift 2;; + --curl) curl_args="$2"; shift 2;; + *) echo "Unknown option: $1"; exit 1;; + esac + done -if [[ -z "$password" ]]; then - echo "Requires an IPTV password with -p or --password" - exit 1 -fi + # Validate user input + if [[ -z "$user_id" ]]; then + fatal "Requires an IPTV user ID with -u or --user" + fi -if [[ -z "$device_id" ]]; then - echo "Requires a device ID with -d or --device" - exit 1 -fi + if [[ -z "$password" ]]; then + fatal "Requires an IPTV password with -p or --password" + fi -if [[ -z "$mac_address" ]]; then - echo "Requires a device MAC address with --mac" - exit 1 -fi + if [[ -z "$device_id" ]]; then + fatal "Requires a device ID with -d or --device" + fi -## -## Check dependencies -## + if [[ -z "$mac_address" ]]; then + fatal "Requires a device MAC address with --mac" + fi -if ! command -v jq &>/dev/null; then - echo "The jq package that handles HTTP JSON response is not installed." - exit 1 -fi + # Retreive Authentication URL + step "Authenticate" -if ! command -v openssl &>/dev/null; then - echo "The OpenSSL package that handles token computation is not installed." - exit 1 -fi + response=$(curl --silent $curl_args \ + "$endpoint/EDS/jsp/AuthenticationURL?Action=Login&UserID=$user_id&return_type=1") -## -## Execute the main script -## + epgurl=$(echo "$response" | jq -r '.epgurl') + host="${epgurl#*//}" + host="${host%%/*}" -echo "[-] Authenticate" + # Retreive challenge token + response=$(curl --silent $curl_args \ + "http://$host/EPG/oauth/v2/authorize?response_type=EncryToken&client_id=jiulian&userid=$user_id") -## -## Retreive Authentication URL -## + encry_token=$(echo "$response" | jq -r '.EncryToken') -response=$(curl --silent $curl_args \ - "$endpoint/EDS/jsp/AuthenticationURL?Action=Login&UserID=$user_id&return_type=1") + # Authenticate + pass=$(printf "%-024s" "$password" | tr ' ' 0 | xxd -p) -epgurl=$(echo "$response" | jq -r '.epgurl') -host="${epgurl#*//}" -host="${host%%/*}" + authinfo=( + $(shuf -i 1-99999999 -n 1) $encry_token + $user_id $device_id + $ip_address $mac_address + "Reserved" "OTT" + ) -## -## Retreive token -## + authinfo=$(IFS='$'; echo "${authinfo[*]}") -response=$(curl --silent $curl_args \ - "http://$host/EPG/oauth/v2/authorize?response_type=EncryToken&client_id=jiulian&userid=$user_id") + authinfo=$(echo -n $authinfo | \ + openssl enc -e -des-ede3 -K $pass -nosalt 2>/dev/null | \ + xxd -p -u -c 0) -encry_token=$(echo "$response" | jq -r '.EncryToken') + response=$(curl --silent $curl_args \ + "http://$host/EPG/oauth/v2/token?grant_type=EncryToken&client_id=jiulian&UserID=$user_id&DeviceType=UNT400G&DeviceVersion=5.5.021&authinfo=$authinfo&issmarthomestb=1&tvdesktopid=") -## -## Authenticate -## + access_token=$(echo "$response" | jq -r '.access_token') -pass=$(printf "%-024s" "$password" | tr ' ' 0 | xxd -p) + step "Request channel metas" + declare -A logos -authinfo=( - $(shuf -i 1-99999999 -n 1) $encry_token - $user_id $device_id - $ip_address $mac_address - "Reserved" "OTT" -) + response=$(curl --silent $curl_args \ + http://120.87.12.38:8083/epg/api/custom/getAllChannel.json) -authinfo=$(IFS='$'; echo "${authinfo[*]}") + while read -r channel; do + hwcode=$(echo "$channel" | jq -r '.params.hwcode') + icon=$(echo "$channel" | jq -r '.icon') -authinfo=$(echo -n $authinfo | \ - openssl enc -e -des-ede3 -K $pass -nosalt 2>/dev/null | \ - xxd -p -u -c 0) + logos["$hwcode"]="$icon" + done < <(echo "$response" | jq -c '.channels[]') -response=$(curl --silent $curl_args \ - "http://$host/EPG/oauth/v2/token?grant_type=EncryToken&client_id=jiulian&UserID=$user_id&DeviceType=UNT400G&DeviceVersion=5.5.021&authinfo=$authinfo&issmarthomestb=1&tvdesktopid=") + step "Request channels from the batch API" + response=$(curl --silent $curl_args \ + --data '{"channelcodes":""}' \ + --header 'Content-Type: application/json;charset=utf-8' \ + --header "Authorization: $access_token" \ + http://$host/EPG/interEpg/channellist/batch) -access_token=$(echo "$response" | jq -r '.access_token') + step "Decode channels from the HTTP response" + echo "#EXTM3U" > $output_file + echo "#EXT-X-VERSION:3" >> $output_file -echo "[-] Request channel metas" + echo "$response" | jq -c '.channellist[]' | while read -r channel; do + channelcode=$(echo "$channel" | jq -r '.channelcode') + channelname=$(echo "$channel" | jq -r '.channelname') + channelurl=$(echo "$channel" | jq -r '.channelurl') + IFS='|' read -ra channelurls <<< "$channelurl" -declare -A logos + step "Process: $channelname" -response=$(curl --silent $curl_args \ - http://120.87.12.38:8083/epg/api/custom/getAllChannel.json) + pattern="://([^/]+)" + if [[ "${channelurls[0]}" =~ $pattern ]]; then + host=${BASH_REMATCH[1]} -while read -r channel; do - hwcode=$(echo "$channel" | jq -r '.params.hwcode') - icon=$(echo "$channel" | jq -r '.icon') + group=$(categorize_by_channel_name "$channelname") - logos["$hwcode"]="$icon" -done < <(echo "$response" | jq -c '.channels[]') + echo "#EXTINF:-1 tvg-id=\"$channelcode\" tvg-name=\"$channelname\" tvg-logo=\"${logos[$channelcode]}\" group-name=\"$group\",$channelname" >> $output_file + echo "$udpxy_endpoint/udp/$host" >> $output_file + fi + done -echo "[-] Request channels from the batch API" + step "Extract IPTV channels successfully!" -response=$(curl --silent $curl_args \ - --data '{"channelcodes":""}' \ - --header 'Content-Type: application/json;charset=utf-8' \ - --header "Authorization: $access_token" \ - http://$host/EPG/interEpg/channellist/batch) + exit 0 +} -echo "[-] Decode channels from the HTTP response" - -echo "#EXTM3U" > $output_file -echo "#EXT-X-VERSION:3" >> $output_file - -echo "$response" | jq -c '.channellist[]' | while read -r channel; do - channelcode=$(echo "$channel" | jq -r '.channelcode') - channelname=$(echo "$channel" | jq -r '.channelname') - channelurl=$(echo "$channel" | jq -r '.channelurl') - IFS='|' read -ra channelurls <<< "$channelurl" - - echo "[-] Process: $channelname" - - pattern="://([^/]+)" - if [[ "${channelurls[0]}" =~ $pattern ]]; then - host=${BASH_REMATCH[1]} - - group="720P" - case "$channelname" in - *"4K"*) - group="4K" - ;; - *"超清"*|*"高清"*) - group="1080P" - ;; - \?) - group="720P" - ;; - esac +## +## Main +## - echo "#EXTINF:-1 tvg-id=\"$channelcode\" tvg-name=\"$channelname\" tvg-logo=\"${logos[$channelcode]}\" group-name=\"$group\",$channelname" >> $output_file - echo "$udpxy_endpoint/udp/$host" >> $output_file - fi +while [ $# -gt 0 ]; do + case "$1" in + make:playlist) shift 1; make_playlist "$@";; + make:epg) shift 1; make_epg "$@";; + *) echo "Unknown command: $1"; exit 1;; + esac done - -echo "[-] Extract IPTV channels successfully!" |
