espejelomar's picture
init: begin
266c6a7
raw
history blame
2.18 kB
import pandas as pd
import re
import gradio as gr
# Constants
CORE_STARS_CSV_PATH = "core_stars.csv"
OUTPUT_CORE_STARS_TXT_PATH = "core_stars.txt"
# Normalize Telegram IDs
def normalize_id(telegram_id):
telegram_id = str(telegram_id).lower().strip()
if telegram_id.startswith("https://t.me/"):
telegram_id = telegram_id.replace("https://t.me/", "")
telegram_id = telegram_id.lstrip("@")
telegram_id = re.sub(r"[^a-zA-Z0-9_]", "", telegram_id)
return telegram_id
# Create core_stars.txt
def create_core_stars_txt(csv_path, output_path, column_name="Telegram Handle"):
df = pd.read_csv(csv_path)
normalized_ids = df[column_name].apply(normalize_id).unique()
with open(output_path, "w") as file:
for id in normalized_ids:
file.write(id + "\n")
# Read core_stars_ids
def read_core_stars_ids(file_path):
with open(file_path, "r") as file:
return [normalize_id(line) for line in file]
# Process uploaded CSV and return updated CSV
def process_csv(uploaded_csv, column_name):
# create_core_stars_txt(CORE_STARS_CSV_PATH, OUTPUT_CORE_STARS_TXT_PATH)
core_stars_ids = read_core_stars_ids(OUTPUT_CORE_STARS_TXT_PATH)
df = pd.read_csv(uploaded_csv)
df[column_name] = df[column_name].apply(normalize_id)
df["is_in_core_stars"] = df[column_name].apply(
lambda id: 1 if id in core_stars_ids else 0
)
cols = ["is_in_core_stars"] + [
col for col in df.columns if col != "is_in_core_stars"
]
df = df[cols]
# Save to a temporary file and return
output_file = "updated_file.csv"
df.to_csv(output_file, index=False)
return output_file
# Set up Gradio interface
iface = gr.Interface(
fn=process_csv,
inputs=[
gr.File(label="Upload CSV File"),
gr.Textbox(label="Enter Column Name for Telegram ID")
],
outputs=gr.File(label="Download Updated CSV"),
title="Telegram Core Stars Membership Checker",
description="With love by @espejelomar! Upload a CSV file and enter the column name to check if each Telegram user is in the Core Stars list."
)
iface.launch()
if __name__ == "__main__":
iface.launch()