summaryrefslogtreecommitdiff
path: root/iptv.sh
blob: e7a23c7b72290dd99def6d5324238875e47d81e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env bash

make_epg () {
  # Default variables
  output_file="epg.xml"
  endpoint="http://120.87.12.38:8083/epg/api/page/biz_59417088.json"
  curl_args=""

  while [ $# -gt 0 ]; do
    case "$1" in
      -o | --output ) output_file="$2"; shift 2;;
      --endpoint ) endpoint="$2"; shift 2;;
      --curl ) curl_args="$2"; shift 2;;
      *) echo "Unknown option: $1"; exit 1;;
    esac
  done

  echo "[-] Request channel index"

  response=$(curl --silent $curl_args "$endpoint")

  echo "[-] 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

  while read -r item; do
    item_title=$(echo "$item" | jq -r '.itemTitle')
    item_type=$(echo "$item" | jq -r '.itemType')
    data_link=$(echo "$item" | jq -r '.dataLink')

    if [[ "$item_type" == "channel" ]]; then
      echo "[-] Process: $item_title"

      response=$(curl --silent --interface eth0 "$data_link")
      channel_icon=$(echo "$response" | jq -r '.channel.icon')
      channel_title=$(echo "$response" | jq -r '.channel.title')
      hwcode=$(echo "$response" | jq -r '.channel.params.hwcode')

      echo "  <channel id=\"$hwcode\">" >> $output_file
      echo "    <display-name lang=\"zh\">$channel_title</display-name>" >> $output_file
      echo "    <icon src=\"$channel_icon\"/>" >> $output_file
      echo "  </channel>" >> $output_file

      while read -r schedule; do
        schedule_title=$(echo "$schedule" | jq -r '.title')
        schedule_starttime=$(echo "$schedule" | jq -r '.starttime')
        schedule_endtime=$(echo "$schedule" | jq -r '.endtime')

        echo "  <programme start=\"$schedule_starttime +0800\" stop=\"$schedule_endtime +0800\" channel=\"$hwcode\">" >> $output_file
        echo "    <title lang=\"zh\">$schedule_title</title>" >> $output_file
        echo "  </programme>" >> $output_file
      done < <(echo "$response" | jq -c '.schedules[]')
    fi
  done < <(echo "$response" | jq -c '.areaDatas[].items[]')

  echo '</tv>' >> $output_file

  echo "[-] 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

##
## Validate user input
##

if [[ -z "$user_id" ]]; then
  echo "Requires an IPTV user ID with -u or --user"
  exit 1
fi

if [[ -z "$password" ]]; then
  echo "Requires an IPTV password with -p or --password"
  exit 1
fi

if [[ -z "$device_id" ]]; then
  echo "Requires a device ID with -d or --device"
  exit 1
fi

if [[ -z "$mac_address" ]]; then
  echo "Requires a device MAC address with --mac"
  exit 1
fi

##
## Check dependencies
##

if ! command -v jq &>/dev/null; then
  echo "The jq package that handles HTTP JSON response is not installed."
  exit 1
fi

if ! command -v openssl &>/dev/null; then
  echo "The OpenSSL package that handles token computation is not installed."
  exit 1
fi

##
## Execute the main script
##

echo "[-] Authenticate"

##
## Retreive Authentication URL
##

response=$(curl --silent $curl_args \
  "$endpoint/EDS/jsp/AuthenticationURL?Action=Login&UserID=$user_id&return_type=1")

epgurl=$(echo "$response" | jq -r '.epgurl')
host="${epgurl#*//}"
host="${host%%/*}"

##
## Retreive token
##

response=$(curl --silent $curl_args \
  "http://$host/EPG/oauth/v2/authorize?response_type=EncryToken&client_id=jiulian&userid=$user_id")

encry_token=$(echo "$response" | jq -r '.EncryToken')

##
## Authenticate
##

pass=$(printf "%-024s" "$password" | tr ' ' 0 | xxd -p)

authinfo=(
    $(shuf -i 1-99999999 -n 1) $encry_token
    $user_id $device_id
    $ip_address $mac_address
    "Reserved" "OTT"
)

authinfo=$(IFS='$'; echo "${authinfo[*]}")

authinfo=$(echo -n $authinfo | \
    openssl enc -e -des-ede3 -K $pass -nosalt 2>/dev/null | \
    xxd -p -u -c 0)

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=")

access_token=$(echo "$response" | jq -r '.access_token')

echo "[-] Request channel metas"

declare -A logos

response=$(curl --silent $curl_args \
  http://120.87.12.38:8083/epg/api/custom/getAllChannel.json)

while read -r channel; do
  hwcode=$(echo "$channel" | jq -r '.params.hwcode')
  icon=$(echo "$channel" | jq -r '.icon')

  logos["$hwcode"]="$icon"
done < <(echo "$response" | jq -c '.channels[]')

echo "[-] 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)

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

    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 "[-] Extract IPTV channels successfully!"