Created
April 3, 2024 04:23
-
-
Save southxzx/33d11786a928593c2a439c5a0bf73fc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import requests | |
url = 'https://abc/api' | |
api_key = '123' | |
file_path = 'magiclink_batch.csv' | |
file_path_output = 'magiclink_batch_output.csv' | |
cookies = {'token': api_key} | |
file_fields = ["Full Name", "Phone", "Email", "TTL", "Picture URL", "Magic Link"] | |
def parse_csv_with_headers(file_path): | |
data = [] | |
with open(file_path, 'r+') as file: | |
csv_reader = csv.DictReader(file) | |
for row in csv_reader: | |
data.append(row) | |
return data | |
def save_ml_to_csv(file_path, magic_link): | |
with open(file_path, 'w') as file: | |
writer = csv.writer(file) | |
writer.writerow(['Magic Link']) | |
writer.writerow([magic_link]) | |
parsed_data = parse_csv_with_headers(file_path) | |
with open(file_path_output, 'w') as file: | |
writer = csv.DictWriter(file, fieldnames=file_fields) | |
writer.writeheader() | |
for index, row in enumerate(parsed_data, 0): | |
response = requests.get(url + '/admin/users/' + row['Email'], cookies=cookies) | |
if response.status_code != 200: | |
requests.post(url + '/admin/users', json={'fullname': row['Full Name'], 'email': row['Email'], 'phone': row['Phone'], 'pictureUrl': row['Picture URL']}, cookies=cookies) | |
ml_url = url + '/admin/users/' + row['Email'] + '/magiclink' | |
if row['TTL']: | |
ml_url += '?ttl=' + row['TTL'] | |
ml_response = requests.get(ml_url, cookies=cookies) | |
if ml_response.status_code == 200: | |
print('\033[92mMagic link for', row['Email'], 'is: \033[0m', ml_response.text) | |
row['Magic Link'] = ml_response.text | |
writer.writerow(row) | |
else: | |
print('\033[91mFailed to create magic link for', row['Email'], '\033[0m') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment