Phoe2004 commited on
Commit
0e29176
Β·
verified Β·
1 Parent(s): dcb492c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -1
app.py CHANGED
@@ -123,6 +123,57 @@ def upload_to_tiktok(video_path, title):
123
  def _do_upload():
124
  """Run in a clean thread (no asyncio loop) so Playwright sync API works"""
125
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  from tiktok_uploader.upload import upload_video
127
  add_log(f'πŸ”§ video size: {os.path.getsize(video_path)} bytes | sessionid len: {len(sessionid)}')
128
  failed = upload_video(
@@ -132,7 +183,7 @@ def upload_to_tiktok(video_path, title):
132
  browser='chromium',
133
  headless=True,
134
  )
135
- result_box[0] = failed # [] = success, [...] = failed list
136
  except Exception as e:
137
  import traceback
138
  add_log(f'❌ Upload error: {e}')
 
123
  def _do_upload():
124
  """Run in a clean thread (no asyncio loop) so Playwright sync API works"""
125
  try:
126
+ import tiktok_uploader.upload as ttu
127
+ from tiktok_uploader import config as ttu_config
128
+ import time as _time
129
+
130
+ # ── Monkey-patch _post_video to fix broken fallback selector ──
131
+ from playwright.sync_api import Page as _Page
132
+ from playwright.sync_api import TimeoutError as _PWTimeout
133
+
134
+ def _patched_post_video(page: _Page) -> None:
135
+ post_btn = page.locator(f"xpath={ttu_config.selectors.upload.post}")
136
+ try:
137
+ # wait up to 4 min for video processing
138
+ for _ in range(120):
139
+ attr = post_btn.get_attribute("data-disabled")
140
+ if attr == "false":
141
+ break
142
+ _time.sleep(2)
143
+ post_btn.scroll_into_view_if_needed()
144
+ post_btn.click()
145
+ add_log('πŸ–±οΈ Post button clicked')
146
+ except Exception as e:
147
+ add_log(f'⚠️ post_btn click failed: {e} β€” trying JS fallback')
148
+ # Updated fallback selectors for 2025 TikTok UI
149
+ for sel in [
150
+ "button[data-e2e='post_video_button']",
151
+ "button.TUXButton--primary",
152
+ "button[type='submit']",
153
+ "div[role='button'][class*='submit']",
154
+ ]:
155
+ try:
156
+ page.evaluate(f"""
157
+ const btn = document.querySelector("{sel}");
158
+ if (btn) btn.click();
159
+ """)
160
+ add_log(f'πŸ–±οΈ JS fallback click: {sel}')
161
+ break
162
+ except Exception:
163
+ continue
164
+ try:
165
+ post_now = page.locator(f"xpath={ttu_config.selectors.upload.post_now}")
166
+ if post_now.is_visible(timeout=5000):
167
+ post_now.click()
168
+ except Exception:
169
+ pass
170
+ confirm = page.locator(f"xpath={ttu_config.selectors.upload.post_confirmation}")
171
+ confirm.wait_for(state="attached", timeout=ttu_config.explicit_wait * 1000)
172
+ add_log('βœ… Post confirmed!')
173
+
174
+ ttu._post_video = _patched_post_video
175
+ # ─────────────────────────────────────────────────────────────
176
+
177
  from tiktok_uploader.upload import upload_video
178
  add_log(f'πŸ”§ video size: {os.path.getsize(video_path)} bytes | sessionid len: {len(sessionid)}')
179
  failed = upload_video(
 
183
  browser='chromium',
184
  headless=True,
185
  )
186
+ result_box[0] = failed
187
  except Exception as e:
188
  import traceback
189
  add_log(f'❌ Upload error: {e}')