Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,7 @@ import time
|
|
| 20 |
from webdriver_manager.chrome import ChromeDriverManager
|
| 21 |
from fastapi import BackgroundTasks
|
| 22 |
from tqdm import tqdm
|
|
|
|
| 23 |
|
| 24 |
load_dotenv()
|
| 25 |
|
|
@@ -531,13 +532,18 @@ def simulate_spotify_view(video_id: str, proxy: Proxy, session: requests.Session
|
|
| 531 |
finally:
|
| 532 |
driver.quit()
|
| 533 |
|
|
|
|
| 534 |
def simulate_views_background(url: str, platform: str, count: int, delay: int, parallel_processes: int, session: requests.Session = None):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
video_id = extract_video_id(url, platform)
|
| 536 |
proxy = get_random_proxy()
|
| 537 |
if not proxy:
|
| 538 |
return
|
| 539 |
|
| 540 |
-
for i in tqdm(range(count)):
|
| 541 |
try:
|
| 542 |
if platform == "instagram":
|
| 543 |
simulate_instagram_view(video_id, proxy, session)
|
|
@@ -551,7 +557,7 @@ def simulate_views_background(url: str, platform: str, count: int, delay: int, p
|
|
| 551 |
simulate_twitch_view(video_id, proxy, session)
|
| 552 |
elif platform == "spotify":
|
| 553 |
simulate_spotify_view(video_id, proxy, session)
|
| 554 |
-
|
| 555 |
except Exception as e:
|
| 556 |
pass
|
| 557 |
|
|
@@ -561,7 +567,7 @@ async def simulate_views_endpoint(request: VisitRequest, background_tasks: Backg
|
|
| 561 |
session = None
|
| 562 |
if os.getenv(f'{request.platform.upper()}_USER') and os.getenv(f'{request.platform.upper()}_PASSWORD'):
|
| 563 |
session = authenticate(os.getenv(f'{request.platform.upper()}_USER'), os.getenv(f'{request.platform.upper()}_PASSWORD'), request.platform)
|
| 564 |
-
|
| 565 |
background_tasks.add_task(
|
| 566 |
simulate_views_background,
|
| 567 |
url=request.url,
|
|
@@ -602,18 +608,23 @@ async def login(username: str = Form(...), password: str = Form(...), platform:
|
|
| 602 |
return e
|
| 603 |
|
| 604 |
@app.post("/simulate")
|
| 605 |
-
async def simulate(urls: str = Form(...), platform: str = Form(...), count: int = Form(...), delay: int = Form(...), parallel_processes: int = Form(...)):
|
| 606 |
try:
|
| 607 |
session = None
|
| 608 |
if os.getenv(f'{platform.upper()}_USER') and os.getenv(f'{platform.upper()}_PASSWORD'):
|
| 609 |
session = authenticate(os.getenv(f'{platform.upper()}_USER'), os.getenv(f'{platform.upper()}_PASSWORD'), platform)
|
| 610 |
|
| 611 |
for index, url in enumerate(urls.split("\n")):
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
except Exception as e:
|
| 619 |
return PlainTextResponse(str(e), status_code=500)
|
|
|
|
| 20 |
from webdriver_manager.chrome import ChromeDriverManager
|
| 21 |
from fastapi import BackgroundTasks
|
| 22 |
from tqdm import tqdm
|
| 23 |
+
import asyncio
|
| 24 |
|
| 25 |
load_dotenv()
|
| 26 |
|
|
|
|
| 532 |
finally:
|
| 533 |
driver.quit()
|
| 534 |
|
| 535 |
+
|
| 536 |
def simulate_views_background(url: str, platform: str, count: int, delay: int, parallel_processes: int, session: requests.Session = None):
|
| 537 |
+
for _ in range(parallel_processes):
|
| 538 |
+
asyncio.run(simulate_one_view_process(url, platform, count, delay, session))
|
| 539 |
+
|
| 540 |
+
async def simulate_one_view_process(url: str, platform: str, count: int, delay: int, session: requests.Session = None):
|
| 541 |
video_id = extract_video_id(url, platform)
|
| 542 |
proxy = get_random_proxy()
|
| 543 |
if not proxy:
|
| 544 |
return
|
| 545 |
|
| 546 |
+
for i in tqdm(range(count), desc=f"Simulating views for {url}"):
|
| 547 |
try:
|
| 548 |
if platform == "instagram":
|
| 549 |
simulate_instagram_view(video_id, proxy, session)
|
|
|
|
| 557 |
simulate_twitch_view(video_id, proxy, session)
|
| 558 |
elif platform == "spotify":
|
| 559 |
simulate_spotify_view(video_id, proxy, session)
|
| 560 |
+
await asyncio.sleep(delay)
|
| 561 |
except Exception as e:
|
| 562 |
pass
|
| 563 |
|
|
|
|
| 567 |
session = None
|
| 568 |
if os.getenv(f'{request.platform.upper()}_USER') and os.getenv(f'{request.platform.upper()}_PASSWORD'):
|
| 569 |
session = authenticate(os.getenv(f'{request.platform.upper()}_USER'), os.getenv(f'{request.platform.upper()}_PASSWORD'), request.platform)
|
| 570 |
+
|
| 571 |
background_tasks.add_task(
|
| 572 |
simulate_views_background,
|
| 573 |
url=request.url,
|
|
|
|
| 608 |
return e
|
| 609 |
|
| 610 |
@app.post("/simulate")
|
| 611 |
+
async def simulate(urls: str = Form(...), platform: str = Form(...), count: int = Form(...), delay: int = Form(...), parallel_processes: int = Form(...), background_tasks: BackgroundTasks):
|
| 612 |
try:
|
| 613 |
session = None
|
| 614 |
if os.getenv(f'{platform.upper()}_USER') and os.getenv(f'{platform.upper()}_PASSWORD'):
|
| 615 |
session = authenticate(os.getenv(f'{platform.upper()}_USER'), os.getenv(f'{platform.upper()}_PASSWORD'), platform)
|
| 616 |
|
| 617 |
for index, url in enumerate(urls.split("\n")):
|
| 618 |
+
background_tasks.add_task(
|
| 619 |
+
simulate_views_background,
|
| 620 |
+
url=url,
|
| 621 |
+
platform=platform,
|
| 622 |
+
count=count,
|
| 623 |
+
delay=delay,
|
| 624 |
+
parallel_processes=parallel_processes,
|
| 625 |
+
session=session
|
| 626 |
+
)
|
| 627 |
+
return PlainTextResponse("Simulations started in the background.")
|
| 628 |
|
| 629 |
except Exception as e:
|
| 630 |
return PlainTextResponse(str(e), status_code=500)
|