Advertisement
ravi80113964

scrapper.py

Oct 1st, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.55 KB | None | 0 0
  1. from telethon.sync import TelegramClient
  2. from telethon.tl.functions.messages import GetDialogsRequest
  3. from telethon.tl.types import InputPeerEmpty
  4. from telethon.tl.types import InputPeerEmpty, InputPeerChannel, InputPeerUser
  5. from telethon.errors.rpcerrorlist import PeerFloodError, UserPrivacyRestrictedError
  6. from telethon.tl.functions.channels import InviteToChannelRequest
  7. import sys
  8. import csv
  9. import os
  10. import random
  11. import traceback
  12. import time
  13. import uuid
  14. import subprocess
  15. import platform
  16. import requests
  17. from config import api_id, api_hash, phone, session_name, target
  18.  
  19. # Function to get and store device ID for Windows
  20. def get_windows_device_id():
  21.     device_id_file = "windows_device_id.txt"  # File to store the device ID for Windows
  22.  
  23.     # Check if device ID already exists
  24.     if os.path.exists(device_id_file):
  25.         with open(device_id_file, "r") as f:
  26.             return f.read().strip()
  27.    
  28.     # Generate a device ID based on the Volume Serial Number for Windows
  29.     device_id = get_windows_volume_serial()
  30.  
  31.     if device_id:
  32.         # Store the device ID in a file for future use
  33.         with open(device_id_file, "w") as f:
  34.             f.write(device_id)
  35.    
  36.     return device_id
  37.  
  38. # Function to get and store device ID for Android/Linux
  39. def get_android_device_id():
  40.     device_id_file = "android_device_id.txt"  # File to store the device ID for Android/Linux
  41.  
  42.     # Check if device ID already exists
  43.     if os.path.exists(device_id_file):
  44.         with open(device_id_file, "r") as f:
  45.             return f.read().strip()
  46.  
  47.     # Generate a device ID based on the MAC address for Android/Linux
  48.     device_id = hex(uuid.getnode())  # This uses the device's MAC address as the unique identifier
  49.  
  50.     if device_id:
  51.         # Store the device ID in a file for future use
  52.         with open(device_id_file, "w") as f:
  53.             f.write(device_id)
  54.    
  55.     return device_id
  56.  
  57. # Function to get the Volume Serial Number on Windows
  58. def get_windows_volume_serial():
  59.     try:
  60.         # Use subprocess to run the 'wmic' command and get the serial number
  61.         result = subprocess.run(["wmic", "volume", "get", "serialnumber"], capture_output=True, text=True)
  62.  
  63.         print(f"WMIC Output: {result.stdout}")  # Debugging: Print the raw output from WMIC
  64.  
  65.         if result.returncode != 0:
  66.             raise Exception(f"wmic command failed with return code {result.returncode}")
  67.  
  68.         # Split the output and filter out empty lines
  69.         lines = [line.strip() for line in result.stdout.strip().split("\n") if line.strip()]
  70.  
  71.         # Ensure that we have at least two lines (header and serial number)
  72.         if len(lines) < 2:
  73.             raise Exception("No serial number found in WMIC output")
  74.  
  75.         serial_number = lines[1]  # The serial number should be on the second line
  76.         return serial_number
  77.     except Exception as e:
  78.         print(f"Error fetching volume serial number: {e}")
  79.         return None
  80.  
  81. # Function to verify the license
  82. def check_license(license_key, device_id):
  83.     if device_id is None:
  84.         print("Unable to retrieve device ID")
  85.         sys.exit()
  86.  
  87.     # Print device ID for debugging
  88.     print(f"Device ID: {device_id}")
  89.  
  90.     # Send request to PHP API for license verification
  91.     response = requests.post("https://blaze.freecodes.in/verify_license.php", data={
  92.         'license_key': license_key,
  93.         'device_id': device_id
  94.     })
  95.  
  96.     # Debugging: Print server response
  97.     print(f"Response from server: {response.text}")
  98.  
  99.     try:
  100.         return response.json()
  101.     except ValueError:
  102.         print("Failed to decode JSON from server response.")
  103.         sys.exit()
  104.  
  105. # Function to add user to a group for Windows
  106. def add_user_windows():
  107.     license_key = input("Enter your license key: ")
  108.     device_id = get_windows_device_id()
  109.     result = check_license(license_key, device_id)
  110.  
  111.     if result['status'] != 'success':
  112.         print(f"License verification failed: {result['message']}")
  113.         sys.exit()
  114.  
  115.     print("License verified! Proceeding with adding user on Windows...")
  116.  
  117.     print('Initializing Telegram client...')
  118.     client = TelegramClient(str(session_name), api_id, api_hash)
  119.  
  120.     client.connect()
  121.     if not client.is_user_authorized():
  122.         client.send_code_request(phone)
  123.         client.sign_in(phone, input('Enter the code: '))
  124.  
  125.     # Add user functionality here
  126.     user_to_add = input("Enter the username or ID of the user to add: ")
  127.  
  128.     try:
  129.         user = client.get_input_entity(user_to_add)
  130.         client(functions.channels.InviteToChannelRequest(
  131.             channel=target,
  132.             users=[user]
  133.         ))
  134.         print(f"User {user_to_add} added successfully to the group on Windows!")
  135.     except Exception as e:
  136.         print(f"Failed to add user {user_to_add}: {e}")
  137.  
  138. # Function to add user to a group for Android/Linux
  139. def add_user_android():
  140.     license_key = input("Enter your license key: ")
  141.     device_id = get_android_device_id()
  142.     result = check_license(license_key, device_id)
  143.  
  144.     if result['status'] != 'success':
  145.         print(f"License verification failed: {result['message']}")
  146.         sys.exit()
  147.  
  148.     print("License verified! Proceeding with adding user on Android/Linux...")
  149.  
  150.     print('Initializing Telegram client...')
  151.     client = TelegramClient(str(session_name), api_id, api_hash)
  152.  
  153.     client.connect()
  154.     if not client.is_user_authorized():
  155.         client.send_code_request(phone)
  156.         client.sign_in(phone, input('Enter the code: '))
  157.  
  158.     # Add user functionality here
  159.     user_to_add = input("Enter the username or ID of the user to add: ")
  160.  
  161.     try:
  162.         user = client.get_input_entity(user_to_add)
  163.         client(functions.channels.InviteToChannelRequest(
  164.             channel=target,
  165.             users=[user]
  166.         ))
  167.         print(f"User {user_to_add} added successfully to the group on Android/Linux!")
  168.     except Exception as e:
  169.         print(f"Failed to add user {user_to_add}: {e}")
  170.  
  171. # Function to run the scraper for Windows
  172. def run_scraper_windows():
  173.     license_key = input("Enter your license key: ")
  174.     device_id = get_windows_device_id()
  175.     result = check_license(license_key, device_id)
  176.  
  177.     if result['status'] != 'success':
  178.         print(f"License verification failed: {result['message']}")
  179.         sys.exit()
  180.  
  181.     print("License verified! Proceeding with the scraper on Windows...")
  182.  
  183.     print('Initializing Telegram client...')
  184.     client = TelegramClient(str(session_name), api_id, api_hash)
  185.  
  186.     client.connect()
  187.     if not client.is_user_authorized():
  188.         client.send_code_request(phone)
  189.         client.sign_in(phone, input('Enter the code: '))
  190.  
  191.     print('Fetching Members...')
  192.     all_participants = []
  193.  
  194.     # Fetch participants from the target group or channel
  195.     try:
  196.         all_participants = client.iter_participants(target, limit=None, filter=None, aggressive=True)
  197.     except Exception as e:
  198.         print(f"Error occurred while fetching participants: {e}")
  199.         sys.exit()
  200.  
  201.     print('Saving to file...')
  202.     with open("data_windows.csv", "w", encoding='UTF-8') as f:
  203.         writer = csv.writer(f, delimiter=",", lineterminator="\n")
  204.         writer.writerow(['sr. no.', 'username', 'user id', 'name', 'Status'])
  205.         i = 0
  206.         for user in all_participants:
  207.             i += 1
  208.             username = user.username if user.username else ""
  209.             first_name = user.first_name if user.first_name else ""
  210.             last_name = user.last_name if user.last_name else ""
  211.             name = (first_name + ' ' + last_name).strip()
  212.             writer.writerow([i, username, user.id, name, 'group name'])
  213.  
  214.     print('Members scraped successfully.')
  215.  
  216. # Function to run the scraper for Android/Linux
  217. def run_scraper_android():
  218.     license_key = input("Enter your license key: ")
  219.     device_id = get_android_device_id()
  220.     result = check_license(license_key, device_id)
  221.  
  222.     if result['status'] != 'success':
  223.         print(f"License verification failed: {result['message']}")
  224.         sys.exit()
  225.  
  226.     print("License verified! Proceeding with the scraper on Android/Linux...")
  227.  
  228.     print('Initializing Telegram client...')
  229.     client = TelegramClient(str(session_name), api_id, api_hash)
  230.  
  231.     client.connect()
  232.     if not client.is_user_authorized():
  233.         client.send_code_request(phone)
  234.         client.sign_in(phone, input('Enter the code: '))
  235.  
  236.     print('Fetching Members...')
  237.     all_participants = []
  238.  
  239.     # Fetch participants from the target group or channel
  240.     try:
  241.         all_participants = client.iter_participants(target, limit=None, filter=None, aggressive=True)
  242.     except Exception as e:
  243.         print(f"Error occurred while fetching participants: {e}")
  244.         sys.exit()
  245.  
  246.     print('Saving to file...')
  247.     with open("data_android.csv", "w", encoding='UTF-8') as f:
  248.         writer = csv.writer(f, delimiter=",", lineterminator="\n")
  249.         writer.writerow(['sr. no.', 'username', 'user id', 'name', 'Status'])
  250.         i = 0
  251.         for user in all_participants:
  252.             i += 1
  253.             username = user.username if user.username else ""
  254.             first_name = user.first_name if user.first_name else ""
  255.             last_name = user.last_name if user.last_name else ""
  256.             name = (first_name + ' ' + last_name).strip()
  257.             writer.writerow([i, username, user.id, name, 'group name'])
  258.  
  259.     print('Members scraped successfully.')
  260.  
  261. # Main menu function
  262. def main_menu():
  263.     print("Choose an option:")
  264.     print("1 - Get Device ID (Windows)")
  265.     print("2 - Get Device ID (Android/Linux)")
  266.     print("3 - Run Scraper (Windows)")
  267.     print("4 - Run Scraper (Android/Linux)")
  268.     print("5 - Add User (Windows)")
  269.     print("6 - Add User (Android/Linux)")
  270.     choice = input("Enter your choice (1, 2, 3, 4, 5, or 6): ")
  271.  
  272.     if choice == '1':
  273.         device_id = get_windows_device_id()
  274.         if device_id:
  275.             print(f"Your Windows Device ID: {device_id}")
  276.         else:
  277.             print("Failed to retrieve Windows Device ID")
  278.     elif choice == '2':
  279.         device_id = get_android_device_id()
  280.         if device_id:
  281.             print(f"Your Android/Linux Device ID: {device_id}")
  282.         else:
  283.             print("Failed to retrieve Android/Linux Device ID")
  284.     elif choice == '3':
  285.         run_scraper_windows()
  286.     elif choice == '4':
  287.         run_scraper_android()
  288.     elif choice == '5':
  289.         add_user_windows()
  290.     elif choice == '6':
  291.         add_user_android()
  292.     else:
  293.         print("Invalid choice. Please select 1, 2, 3, 4, 5, or 6.")
  294.  
  295. if __name__ == "__main__":
  296.     main_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement