import os import json import requests import openpyxl from concurrent.futures import…
- User
import os import json import requests import openpyxl from concurrent.futures import ThreadPoolExecutor import traceback
Function to send push notifications
def send_push_notification(group, payload): # Create an xlsx file to store the results wb = openpyxl.Workbook() sheet = wb.active sheet['A1'] = 'Token' sheet['B1'] = 'Success'
try: # Update the payload with the current token group payload['registration_ids'] = group # Send the request and get the response response = requests.post( 'https://fcm.googleapis.com/fcm/send', headers={'Authorization': 'key=A', 'Content-Type': 'application/json'}, data=json.dumps(payload) ) result = response.json() # Write the result to the xlsx file for i, token in enumerate(group): success = result['results'][i].get('success', 0) sheet.cell(row=i+2, column=1).value = token sheet.cell(row=i+2, column=2).value = success except Exception as e: print(f"Error in thread: {traceback.format_exc()}") finally: # Save the xlsx file wb.save('result.xlsx')Function to read tokens from a file
def read_tokens_from_file(file_path): with open(file_path, 'r') as f: tokens = f.read().split(',') return tokens
def send_notifications_concurrently(tokens, payload): # Split tokens into groups of 500 token_groups = [tokens[i:i+500] for i in range(0, len(tokens), 500)]
# Create a pool of workers with ThreadPoolExecutor(max_workers=4) as executor: executor.map(send_push_notification, token_groups, [payload]*len(token_groups))Process Android tokens
android_files = [f for f in os.listdir('.') if f.startswith('android') and f.endswith('.txt')] for file in android_files: tokens = read_tokens_from_file(file) payload = { "notification": { "title": "ثانیههای یک میلیونی در جشنواره پلانو", "body": "تماشای رایگان «توطئه آمیز» با جایزه", "image": "https://pelano.net/uploads/push/CinemaFestival/01.jpg", "icon": "https://pelano.net/uploads/Pelano_Push_Logo.png", "sound": "default", }, "data": { "url": "https://pelano.net/Campaign?PF=CB", "image": "https://pelano.net/uploads/push/CinemaFestival/01.jpg" }, "priority": "high", "content_available": True, "mutable_content": True, } send_notifications_concurrently(tokens, payload)
Process web tokens
web_files = [f for f in os.listdir('.') if f.startswith('web') and f.endswith('.txt')] for file in web_files: tokens = read_tokens_from_file(file) payload = { "notification": { "title": "ثانیههای یک میلیونی در جشنواره پلانو", "body": "تماشای رایگان «توطئه آمیز» با جایزه", "image": "", "icon": "", "sound": "default", "click_action": "", }, "priority": "high", "content_available": True, "mutable_content": True, } send_notifications_concurrently(tokens, payload) in the top code i want to print response please give me the full edited code
- Assistant