simple_magnet2direct.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. from flask import Flask, request, render_template_string, jsonify, redirect, send_file
  2. from seedrcc import Seedr
  3. import time
  4. import os
  5. import threading
  6. import requests
  7. app = Flask(__name__)
  8. # Default credentials (can be overridden by user)
  9. DEFAULT_EMAIL = ""
  10. DEFAULT_PASSWORD = ""
  11. # Download folder
  12. DOWNLOAD_FOLDER = os.path.join(os.path.expanduser("~"), "Downloads", "Magnet2Direct")
  13. # Global variables
  14. download_status = {"progress": 0, "status": "idle", "filename": "", "download_url": "", "file_size": "", "error": ""}
  15. # Create download folder
  16. if not os.path.exists(DOWNLOAD_FOLDER):
  17. os.makedirs(DOWNLOAD_FOLDER)
  18. HTML_TEMPLATE = """
  19. <!DOCTYPE html>
  20. <html lang="en">
  21. <head>
  22. <meta charset="UTF-8">
  23. <link rel="icon" href="/favicon.ico" type="image/x-icon">
  24. <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
  25. <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
  26. <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
  27. <link rel="manifest" href="/site.webmanifest">
  28. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  29. <title>🧲 Magnet2Direct</title>
  30. <style>
  31. * {
  32. margin: 0;
  33. padding: 0;
  34. box-sizing: border-box;
  35. }
  36. body {
  37. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  38. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  39. min-height: 100vh;
  40. padding: 15px;
  41. line-height: 1.6;
  42. }
  43. .container {
  44. max-width: 650px;
  45. margin: 0 auto;
  46. background: white;
  47. border-radius: 20px;
  48. box-shadow: 0 20px 40px rgba(0,0,0,0.15);
  49. padding: 40px;
  50. margin-top: 20px;
  51. position: relative;
  52. }
  53. @media (max-width: 768px) {
  54. .container {
  55. padding: 25px;
  56. margin-top: 60px;
  57. border-radius: 15px;
  58. }
  59. body {
  60. padding: 10px;
  61. }
  62. }
  63. h1 {
  64. text-align: center;
  65. color: #2d3748;
  66. margin-bottom: 15px;
  67. font-size: 2.8em;
  68. font-weight: 700;
  69. }
  70. @media (max-width: 768px) {
  71. h1 {
  72. font-size: 2.2em;
  73. margin-bottom: 10px;
  74. }
  75. }
  76. .subtitle {
  77. text-align: center;
  78. color: #718096;
  79. margin-bottom: 35px;
  80. font-size: 1.1em;
  81. font-weight: 400;
  82. }
  83. @media (max-width: 768px) {
  84. .subtitle {
  85. font-size: 1em;
  86. margin-bottom: 25px;
  87. }
  88. }
  89. .form-group {
  90. margin: 25px 0;
  91. }
  92. label {
  93. display: block;
  94. margin-bottom: 10px;
  95. font-weight: 600;
  96. color: #4a5568;
  97. font-size: 0.95em;
  98. }
  99. input[type="text"], input[type="email"], input[type="password"] {
  100. width: 100%;
  101. padding: 16px 20px;
  102. border: 2px solid #e2e8f0;
  103. border-radius: 12px;
  104. font-size: 16px;
  105. transition: all 0.3s ease;
  106. background: #f8fafc;
  107. color: #2d3748;
  108. }
  109. input[type="text"]:focus, input[type="email"]:focus, input[type="password"]:focus {
  110. outline: none;
  111. border-color: #667eea;
  112. background: white;
  113. box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  114. transform: translateY(-1px);
  115. }
  116. input::placeholder {
  117. color: #a0aec0;
  118. font-style: italic;
  119. }
  120. .btn {
  121. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  122. color: white;
  123. border: none;
  124. padding: 16px 32px;
  125. border-radius: 12px;
  126. font-size: 16px;
  127. font-weight: 600;
  128. cursor: pointer;
  129. transition: all 0.3s ease;
  130. text-decoration: none;
  131. display: inline-block;
  132. margin: 8px;
  133. min-width: 140px;
  134. text-align: center;
  135. box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  136. }
  137. .btn:hover {
  138. transform: translateY(-2px);
  139. box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
  140. }
  141. .btn:active {
  142. transform: translateY(0);
  143. }
  144. .btn:disabled {
  145. opacity: 0.6;
  146. cursor: not-allowed;
  147. transform: none;
  148. box-shadow: 0 4px 15px rgba(102, 126, 234, 0.2);
  149. }
  150. .btn-copy {
  151. background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
  152. box-shadow: 0 4px 15px rgba(72, 187, 120, 0.3);
  153. }
  154. .btn-copy:hover {
  155. box-shadow: 0 8px 25px rgba(72, 187, 120, 0.4);
  156. }
  157. .btn-download {
  158. background: linear-gradient(135deg, #4299e1 0%, #3182ce 100%);
  159. box-shadow: 0 4px 15px rgba(66, 153, 225, 0.3);
  160. }
  161. .btn-download:hover {
  162. box-shadow: 0 8px 25px rgba(66, 153, 225, 0.4);
  163. }
  164. .btn-danger {
  165. background: linear-gradient(135deg, #f56565 0%, #e53e3e 100%);
  166. box-shadow: 0 4px 15px rgba(245, 101, 101, 0.3);
  167. }
  168. .btn-danger:hover {
  169. box-shadow: 0 8px 25px rgba(245, 101, 101, 0.4);
  170. }
  171. @media (max-width: 768px) {
  172. .btn {
  173. width: 100%;
  174. margin: 5px 0;
  175. padding: 14px 24px;
  176. font-size: 15px;
  177. }
  178. }
  179. .progress-container {
  180. margin: 30px 0;
  181. padding: 25px;
  182. background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%);
  183. border-radius: 15px;
  184. border: 1px solid #e2e8f0;
  185. }
  186. .account-info {
  187. margin: 30px 0;
  188. padding: 25px;
  189. background: linear-gradient(135deg, #f0fff4 0%, #e6fffa 100%);
  190. border-radius: 15px;
  191. border: 1px solid #9ae6b4;
  192. font-size: 14px;
  193. }
  194. .account-info h4 {
  195. margin: 0 0 15px 0;
  196. color: #22543d;
  197. font-size: 1.1em;
  198. }
  199. .storage-bar {
  200. width: 100%;
  201. height: 24px;
  202. background: #e2e8f0;
  203. border-radius: 12px;
  204. overflow: hidden;
  205. margin: 12px 0;
  206. }
  207. .storage-fill {
  208. height: 100%;
  209. background: linear-gradient(90deg, #48bb78, #ed8936, #f56565);
  210. transition: width 0.5s ease;
  211. }
  212. .progress-bar {
  213. width: 100%;
  214. height: 24px;
  215. background: #e2e8f0;
  216. border-radius: 12px;
  217. overflow: hidden;
  218. margin: 15px 0;
  219. }
  220. .progress-fill {
  221. height: 100%;
  222. background: linear-gradient(90deg, #667eea, #48bb78);
  223. transition: width 0.5s ease;
  224. animation: shimmer 2s infinite;
  225. }
  226. .status-filename {
  227. word-break: break-word;
  228. overflow-wrap: anywhere;
  229. font-size: 0.9em;
  230. color: #4a5568;
  231. margin-top: 10px;
  232. padding: 8px 12px;
  233. background: rgba(255,255,255,0.8);
  234. border-radius: 6px;
  235. font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
  236. }
  237. @keyframes shimmer {
  238. 0% { opacity: 1; }
  239. 50% { opacity: 0.8; }
  240. 100% { opacity: 1; }
  241. }
  242. .result {
  243. margin: 30px 0;
  244. padding: 25px;
  245. background: linear-gradient(135deg, #f0fff4 0%, #e6fffa 100%);
  246. border-radius: 15px;
  247. border: 1px solid #9ae6b4;
  248. }
  249. .result.error {
  250. background: linear-gradient(135deg, #fed7d7 0%, #fbb6ce 100%);
  251. border: 1px solid #fc8181;
  252. color: #742a2a;
  253. }
  254. .file-info {
  255. background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%);
  256. padding: 20px;
  257. border-radius: 12px;
  258. margin: 15px 0;
  259. border: 1px solid #e2e8f0;
  260. word-break: break-word;
  261. overflow-wrap: break-word;
  262. }
  263. .file-info p {
  264. margin: 8px 0;
  265. line-height: 1.5;
  266. }
  267. .filename-text {
  268. word-break: break-all;
  269. overflow-wrap: anywhere;
  270. max-width: 100%;
  271. font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
  272. font-size: 13px;
  273. background: rgba(0,0,0,0.05);
  274. padding: 8px 10px;
  275. border-radius: 6px;
  276. margin-top: 5px;
  277. }
  278. .url-box {
  279. background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%);
  280. padding: 15px;
  281. border-radius: 10px;
  282. font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
  283. word-break: break-all;
  284. overflow-wrap: anywhere;
  285. border: 1px solid #e2e8f0;
  286. margin: 15px 0;
  287. font-size: 13px;
  288. color: #4a5568;
  289. max-width: 100%;
  290. overflow-x: auto;
  291. }
  292. .settings-btn {
  293. position: absolute;
  294. top: 15px;
  295. left: 15px;
  296. background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
  297. color: white;
  298. border: none;
  299. border-radius: 50%;
  300. width: 50px;
  301. height: 50px;
  302. font-size: 20px;
  303. cursor: pointer;
  304. box-shadow: 0 6px 15px rgba(72, 187, 120, 0.4);
  305. transition: all 0.3s ease;
  306. z-index: 100;
  307. }
  308. .settings-btn:hover {
  309. background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
  310. transform: scale(1.1);
  311. box-shadow: 0 12px 30px rgba(72, 187, 120, 0.5);
  312. }
  313. @media (max-width: 768px) {
  314. .settings-btn {
  315. width: 45px;
  316. height: 45px;
  317. font-size: 18px;
  318. top: 12px;
  319. left: 12px;
  320. }
  321. }
  322. .modal {
  323. display: none;
  324. position: fixed;
  325. z-index: 2000;
  326. left: 0;
  327. top: 0;
  328. width: 100%;
  329. height: 100%;
  330. background-color: rgba(0,0,0,0.6);
  331. backdrop-filter: blur(5px);
  332. overflow-y: auto;
  333. align-items: center;
  334. justify-content: center;
  335. padding: 20px;
  336. box-sizing: border-box;
  337. }
  338. .modal.show {
  339. display: flex;
  340. }
  341. .modal-content {
  342. background: white;
  343. padding: 40px;
  344. border-radius: 20px;
  345. width: 100%;
  346. max-width: 550px;
  347. max-height: 90vh;
  348. overflow-y: auto;
  349. box-shadow: 0 25px 50px rgba(0,0,0,0.2);
  350. position: relative;
  351. animation: modalSlideIn 0.3s ease;
  352. margin: auto;
  353. }
  354. @keyframes modalSlideIn {
  355. from {
  356. opacity: 0;
  357. transform: translateY(-30px);
  358. }
  359. to {
  360. opacity: 1;
  361. transform: translateY(0);
  362. }
  363. }
  364. @media (max-width: 768px) {
  365. .modal {
  366. padding: 15px;
  367. }
  368. .modal-content {
  369. padding: 25px;
  370. border-radius: 15px;
  371. max-height: 85vh;
  372. }
  373. }
  374. .close {
  375. color: #a0aec0;
  376. float: right;
  377. font-size: 32px;
  378. font-weight: bold;
  379. cursor: pointer;
  380. line-height: 1;
  381. transition: color 0.3s ease;
  382. }
  383. .close:hover {
  384. color: #4a5568;
  385. }
  386. .credentials-status {
  387. padding: 16px 20px;
  388. border-radius: 12px;
  389. margin: 20px 0;
  390. font-weight: 600;
  391. font-size: 0.95em;
  392. }
  393. .credentials-status.saved {
  394. background: linear-gradient(135deg, #c6f6d5 0%, #9ae6b4 100%);
  395. color: #22543d;
  396. border: 1px solid #9ae6b4;
  397. }
  398. .credentials-status.not-saved {
  399. background: linear-gradient(135deg, #fed7d7 0%, #fbb6ce 100%);
  400. color: #742a2a;
  401. border: 1px solid #fc8181;
  402. }
  403. .privacy-note {
  404. margin-top: 25px;
  405. padding: 20px;
  406. background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%);
  407. border-radius: 12px;
  408. font-size: 14px;
  409. color: #4a5568;
  410. border: 1px solid #e2e8f0;
  411. }
  412. .button-group {
  413. display: flex;
  414. gap: 10px;
  415. justify-content: center;
  416. flex-wrap: wrap;
  417. margin-top: 25px;
  418. }
  419. @media (max-width: 768px) {
  420. .button-group {
  421. flex-direction: column;
  422. gap: 8px;
  423. }
  424. }
  425. /* Loading states */
  426. .loading {
  427. opacity: 0.7;
  428. pointer-events: none;
  429. }
  430. /* Improved responsive typography */
  431. @media (max-width: 480px) {
  432. html {
  433. font-size: 14px;
  434. }
  435. h1 {
  436. font-size: 2em;
  437. }
  438. .subtitle {
  439. font-size: 0.95em;
  440. }
  441. input[type="text"], input[type="email"], input[type="password"] {
  442. padding: 14px 16px;
  443. font-size: 16px; /* Prevents zoom on iOS */
  444. }
  445. .filename-text {
  446. font-size: 11px;
  447. padding: 6px 8px;
  448. line-height: 1.4;
  449. }
  450. .url-box {
  451. font-size: 11px;
  452. padding: 12px;
  453. line-height: 1.4;
  454. }
  455. .status-filename {
  456. font-size: 0.8em;
  457. padding: 6px 10px;
  458. }
  459. .file-info {
  460. padding: 15px;
  461. }
  462. }
  463. </style>
  464. <script>
  465. function checkStatus() {
  466. fetch('/status')
  467. .then(response => response.json())
  468. .then(data => {
  469. const statusEl = document.getElementById('status');
  470. const resultEl = document.getElementById('result');
  471. const submitBtn = document.getElementById('submitBtn');
  472. if (data.status === 'processing') {
  473. statusEl.style.display = 'block';
  474. statusEl.innerHTML = `
  475. <div><strong>Status:</strong> ${data.status}</div>
  476. <div>Progress: ${data.progress}%</div>
  477. <div class="progress-bar">
  478. <div class="progress-fill" style="width: ${data.progress}%"></div>
  479. </div>
  480. ${data.filename ? `<div class="status-filename">${data.filename}</div>` : ''}
  481. `;
  482. submitBtn.disabled = true;
  483. setTimeout(checkStatus, 2000);
  484. } else if (data.status === 'completed') {
  485. statusEl.style.display = 'none';
  486. resultEl.innerHTML = `
  487. <h3>✅ Video Found!</h3>
  488. <div class="file-info">
  489. <p><strong>Filename:</strong></p>
  490. <div class="filename-text">${data.filename}</div>
  491. <p style="margin-top: 15px;"><strong>Size:</strong> ${data.file_size}</p>
  492. </div>
  493. <div class="url-box" id="downloadUrl">${data.download_url}</div>
  494. <div class="button-group" style="margin-top: 20px;">
  495. <button class="btn btn-copy" onclick="copyUrl()">📋 Copy Download URL</button>
  496. <a href="/download-file" target="_blank" class="btn btn-download">📥 Download File</a>
  497. </div>
  498. `;
  499. resultEl.className = 'result';
  500. resultEl.style.display = 'block';
  501. submitBtn.disabled = false;
  502. // Auto-scroll to results section
  503. setTimeout(() => {
  504. resultEl.scrollIntoView({
  505. behavior: 'smooth',
  506. block: 'center'
  507. });
  508. }, 100);
  509. } else if (data.status === 'error') {
  510. statusEl.style.display = 'none';
  511. resultEl.innerHTML = `<h3>❌ Error</h3><p>${data.error}</p>`;
  512. resultEl.className = 'result error';
  513. resultEl.style.display = 'block';
  514. submitBtn.disabled = false;
  515. // Auto-scroll to error message
  516. setTimeout(() => {
  517. resultEl.scrollIntoView({
  518. behavior: 'smooth',
  519. block: 'center'
  520. });
  521. }, 100);
  522. } else {
  523. submitBtn.disabled = false;
  524. }
  525. });
  526. }
  527. function addMagnet() {
  528. const magnet = document.getElementById('magnet').value.trim();
  529. if (!magnet) {
  530. alert('Please enter a magnet link');
  531. return;
  532. }
  533. const credentials = getSavedCredentials();
  534. if (!credentials.email || !credentials.password) {
  535. alert('Please set up your Seedr credentials first (click the ⚙️ button)');
  536. openSettings();
  537. return;
  538. }
  539. const formData = new FormData();
  540. formData.append('magnet', magnet);
  541. formData.append('email', credentials.email);
  542. formData.append('password', credentials.password);
  543. fetch('/add-magnet', {
  544. method: 'POST',
  545. body: formData
  546. })
  547. .then(response => response.json())
  548. .then(data => {
  549. if (data.status === 'started') {
  550. checkStatus();
  551. } else if (data.error) {
  552. alert('Error: ' + data.error);
  553. }
  554. });
  555. }
  556. function copyUrl() {
  557. const urlText = document.getElementById('downloadUrl').textContent;
  558. navigator.clipboard.writeText(urlText).then(() => {
  559. alert('Download URL copied to clipboard!');
  560. });
  561. }
  562. // Auto-check status on page load
  563. window.onload = function() {
  564. checkStatus();
  565. loadAccountInfo();
  566. loadSavedCredentials();
  567. };
  568. // Credential management functions
  569. function loadSavedCredentials() {
  570. const email = localStorage.getItem('seedr_email');
  571. const password = localStorage.getItem('seedr_password');
  572. const statusEl = document.getElementById('credentialsStatus');
  573. if (email && password) {
  574. statusEl.className = 'credentials-status saved';
  575. statusEl.innerHTML = '✅ Credentials saved for: ' + email;
  576. document.getElementById('seedrEmail').value = email;
  577. document.getElementById('seedrPassword').value = password;
  578. } else {
  579. statusEl.className = 'credentials-status not-saved';
  580. statusEl.innerHTML = '❌ No credentials saved - Please add your Seedr account';
  581. }
  582. }
  583. function saveCredentials() {
  584. const email = document.getElementById('seedrEmail').value.trim();
  585. const password = document.getElementById('seedrPassword').value.trim();
  586. if (!email || !password) {
  587. alert('Please enter both email and password');
  588. return;
  589. }
  590. // Test credentials first
  591. testCredentials(email, password).then(valid => {
  592. if (valid) {
  593. localStorage.setItem('seedr_email', email);
  594. localStorage.setItem('seedr_password', password);
  595. loadSavedCredentials();
  596. alert('✅ Credentials saved successfully!');
  597. closeSettings();
  598. loadAccountInfo(); // Refresh account info
  599. } else {
  600. alert('❌ Invalid credentials. Please check your email and password.');
  601. }
  602. });
  603. }
  604. function clearCredentials() {
  605. if (confirm('Are you sure you want to clear saved credentials?')) {
  606. localStorage.removeItem('seedr_email');
  607. localStorage.removeItem('seedr_password');
  608. document.getElementById('seedrEmail').value = '';
  609. document.getElementById('seedrPassword').value = '';
  610. loadSavedCredentials();
  611. alert('🗑️ Credentials cleared');
  612. }
  613. }
  614. function testCredentials(email, password) {
  615. return fetch('/test-credentials', {
  616. method: 'POST',
  617. headers: {
  618. 'Content-Type': 'application/json',
  619. },
  620. body: JSON.stringify({
  621. email: email,
  622. password: password
  623. })
  624. })
  625. .then(response => response.json())
  626. .then(data => data.valid)
  627. .catch(() => false);
  628. }
  629. function openSettings() {
  630. const modal = document.getElementById('settingsModal');
  631. modal.style.display = 'flex';
  632. modal.classList.add('show');
  633. }
  634. function closeSettings() {
  635. const modal = document.getElementById('settingsModal');
  636. modal.style.display = 'none';
  637. modal.classList.remove('show');
  638. }
  639. // Close modal when clicking outside
  640. window.onclick = function(event) {
  641. const modal = document.getElementById('settingsModal');
  642. if (event.target == modal) {
  643. closeSettings();
  644. }
  645. }
  646. // Modified functions to use saved credentials
  647. function getSavedCredentials() {
  648. return {
  649. email: localStorage.getItem('seedr_email') || '',
  650. password: localStorage.getItem('seedr_password') || ''
  651. };
  652. }
  653. function loadAccountInfo() {
  654. const credentials = getSavedCredentials();
  655. if (!credentials.email || !credentials.password) {
  656. const accountInfoEl = document.getElementById('account-info');
  657. const accountDetailsEl = document.getElementById('account-details');
  658. accountDetailsEl.innerHTML = `
  659. <div style="color: #dc3545;">
  660. <strong>⚠️ No Seedr credentials set</strong><br>
  661. <small>Click the ⚙️ button to add your account</small>
  662. </div>
  663. `;
  664. accountInfoEl.style.display = 'block';
  665. return;
  666. }
  667. fetch('/account-info', {
  668. method: 'POST',
  669. headers: {
  670. 'Content-Type': 'application/json',
  671. },
  672. body: JSON.stringify(credentials)
  673. })
  674. .then(response => response.json())
  675. .then(data => {
  676. const accountInfoEl = document.getElementById('account-info');
  677. const accountDetailsEl = document.getElementById('account-details');
  678. if (data.max_file_size !== 'Connection Error') {
  679. accountDetailsEl.innerHTML = `
  680. <div style="background: #fff3cd; padding: 8px; border-radius: 4px; border: 1px solid #ffeaa7;">
  681. <strong>⚠️ Max file size you can download:</strong> <span style="color: #856404; font-weight: bold;">${data.max_file_size}</span>
  682. </div>
  683. `;
  684. accountInfoEl.style.display = 'block';
  685. } else {
  686. accountDetailsEl.innerHTML = `
  687. <div style="color: #dc3545;">
  688. <strong>⚠️ Could not load account info</strong><br>
  689. <small>Check your Seedr credentials</small>
  690. </div>
  691. `;
  692. accountInfoEl.style.display = 'block';
  693. }
  694. })
  695. .catch(error => {
  696. console.error('Error loading account info:', error);
  697. const accountInfoEl = document.getElementById('account-info');
  698. const accountDetailsEl = document.getElementById('account-details');
  699. accountDetailsEl.innerHTML = `
  700. <div style="color: #dc3545;">
  701. <strong>⚠️ Connection error</strong><br>
  702. <small>Could not load account information</small>
  703. </div>
  704. `;
  705. accountInfoEl.style.display = 'block';
  706. });
  707. }
  708. </script>
  709. </head>
  710. <body>
  711. <!-- Settings Modal -->
  712. <div id="settingsModal" class="modal">
  713. <div class="modal-content">
  714. <span class="close" onclick="closeSettings()">&times;</span>
  715. <h2>🔐 Seedr Account Settings</h2>
  716. <p style="color: #718096; margin-bottom: 25px; font-size: 1.05em;">
  717. Connect your Seedr account to start downloading. Your credentials are stored securely in your browser only.
  718. </p>
  719. <div id="credentialsStatus" class="credentials-status not-saved">
  720. ❌ No credentials saved - Please add your Seedr account
  721. </div>
  722. <div class="form-group">
  723. <label for="seedrEmail">📧 Seedr Email Address:</label>
  724. <input type="email" id="seedrEmail"
  725. placeholder="Enter your Seedr email (e.g., [email protected])"
  726. autocomplete="email" required>
  727. </div>
  728. <div class="form-group">
  729. <label for="seedrPassword">🔒 Seedr Password:</label>
  730. <input type="password" id="seedrPassword"
  731. placeholder="Enter your Seedr account password"
  732. autocomplete="current-password" required>
  733. </div>
  734. <div class="button-group">
  735. <button type="button" class="btn" onclick="saveCredentials()">
  736. 💾 Save & Test Connection
  737. </button>
  738. <button type="button" class="btn btn-danger" onclick="clearCredentials()">
  739. 🗑️ Clear Saved Data
  740. </button>
  741. </div>
  742. <div class="privacy-note">
  743. <strong>🔒 Privacy & Security:</strong><br>
  744. • Your credentials are stored only in your browser's local storage<br>
  745. • No data is sent to our servers - only directly to Seedr<br>
  746. • You can clear your data anytime using the button above<br>
  747. • Your credentials are tested before saving to ensure they work
  748. </div>
  749. </div>
  750. </div>
  751. <div class="container">
  752. <!-- Settings Button -->
  753. <button class="settings-btn" onclick="openSettings()" title="Seedr Account Settings">⚙️</button>
  754. <h1>🧲 Magnet2Direct</h1>
  755. <p class="subtitle">
  756. No more Seedr login marathons - Just paste, wait & enjoy! 🍿
  757. </p>
  758. <div id="account-info" class="account-info" style="display: none;">
  759. <h4>📊 Your Seedr Account</h4>
  760. <div id="account-details">Loading account info...</div>
  761. </div>
  762. <div class="form-group">
  763. <label for="magnet">🔗 Magnet Link:</label>
  764. <input type="text" id="magnet" name="magnet"
  765. placeholder="Paste your magnet link here... (e.g., magnet:?xt=urn:btih:...)"
  766. required>
  767. </div>
  768. <div style="text-align: center;">
  769. <button type="button" class="btn" id="submitBtn" onclick="addMagnet()">
  770. 🚀 Add Magnet & Download
  771. </button>
  772. </div>
  773. <div id="status" class="progress-container" style="display: none;"></div>
  774. <div id="result" class="result" style="display: none;"></div>
  775. </div>
  776. </body>
  777. </html>
  778. """
  779. def get_account_info(client):
  780. """Get Seedr account information including storage limits."""
  781. try:
  782. # Get account settings/info
  783. settings = client.get_settings()
  784. if settings and hasattr(settings, 'account'):
  785. account = settings.account
  786. space_max = getattr(account, 'space_max', 0)
  787. if space_max > 0:
  788. # Since we clear Seedr each time, max file size = total space
  789. return {
  790. 'max_file_size': format_file_size(space_max)
  791. }
  792. # Fallback for free accounts
  793. return {
  794. 'max_file_size': '2.0 GB (Free Account Limit)'
  795. }
  796. except Exception as e:
  797. print(f"Error getting account info: {e}")
  798. # Return default values for free account
  799. return {
  800. 'max_file_size': '2.0 GB (Free Account Limit)'
  801. }
  802. def connect_to_seedr(email, password, retries=3):
  803. """Connect to Seedr with retry logic."""
  804. for attempt in range(retries):
  805. try:
  806. print(f"Connecting to Seedr (attempt {attempt + 1}/{retries})...")
  807. client = Seedr.from_password(email, password)
  808. print("Connected to Seedr successfully")
  809. return client
  810. except Exception as e:
  811. print(f"Connection attempt {attempt + 1} failed: {e}")
  812. if attempt < retries - 1:
  813. time.sleep(2) # Wait before retry
  814. else:
  815. raise Exception(f"Could not connect to Seedr after {retries} attempts: {e}")
  816. def clear_seedr_account(client):
  817. """Delete all files and folders from Seedr account."""
  818. try:
  819. contents = client.list_contents()
  820. deleted_count = 0
  821. # Delete all files
  822. for file in contents.files:
  823. try:
  824. client.delete_file(file.folder_file_id)
  825. print(f"Deleted file: {file.name}")
  826. deleted_count += 1
  827. except Exception as e:
  828. print(f"Error deleting file {file.name}: {e}")
  829. # Delete all folders
  830. for folder in contents.folders:
  831. try:
  832. client.delete_folder(folder.id)
  833. print(f"Deleted folder: {folder.name}")
  834. deleted_count += 1
  835. except Exception as e:
  836. print(f"Error deleting folder {folder.name}: {e}")
  837. print(f"Cleared Seedr account: {deleted_count} items deleted")
  838. return True
  839. except Exception as e:
  840. print(f"Error clearing Seedr account: {e}")
  841. return False
  842. def process_magnet(magnet_link, email, password):
  843. """Process magnet link and find video."""
  844. global download_status
  845. try:
  846. download_status = {"progress": 10, "status": "processing", "filename": "", "download_url": "", "file_size": "", "error": ""}
  847. client = connect_to_seedr(email, password)
  848. with client:
  849. print("Connected to Seedr successfully")
  850. # First clear the Seedr account
  851. download_status["progress"] = 15
  852. download_status["filename"] = "Clearing Seedr account..."
  853. clear_success = clear_seedr_account(client)
  854. if not clear_success:
  855. print("Warning: Could not fully clear Seedr account, continuing anyway...")
  856. # Add the new magnet
  857. download_status["progress"] = 30
  858. download_status["filename"] = "Adding magnet to Seedr..."
  859. try:
  860. result = client.add_torrent(magnet_link)
  861. print(f"Magnet added successfully: {type(result)}")
  862. except Exception as e:
  863. raise Exception(f"Failed to add magnet: {e}")
  864. # Wait for files to appear
  865. download_status["progress"] = 40
  866. download_status["filename"] = "Waiting for download..."
  867. max_wait = 300 # 5 minutes
  868. wait_time = 0
  869. while wait_time < max_wait:
  870. try:
  871. video_file = find_biggest_video(client)
  872. if video_file:
  873. print(f"Found video file: {video_file.name}")
  874. break
  875. except Exception as e:
  876. print(f"Error checking for files: {e}")
  877. progress = 40 + int((wait_time / max_wait) * 50) # 40-90%
  878. download_status["progress"] = progress
  879. download_status["filename"] = f"Downloading... ({wait_time}s)"
  880. time.sleep(10)
  881. wait_time += 10
  882. if not video_file:
  883. raise Exception("No video file found after 5 minutes. The torrent might be slow or invalid.")
  884. # Get download URL
  885. download_status["progress"] = 95
  886. download_status["filename"] = video_file.name
  887. try:
  888. fetch_result = client.fetch_file(video_file.folder_file_id)
  889. download_url = fetch_result.url if hasattr(fetch_result, 'url') else str(fetch_result)
  890. except Exception as e:
  891. raise Exception(f"Could not get download URL: {e}")
  892. # Success
  893. download_status = {
  894. "progress": 100,
  895. "status": "completed",
  896. "filename": video_file.name,
  897. "download_url": download_url,
  898. "file_size": format_file_size(video_file.size),
  899. "error": ""
  900. }
  901. print(f"Success! Video: {video_file.name} ({download_status['file_size']})")
  902. except Exception as e:
  903. print(f"Process error: {e}")
  904. import traceback
  905. traceback.print_exc()
  906. download_status = {
  907. "progress": 0,
  908. "status": "error",
  909. "filename": "",
  910. "download_url": "",
  911. "file_size": "",
  912. "error": f"Error: {str(e)}. Try again with a different magnet link."
  913. }
  914. def find_biggest_video(client, folder_id=None):
  915. """Find the biggest video file."""
  916. biggest_video = None
  917. biggest_size = 0
  918. try:
  919. contents = client.list_contents(folder_id)
  920. folder_name = "root" if folder_id is None else f"folder_{folder_id}"
  921. print(f"Searching {folder_name}: {len(contents.files)} files, {len(contents.folders)} folders")
  922. # Check files in current folder
  923. for file in contents.files:
  924. print(f"Found file: {file.name} ({format_file_size(file.size)})")
  925. if file.name.lower().endswith(('.mp4', '.mkv', '.avi', '.mov', '.wmv', '.flv', '.m4v', '.webm', '.mpg', '.mpeg')):
  926. print(f"Video file: {file.name}")
  927. if file.size > biggest_size:
  928. biggest_video = file
  929. biggest_size = file.size
  930. print(f"New biggest video: {file.name} ({format_file_size(file.size)})")
  931. # Check subfolders
  932. for subfolder in contents.folders:
  933. print(f"Checking subfolder: {subfolder.name}")
  934. result = find_biggest_video(client, subfolder.id)
  935. if result and result.size > biggest_size:
  936. biggest_video = result
  937. biggest_size = result.size
  938. print(f"New biggest from subfolder: {result.name} ({format_file_size(result.size)})")
  939. except Exception as e:
  940. print(f"Error searching folder {folder_id}: {e}")
  941. import traceback
  942. traceback.print_exc()
  943. if biggest_video:
  944. print(f"Returning biggest video: {biggest_video.name} ({format_file_size(biggest_video.size)})")
  945. else:
  946. print("No video files found in this search")
  947. return biggest_video
  948. def format_file_size(size_bytes):
  949. """Convert bytes to human readable format."""
  950. if size_bytes == 0:
  951. return "0 B"
  952. for unit in ['B', 'KB', 'MB', 'GB']:
  953. if size_bytes < 1024.0:
  954. return f"{size_bytes:.1f} {unit}"
  955. size_bytes /= 1024.0
  956. @app.route('/')
  957. def index():
  958. return render_template_string(HTML_TEMPLATE)
  959. @app.route('/add-magnet', methods=['POST'])
  960. def add_magnet():
  961. magnet = request.form['magnet']
  962. email = request.form.get('email', DEFAULT_EMAIL)
  963. password = request.form.get('password', DEFAULT_PASSWORD)
  964. if not email or not password:
  965. return jsonify({"status": "error", "error": "Seedr credentials required"})
  966. # Start processing in background
  967. thread = threading.Thread(target=process_magnet, args=(magnet, email, password))
  968. thread.daemon = True
  969. thread.start()
  970. return jsonify({"status": "started"})
  971. @app.route('/test-credentials', methods=['POST'])
  972. def test_credentials():
  973. """Test if Seedr credentials are valid."""
  974. data = request.get_json()
  975. email = data.get('email', '')
  976. password = data.get('password', '')
  977. try:
  978. client = connect_to_seedr(email, password, retries=1)
  979. with client:
  980. # Try to get account info to verify credentials
  981. settings = client.get_settings()
  982. return jsonify({"valid": True})
  983. except Exception as e:
  984. print(f"Credential test failed: {e}")
  985. return jsonify({"valid": False})
  986. @app.route('/account-info', methods=['POST'])
  987. def get_account_info_route():
  988. """Get Seedr account information."""
  989. try:
  990. data = request.get_json()
  991. email = data.get('email', DEFAULT_EMAIL)
  992. password = data.get('password', DEFAULT_PASSWORD)
  993. if not email or not password:
  994. return jsonify({'max_file_size': 'No credentials provided'})
  995. client = connect_to_seedr(email, password)
  996. with client:
  997. account_info = get_account_info(client)
  998. return jsonify(account_info)
  999. except Exception as e:
  1000. return jsonify({
  1001. 'max_file_size': 'Connection Error'
  1002. })
  1003. @app.route('/status')
  1004. def get_status():
  1005. return jsonify(download_status)
  1006. @app.route('/download-file', methods=['GET'])
  1007. def download_file():
  1008. """Redirect to direct download URL for browser download."""
  1009. global download_status
  1010. if download_status["status"] != "completed" or not download_status["download_url"]:
  1011. return "No file ready for download", 400
  1012. # Redirect browser to the direct download URL
  1013. return redirect(download_status["download_url"])
  1014. # Favicon and manifest routes
  1015. @app.route('/favicon.ico')
  1016. def favicon():
  1017. return send_file('favicon.ico')
  1018. @app.route('/apple-touch-icon.png')
  1019. def apple_touch_icon():
  1020. return send_file('apple-touch-icon.png')
  1021. @app.route('/favicon-32x32.png')
  1022. def favicon_32():
  1023. return send_file('favicon-32x32.png')
  1024. @app.route('/favicon-16x16.png')
  1025. def favicon_16():
  1026. return send_file('favicon-16x16.png')
  1027. @app.route('/site.webmanifest')
  1028. def site_manifest():
  1029. return send_file('site.webmanifest')
  1030. @app.route('/android-chrome-192x192.png')
  1031. def android_chrome_192():
  1032. return send_file('android-chrome-192x192.png')
  1033. @app.route('/android-chrome-512x512.png')
  1034. def android_chrome_512():
  1035. return send_file('android-chrome-512x512.png')
  1036. if __name__ == '__main__':
  1037. print("🧲 Starting Simple Magnet2Direct...")
  1038. print(f"📁 Downloads folder: {DOWNLOAD_FOLDER}")
  1039. print("🌐 Open http://localhost:5000")
  1040. app.run(debug=True, host='0.0.0.0', port=5000)