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""" {show_title} {tv_remarks[current_key]} """ 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""" {title} {overview} """ else: nfo_content = f""" {title} {overview} """ 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()