Phoe2004 commited on
Commit
f8b24e0
Β·
verified Β·
1 Parent(s): 389f338

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -15
app.py CHANGED
@@ -54,6 +54,10 @@ def get_tiktok_cookies():
54
  def get_tiktok_sessionid():
55
  return get_tiktok_cookies().get('sessionid')
56
 
 
 
 
 
57
  def _cookies_to_netscape(cookie_dict, out_path):
58
  """Write Netscape cookies.txt format that tiktok-uploader accepts"""
59
  lines = ['# Netscape HTTP Cookie File']
@@ -62,22 +66,47 @@ def _cookies_to_netscape(cookie_dict, out_path):
62
  Path(out_path).write_text('\n'.join(lines))
63
 
64
  def upload_to_tiktok(video_path, title):
65
- cookies = get_tiktok_cookies()
66
- sessionid = cookies.get('sessionid')
67
- if not sessionid:
68
- add_log('❌ sessionid not found in cookies.txt')
 
69
  return False
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  add_log(f'πŸ“€ Uploading via browser automation: {title[:50]}...')
 
72
  caption = f"{title} #movierecap #myanmar #fyp #viral"
73
-
74
- # Write temp Netscape cookies file
75
- tmp_cookies = str(OUTPUT_DIR / f'_tkcookies_{uuid.uuid4().hex[:6]}.txt')
76
- try:
77
- _cookies_to_netscape(cookies, tmp_cookies)
78
- except Exception as e:
79
- add_log(f'❌ Cookie write error: {e}')
80
- return False
81
 
82
  result_box = [None]
83
 
@@ -102,10 +131,11 @@ def upload_to_tiktok(video_path, title):
102
 
103
  t = threading.Thread(target=_do_upload, daemon=True)
104
  t.start()
105
- t.join(timeout=180) # max 3 min
106
 
107
- try: os.remove(tmp_cookies)
108
- except: pass
 
109
 
110
  if t.is_alive():
111
  add_log('❌ Upload timed out after 3 minutes')
 
54
  def get_tiktok_sessionid():
55
  return get_tiktok_cookies().get('sessionid')
56
 
57
+ def _is_netscape_cookies(content):
58
+ """Check if cookies.txt is already in Netscape format"""
59
+ return '# Netscape' in content or '\tTRUE\t' in content or '\tFALSE\t' in content
60
+
61
  def _cookies_to_netscape(cookie_dict, out_path):
62
  """Write Netscape cookies.txt format that tiktok-uploader accepts"""
63
  lines = ['# Netscape HTTP Cookie File']
 
66
  Path(out_path).write_text('\n'.join(lines))
67
 
68
  def upload_to_tiktok(video_path, title):
69
+ # Read raw cookies.txt
70
+ try:
71
+ raw_content = open(TIKTOK_COOKIE_FILE, 'r').read().strip()
72
+ except Exception as e:
73
+ add_log(f'❌ Cookie read error: {e}')
74
  return False
75
 
76
+ # Auto-detect format
77
+ if _is_netscape_cookies(raw_content):
78
+ # Already Netscape format β€” use directly
79
+ add_log('πŸͺ Netscape format cookies detected β€” using directly')
80
+ tmp_cookies = TIKTOK_COOKIE_FILE
81
+ # Extract sessionid for validation
82
+ sessionid = None
83
+ for line in raw_content.splitlines():
84
+ parts = line.split('\t')
85
+ if len(parts) >= 7 and parts[5] == 'sessionid':
86
+ sessionid = parts[6]
87
+ break
88
+ if not sessionid:
89
+ add_log('❌ sessionid not found in Netscape cookies')
90
+ return False
91
+ else:
92
+ # One-line format β€” convert to Netscape
93
+ add_log('πŸͺ One-line cookies detected β€” converting to Netscape format')
94
+ cookies = parse_cookies_string(raw_content)
95
+ sessionid = cookies.get('sessionid')
96
+ if not sessionid:
97
+ add_log('❌ sessionid not found in cookies.txt')
98
+ return False
99
+ tmp_cookies = str(OUTPUT_DIR / f'_tkcookies_{uuid.uuid4().hex[:6]}.txt')
100
+ try:
101
+ _cookies_to_netscape(cookies, tmp_cookies)
102
+ except Exception as e:
103
+ add_log(f'❌ Cookie write error: {e}')
104
+ return False
105
+
106
  add_log(f'πŸ“€ Uploading via browser automation: {title[:50]}...')
107
+ add_log(f'πŸ”§ sessionid len: {len(sessionid)} | video size: {os.path.getsize(video_path)} bytes')
108
  caption = f"{title} #movierecap #myanmar #fyp #viral"
109
+ is_temp = tmp_cookies != TIKTOK_COOKIE_FILE
 
 
 
 
 
 
 
110
 
111
  result_box = [None]
112
 
 
131
 
132
  t = threading.Thread(target=_do_upload, daemon=True)
133
  t.start()
134
+ t.join(timeout=180)
135
 
136
+ if is_temp:
137
+ try: os.remove(tmp_cookies)
138
+ except: pass
139
 
140
  if t.is_alive():
141
  add_log('❌ Upload timed out after 3 minutes')