IPTV M3U Username Password URL Validator and IPTVNator Importer Process - Python and Node.js 5

IPTV URL Scraping and Validating for IPTVnator: A Node.js and Python Scripting Solution for M3U Playlists scraped from the web

Streamlining IPTV URL Management: A Node.js and Python Scripting Solution

As an IPTV enthusiast, managing a large number of channel URLs can be a daunting task. Copying and pasting URLs from various sources, checking their validity, and importing them into your IPTV app can be a time-consuming process. To simplify this workflow, I’ve developed a set of Node.js and Python scripts that automate these tasks, saving you time and effort.

In this article, I’ll walk you through the four-step process of formatting, validating, and exporting IPTV URLs using these scripts. By the end of this tutorial, you’ll be able to:

  1. Format IPTV URLs from any text source
  2. Validate the URLs to ensure they’re online and working
  3. Export the valid URLs to an IPTVnator backup file for easy import
  4. Download the valid URLs as M3U playlists for local playback

Step 1: Formatting IPTV URLs with 1-txt-to-urls.js

The first script, 1-txt-to-urls.py, takes any text input and formats the IPTV URLs into a standardized format. This is particularly useful when copying URLs from websites or other sources that may have inconsistent formatting.

To use this script, simply copy the text containing the IPTV URLs and paste it into a text file. Then, run the script using Node.js, and it will output a formatted list of URLs.

Extract URLs from text blocks for M3U playlists WITH username and password

1-txt-to-urls-with-Username-Password.py

import re

def extract_m3u_urls(text):
    pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
    urls = re.findall(pattern, text)
    m3u_urls = []
    for url in urls:
        if 'get.php' in url and 'username' in url and 'password' in url and 'type=m3u_plus' in url:
            m3u_urls.append(url)
    return m3u_urls

with open('0-IPTV-URLS.txt', 'r') as input_file:
    text = input_file.read()

m3u_urls = extract_m3u_urls(text)

with open('1-URLS-TO-CHECK.txt', 'w') as output_file:
    for url in m3u_urls:
        output_file.write(url + '\n')

Extract URLs from text blocks for M3U playlists WITHOUT username and password

1-txt-to-urls-NO-Username-Password.py

import re

def extract_m3u_urls(text):
    pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+\.m3u'
    urls = re.findall(pattern, text)
    return urls

with open('0-IPTV-URLS.txt', 'r') as input_file:
    text = input_file.read()

m3u_urls = extract_m3u_urls(text)

with open('1-URLS-TO-CHECK.txt', 'w') as output_file:
    for url in m3u_urls:
        output_file.write(url + '\n')

Step 2: Validating IPTV URLs with 2-check_iptv.js

The second script, 2-check_iptv.js, takes the formatted list of URLs from Step 1 and checks each one to ensure it’s online and working. This script uses a combination of HTTP requests and timeout checks to verify the validity of each URL.

Once the script has finished checking the URLs, it will output a new text file containing only the valid URLs.

2-check_iptv.js

const fs = require('fs');
const path = require('path');
const axios = require('axios');

// Paths for input and output files
const inputFilePath = path.join(__dirname, '1-URLS-TO-CHECK.txt');
const outputFilePath = path.join(__dirname, '2-valid_urls.txt');

// Clear the output file at the start
fs.writeFileSync(outputFilePath, '', 'utf8');

// Function to read URLs from input file and check them
fs.readFile(inputFilePath, 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading the file:', err);
        return;
    }

    const urls = data.split('\n').filter(line => line.trim() !== '');

    urls.forEach(async (url, index) => {
        try {
            const response = await axios.get(url.trim());

            if (response.status === 200) {
                console.log(`URL ${index + 1}: ${url} is reachable.`);

                // Append valid URL to the output file in real-time
                fs.appendFile(outputFilePath, url + '\n', 'utf8', (err) => {
                    if (err) {
                        console.error('Error writing to the output file:', err);
                    }
                });
            } else {
                console.log(`URL ${index + 1}: ${url} returned status code: ${response.status}`);
            }
        } catch (error) {
            console.error(`URL ${index + 1}: ${url} is not reachable. Error: ${error.message}`);
        }
    });
});

Step 3: Exporting to IPTVnator with 3-iptvnator_import.py

The third script, 3-iptvnator_import.py, takes the list of valid URLs from Step 2 and exports them to an IPTVnator backup file. This file can be imported into IPTVnator as if you were restoring a backup, eliminating the need to manually copy and paste each URL.

3-iptvnator_import.py

import json
import uuid
from datetime import datetime
import re
from urllib.parse import urlparse

