File size: 1,689 Bytes
d6be317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import sys

def extract_urls_to_file(json_data, output_filename="all_urls.txt"):
    """Extracts URLs from a JSON object and writes them to a text file.

    Args:
        json_data: The JSON object containing the URLs.
        output_filename: The name of the output text file (default: all_urls.txt).
    """
    try:
        with open(output_filename, 'w') as outfile:
            for category, urls in json_data.items():
                for url in urls:
                    outfile.write(url.strip() + '\n')

    except (IOError, json.JSONDecodeError) as e:
        print(f"An error occurred: {e}")



if __name__ == "__main__":
    if len(sys.argv) > 1:
        json_filename = sys.argv[1]
        try:
            with open(json_filename, 'r') as f:
                json_data = json.load(f)
        except FileNotFoundError:
            print(f"Error: File '{json_filename}' not found.")
            sys.exit(1)
        except json.JSONDecodeError:
            print(f"Error: Invalid JSON format in '{json_filename}'.")
            sys.exit(1)


        if len(sys.argv) > 2:
            output_filename = sys.argv[2]
            extract_urls_to_file(json_data, output_filename)
            print(f"URLs extracted to '{output_filename}'")            
        else:
            extract_urls_to_file(json_data)
            print("URLs extracted to 'all_urls.txt'")


    else:
        print("Usage: python script_name.py <json_filename> <output_filename>(optional)")
        default_json = { 
           "ftp": [], "tv": [], "others": [], "all": [] 
        }
        extract_urls_to_file(default_json)
        print("URLs (from default JSON) extracted to 'all_urls.txt'")