vampire0817

send-media

Nov 3rd, 2023
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. import csv
  2. import requests
  3. import time
  4. import threading
  5. from queue import Queue
  6.  
  7. api_url = "https://repy.in/send-media"
  8. jwt_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFscGhhQGdtYWlsLmNvbSIsImNsaW5lSWQiOiJkcmI2ZGxjdTYzdHBoenMiLCJpYXQiOjE2OTg5ODk2OTJ9.Tm0h_GLDgnIKr2kbfwBgRvUepl6NEdS22v0rqmnzjvk"
  9.  
  10. # Function to send media to a phone number
  11. def send_media(phone, media_url):
  12.     headers = {
  13.         "Authorization": f"Bearer {jwt_token}",
  14.         "Content-Type": "application/json",
  15.     }
  16.  
  17.     payload = {
  18.         "phone": phone,
  19.         "mediaUrl": media_url,
  20.     }
  21.  
  22.     retries = 3
  23.     success = False
  24.     while retries > 0:
  25.         response = requests.post(api_url, json=payload, headers=headers)
  26.  
  27.         if response.status_code == 200:
  28.             success = True
  29.             print(f"Media sent to {phone} successfully")
  30.             break
  31.         else:
  32.             try:
  33.                 error_message = response.json()["message"]
  34.                 print(f"Failed to send media to {phone}. Error message: {error_message}")
  35.             except:
  36.                 print(f"Failed to send media to {phone}. Unable to parse error message.")
  37.             retries -= 1
  38.             time.sleep(1)  # Wait for 1 second before retrying
  39.  
  40.     if not success:
  41.         # Append the failed data to a CSV file
  42.         with open("failed_records.csv", "a", newline="") as file:
  43.             writer = csv.writer(file)
  44.             writer.writerow([phone, media_url])
  45.            
  46. # Function to process the CSV file
  47. def process_csv():
  48.     processed_phones = set()  # To keep track of processed phone numbers
  49.     while not csv_queue.empty():
  50.         phone, media_url = csv_queue.get()
  51.         if phone not in processed_phones:
  52.             send_media(phone, media_url)
  53.             processed_phones.add(phone)
  54.         csv_queue.task_done()
  55.  
  56. # Limit API calls to 40 per minute
  57. def throttle_api_calls():
  58.     while not csv_queue.empty():
  59.         for _ in range(40):
  60.             if not csv_queue.empty():
  61.                 phone, media_url = csv_queue.get()
  62.                 send_media(phone, media_url)
  63.                 csv_queue.task_done()
  64.             else:
  65.                 break
  66.  
  67. # Read phone numbers and media URLs from a CSV file
  68. csv_file = "demo.csv"
  69.  
  70. csv_queue = Queue()
  71. with open(csv_file, "r") as file:
  72.     reader = csv.DictReader(file)
  73.     for row in reader:
  74.         phone = row["phone"]
  75.         media_url = row["mediaUrl"]
  76.         csv_queue.put((phone, media_url))
  77.  
  78. # Start a separate thread to limit API calls
  79. api_thread = threading.Thread(target=throttle_api_calls)
  80. api_thread.start()
  81.  
  82. # Process the CSV file
  83. process_csv()
  84.  
  85. # Wait for the API thread and CSV processing to finish
  86. api_thread.join()
  87. csv_queue.join()
Advertisement
Add Comment
Please, Sign In to add comment