-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiptv-xtream-download.py
235 lines (203 loc) · 9.43 KB
/
iptv-xtream-download.py
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
233
234
235
import os
import sys
import argparse
import requests
import time
import json
from pathlib import Path
from xml.dom.minidom import parseString
from datetime import datetime
DEBUG_MODE = False # Default: Debugging is off
def debug_log(message):
"""
Logs a debug message if debugging is enabled.
"""
if DEBUG_MODE:
print(f"[DEBUG] {message}")
def save_data_to_file(url, save_path, headers, retries, sleep_time, debug, format_data):
"""
Downloads data from the given URL and saves it to a file.
"""
attempt = 0
while attempt <= retries:
try:
response = requests.get(url, headers=headers, timeout=30)
response.raise_for_status()
debug_log(f"Response from server ({url}): {response.text[:500]}") # Print first 500 characters
if format_data:
try:
data = response.json()
with open(save_path, "w") as f:
json.dump(data, f, indent=4)
if debug:
print(f"Formatted JSON data successfully saved to {save_path}")
except ValueError:
with open(save_path, "wb") as f:
f.write(response.content)
if debug:
print(f"Non-JSON data saved as is to {save_path}")
else:
with open(save_path, "wb") as f:
f.write(response.content)
if debug:
print(f"Data saved as is to {save_path}")
return True
except requests.RequestException as e:
if debug:
print(f"Attempt {attempt + 1} failed for URL: {url}, Error: {e}")
attempt += 1
if attempt > retries:
print(f"Failed to retrieve data from {url} after {retries} retries.")
else:
time.sleep(sleep_time)
return False
def save_epg_data(url, save_path, headers, retries, sleep_time, debug, format_data):
"""
Fetches EPG data from the given URL, formats it (if required), and saves it as an XML file.
"""
attempt = 0
while attempt <= retries:
try:
response = requests.get(url, headers=headers, timeout=30)
response.raise_for_status()
debug_log(f"Response from server ({url}): {response.text[:500]}") # Print first 500 characters
if debug:
print(f"Retrieving EPG data from {url}...")
if not response.content.strip():
print(f"No EPG data returned from {url}.")
return False
if format_data:
try:
dom = parseString(response.content)
pretty_xml_as_string = dom.toprettyxml(indent=" ")
with open(save_path.with_suffix(".xml"), "w", encoding="utf-8") as f:
f.write(pretty_xml_as_string)
if debug:
print(f"Formatted EPG data successfully saved to {save_path}.xml")
except Exception as e:
if debug:
print(f"Error formatting XML data: {e}")
return False
else:
with open(save_path.with_suffix(".xml"), "wb") as f:
f.write(response.content)
if debug:
print(f"Raw EPG data saved to {save_path}.xml")
return True
except requests.RequestException as e:
if debug:
print(f"Attempt {attempt + 1} failed for EPG URL: {url}, Error: {e}")
attempt += 1
if attempt > retries:
print(f"Failed to retrieve EPG data from {url} after {retries} retries.")
else:
time.sleep(sleep_time)
return False
def ensure_http_prefix(server):
"""
Ensures the server URL starts with http:// or https://.
"""
if not server.startswith(("http://", "https://")):
return f"http://{server}"
return server
def anonymize_user_info(file_path, debug):
"""
Reads the user_info.json file and anonymizes sensitive fields.
"""
try:
with open(file_path, "r") as f:
data = json.load(f)
if "user_info" in data:
data["user_info"]["username"] = "XXXXX"
data["user_info"]["password"] = "YYYYY"
if "server_info" in data:
url = data["server_info"]["url"]
if "." in url:
parts = url.split(".")
parts[0] = "UUUUU"
data["server_info"]["url"] = ".".join(parts)
with open(file_path, "w") as f:
json.dump(data, f, indent=4)
if debug:
print(f"User info successfully anonymized in {file_path}")
except Exception as e:
print(f"Failed to anonymize user info: {e}")
def prune_old_versions(server_dir, prune_count, debug):
"""
Keeps the most recent `prune_count` directories and deletes older ones.
"""
subdirs = [d for d in server_dir.iterdir() if d.is_dir()]
subdirs.sort(key=lambda d: d.stat().st_mtime, reverse=True) # Sort by modification time (most recent first)
if len(subdirs) > prune_count:
print(f"Found {len(subdirs)} subdirectories under {server_dir}. Pruning to : {prune_count}")
to_delete = subdirs[prune_count:]
for old_dir in to_delete:
if debug:
print(f"Deleting old directory: {old_dir}")
for root, dirs, files in os.walk(old_dir, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(old_dir)
else:
print(f"Nothing pruned as only {len(subdirs)} versions available")
def main():
global DEBUG_MODE
parser = argparse.ArgumentParser(description="Program to identify channels from iptv providers")
parser.add_argument("--server", required=True, help="The URL of the Xtream server.")
parser.add_argument("--user", required=True, help="The username for authentication.")
parser.add_argument("--pw", required=True, help="The password for authentication.")
parser.add_argument("--savedir", required=True, help="The directory to save the retrieved files.")
parser.add_argument("--agent", default="Mozilla/5.0", help="User-Agent for accessing the server.")
parser.add_argument("--debug", action="store_true", help="Enable debug mode.") # Debug flag
parser.add_argument("--retries", type=int, default=3, help="Number of retries (default: 3).")
parser.add_argument("--saveraw", action="store_true", help="Keep username/password in user_info.json.")
parser.add_argument("--format", action="store_false", help="Save data in reformatted form.")
parser.add_argument("--prune", type=int, help="Keep only the most recent <prune> versions.")
args = parser.parse_args()
# Print the date and time when the program is run
run_time = datetime.now().strftime("%Y-%m-%d %H:%M")
print(f"\n\niptv-xtream-download - Running for server {args.server} on {run_time}\n")
# Enable debug mode if the --debug flag is present
DEBUG_MODE = args.debug
debug_log("Debug mode enabled")
server_url = ensure_http_prefix(args.server)
save_dir = Path(args.savedir)
if not save_dir.exists():
print(f"Error: '{save_dir}' does not exist. Please create it.")
sys.exit(1)
server_name = args.server.split("://")[-1].replace("/", "_")
server_dir = save_dir / server_name
server_dir.mkdir(parents=True, exist_ok=True)
if args.prune and args.prune > 0:
prune_old_versions(server_dir, args.prune, args.debug)
timestamp_dir = server_dir / datetime.now().strftime("%y-%m-%d--%H-%M")
timestamp_dir.mkdir(parents=True)
headers = {"User-Agent": args.agent}
endpoints = {
"user_info": f"{server_url}/player_api.php?username={args.user}&password={args.pw}",
"live_categories": f"{server_url}/player_api.php?username={args.user}&password={args.pw}&action=get_live_categories",
"live_streams": f"{server_url}/player_api.php?username={args.user}&password={args.pw}&action=get_live_streams",
"vod_categories": f"{server_url}/player_api.php?username={args.user}&password={args.pw}&action=get_vod_categories",
"vod_streams": f"{server_url}/player_api.php?username={args.user}&password={args.pw}&action=get_vod_streams",
"series_categories": f"{server_url}/player_api.php?username={args.user}&password={args.pw}&action=get_series_categories",
"series_streams": f"{server_url}/player_api.php?username={args.user}&password={args.pw}&action=get_series",
}
epg_url = f"{server_url}/xmltv.php?username={args.user}&password={args.pw}"
epg_save_path = timestamp_dir / "epg_data"
for key, url in endpoints.items():
file_path = timestamp_dir / f"{key}.json"
print(f"Retrieving {key}")
save_data_to_file(url, file_path, headers, args.retries, 30, args.debug, args.format)
if key == "user_info" and not args.saveraw:
anonymize_user_info(file_path, args.debug)
print(f"Retrieving EPG data")
save_epg_data(epg_url, epg_save_path, headers, args.retries, 30, args.debug, args.format)
print(f"\nData saved in '{timestamp_dir}'\n\n")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nProgram interrupted. Exiting.")
sys.exit(0)