Untitled

 avatar
unknown
plain_text
2 years ago
3.2 kB
3
Indexable
from pyrogram import Client
import requests
import os
import time
import re
from tqdm import tqdm

api_id = 
api_hash = ''

client = Client('hollywoodhindi', api_id, api_hash)

# Telegram channel name or ID
channel_username = '-'

# File to store IDs of uploaded files
uploaded_ids_file = 'hollywoodhindi_done.txt'

def download_file(url):
    with requests.get(url, stream=True) as r:
        if r.status_code == 403:
            print("Skipping download due to 403 status code")
            return None, 0
        if r.status_code == 500:
            download_file(url)
        file_name = re.findall('filename="(.+)"', r.headers.get('Content-Disposition'))[0]
        file_size = int(r.headers.get('Content-Length', 0))
        if os.path.isfile(file_name) and os.path.getsize(file_name) == file_size:
            print("File already exists:", file_name)
            return file_name, file_size
        print(f"Downloading {file_name}...")
        with open(file_name, "wb") as f:
            for chunk in tqdm(r.iter_content(chunk_size=1048576)):
                if chunk:
                    f.write(chunk)
        print(f"Downloaded {file_name}!")
        return file_name, file_size

def upload_file(file_path):
    retries = 5
    for i in range(retries):
        try:
            with tqdm(total=os.path.getsize(file_path), unit='B', unit_scale=True, desc=f"Uploading {os.path.basename(file_path)}") as pbar:
                progress_callback = lambda current, total: pbar.update(current - pbar.n)
                result = client.send_document(int(channel_username), file_path, progress=progress_callback)
            print(f'Uploaded {file_path} with ID {result.document.file_id}')
            os.remove(file_path)
            return result.document.file_id
        except Exception as e:
            print(f'Error uploading {file_path}: {e}')
            print(f'Retrying in 10 seconds...')
            time.sleep(10)
    return None

if __name__ == '__main__':
    client.start()
    if not client.is_connected:
        print('Error connecting to Telegram')
        exit()

    uploaded_ids = set()
    if os.path.isfile(uploaded_ids_file):
        with open(uploaded_ids_file, 'r') as f:
            uploaded_ids = set(f.read().splitlines())

    with open('hollywoodhindi.txt', 'r') as f:
        file_ids = f.read().splitlines()

    for file_id in file_ids:
        if file_id in uploaded_ids:
            print(f'{file_id} already uploaded')
            continue

        link = f'https://spdownload.b-f.workers.dev/download/{file_id}'
        file_path, file_size = download_file(link)
        if file_path is None:
            print("Download failed due to 403 error")
            continue
        elif file_size == 0:
            print("Download failed due to unknown error")
            continue
        else:
            print(f"Downloaded {file_path} ({file_size} bytes)")
        uploaded_id = upload_file(file_path)
        if uploaded_id:
            uploaded_ids.add(file_id)
            with open(uploaded_ids_file, 'a') as f:
                f.write(f'{file_id}\n')

    client.stop()
Editor is loading...