def read_urls_from_file(file_path):
    try:
        with open(file_path, 'r') as file:
            urls = [line.strip() for line in file.readlines() if line.strip()]
        return urls
    except FileNotFoundError:
        print("The file was not found.")
        return []
    except Exception as e:
        print(f"An error occurred: {e}")
        return []

def extract_credentials(url):
    username = None
    password = None
    
    username_match = re.search(r"username=([^&]+)", url)
    password_match = re.search(r"password=([^&]+)", url)
    
    if username_match:
        username = username_match.group(1)
    if password_match:
        password = password_match.group(1)
    
    return username, password

def extract_server_url(url):
    parsed_url = urlparse(url)
    return f"{parsed_url.scheme}://{parsed_url.netloc}"

def create_playlist_entry(url):
    username, password = extract_credentials(url)
    server_url = extract_server_url(url)

    return {
        "_id": str(uuid.uuid4()),
        "title": url,
        "password": password if password else "default_password",  # Provide a default if not found
        "username": username if username else "default_username",  # Provide a default if not found
        "serverUrl": server_url,
        "importDate": datetime.now().isoformat()
    }

def convert_to_json(urls):
    playlists = [create_playlist_entry(url) for url in urls]
    return json.dumps(playlists, indent=4)

def write_json_to_file(json_data, output_file):
    try:
        with open(output_file, 'w') as file:
            file.write(json_data)
        print(f"JSON data has been written to {output_file}")
    except Exception as e:
        print(f"An error occurred while writing to file: {e}")

def main():
    input_file = '2-valid_urls.txt'
    output_file = '3-iptvnator_import.json'

    urls = read_urls_from_file(input_file)
    if urls:
        json_data = convert_to_json(urls)
        write_json_to_file(json_data, output_file)
    else:
        print("No valid URLs to process.")

if __name__ == "__main__":
    main()

Step 4: Downloading M3U Playlists with 4-download_playlists.js

The final script, 4-download_playlists.js, takes the list of valid URLs from Step 2 and downloads each one as an M3U playlist. These playlists can be opened with VLC or any other media player, allowing you to play all the channels without having to manually download each one.

4-download_playlists.js

const fs = require('fs');
const path = require('path');
const axios = require('axios');

// Path for the input file with valid URLs
const inputFilePath = path.join(__dirname, '2-valid_urls.txt');
const outputDirectory = path.join(__dirname, 'playlists');

// Create the output directory if it doesn't exist
if (!fs.existsSync(outputDirectory)) {
    fs.mkdirSync(outputDirectory);
}

// Function to read URLs and download the playlists
fs.readFile(inputFilePath, 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading the file:', err);
        return;
    }

    const urls = data.split('\n').filter(line => line.trim() !== '');

    urls.forEach(async (url, index) => {
        try {
            const response = await axios.get(url.trim(), { responseType: 'stream' });

            if (response.status === 200) {
                const fileName = `playlist_${index + 1}.m3u`;
                const filePath = path.join(outputDirectory, fileName);

                const writer = fs.createWriteStream(filePath);

                response.data.pipe(writer);

                writer.on('finish', () => {
                    console.log(`Downloaded: ${fileName}`);
                });

                writer.on('error', (err) => {
                    console.error(`Error writing file ${fileName}:`, err);
                });
            } else {
                console.log(`Failed to download from URL ${index + 1}: ${url}. Status code: ${response.status}`);
            }
        } catch (error) {
            console.error(`Error fetching URL ${index + 1}: ${url}. Error: ${error.message}`);
        }
    });
});
IPTV M3U Username Password URL Validator and IPTVNator Importer Process - Python and Node.js 11
IPTV M3U Username Password URL Validator and IPTVNator Importer Process – Python and Node.js 9
IPTV M3U Username Password URL Validator and IPTVNator Importer Process - Python and Node.js
IPTV M3U Username Password URL Validator and IPTVNator Importer Process – Python and Node.js 10

Conclusion

If you want to play with these I have all the files along with the NPM and Node.JS nodes here: https://drive.google.com/file/d/1ajqjlNMwN5B4vAwhV-lauiZSBA3vvxeA/view?usp=drive_link

By automating the process of formatting, validating, and exporting IPTV URLs, these Node.js and Python scripts can save you a significant amount of time and effort. Whether you’re an IPTV enthusiast or a content provider, these scripts can help streamline your workflow and make managing large numbers of channel URLs a breeze.

To get started, simply download the scripts and follow the instructions outlined in this article. With these scripts, you’ll be able to format, validate, and export IPTV URLs in just a few minutes, making it easier than ever to enjoy your favorite channels.

Leave a Reply

Your email address will not be published. Required fields are marked *