File size: 3,508 Bytes
040a95f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, requests, shutil

API_URL = os.getenv("API_URL")

def sync_media():
    if not API_URL:
        print("Error: API_URL secret variable is missing!")
        return

    try:
        response = requests.get(API_URL)
        data = response.json()
        data.reverse()
        
        base_path = "/data"
        if os.path.exists(base_path):
            shutil.rmtree(base_path)
        os.makedirs(base_path, exist_ok=True)

        # --- STEP 1: TV Show တစ်ခုချင်းစီအတွက် Remark (Main Overview) စုဆောင်းခြင်း ---
        tv_remarks = {}
        for item in data:
            m_cat = str(item.get('main_category', '')).strip()
            s_cat = str(item.get('sub_category', '')).strip()
            remark = item.get('remark', '')
            
            if "TV" in m_cat.upper() and remark and str(remark).strip():
                key = (m_cat, s_cat)
                if key not in tv_remarks:
                    tv_remarks[key] = str(remark).strip()

        # --- STEP 2: ဖိုင်များ စတင်ဆောက်ခြင်း ---
        created_tvshow_nfo = set()

        for item in data:
            main_cat = str(item.get('main_category', 'Others')).strip()
            sub_cat = str(item.get('sub_category', 'General')).strip()
            title = str(item.get('title', 'Untitled')).strip()
            url = str(item.get('url', '')).strip()
            overview = item.get('overview', '')

            if not url: continue

            target_dir = os.path.join(base_path, main_cat, sub_cat)
            os.makedirs(target_dir, exist_ok=True)

            safe_title = title.replace("/", "_")
            strm_file = os.path.join(target_dir, f"{safe_title}.strm")
            with open(strm_file, "w") as f:
                f.write(url)

            is_tv = "TV" in main_cat.upper()
            current_key = (main_cat, sub_cat)

            if is_tv and current_key in tv_remarks:
                if current_key not in created_tvshow_nfo:
                    tv_nfo_file = os.path.join(target_dir, "tvshow.nfo")
                    # Folder နောက်ဆုံးအမည်ကိုသာ Title အဖြစ်ယူခြင်း
                    show_title = sub_cat.split('/')[-1].strip()
                    
                    tv_nfo_content = f"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tvshow>
    <title>{show_title}</title>
    <plot>{tv_remarks[current_key]}</plot>
</tvshow>"""
                    with open(tv_nfo_file, "w", encoding="utf-8") as f:
                        f.write(tv_nfo_content)
                    created_tvshow_nfo.add(current_key)

            if overview and str(overview).strip():
                nfo_file = os.path.join(target_dir, f"{safe_title}.nfo")
                if is_tv:
                    nfo_content = f"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<episodedetails>
    <title>{title}</title>
    <plot>{overview}</plot>
</episodedetails>"""
                else:
                    nfo_content = f"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<movie>
    <title>{title}</title>
    <plot>{overview}</plot>
</movie>"""
                
                with open(nfo_file, "w", encoding="utf-8") as f:
                    f.write(nfo_content)

        print("Successfully synced media files!")
    except Exception as e:
        print(f"Sync Error: {e}")

if __name__ == "__main__":
    sync_media()