SPB Git

Repository Public

main.py 7.0 KB 165 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project          : KA Apify Actors9# File             : ka-discord/src/main.py10# Module           : ka-discord11# Repository       : https://git.spboucher.ai/ka-apify-actors12# Created          : 2026-08-1813# Last Modified    : 2026-08-1814#15# =============================================================================16# AUTHOR & PROFESSIONAL AFFILIATION17# =============================================================================18#19# Author           : Simon-Pierre Boucher20# Position         : Professor of Finance21# Institution      : Université du Québec en Outaouais (UQO)22# Department       : Département des sciences administratives23# Organization     : SP Boucher24#25# Website          : https://spboucher.ai26# Git              : https://git.spboucher.ai27#28# =============================================================================29# OFFICIAL CONTACT DIRECTORY30# =============================================================================31#32# General          : info@spboucher.ai33# Professional     : contact@spboucher.ai34# Direct           : simon@spboucher.ai35# Research         : research@spboucher.ai36# Development      : dev@spboucher.ai37# Support          : support@spboucher.ai38# Security/Admin   : admin@spboucher.ai39# Notifications    : notifications@spboucher.ai40#41# Preferred Contact: contact@spboucher.ai42#43# =============================================================================44# COPYRIGHT & INTELLECTUAL PROPERTY45# =============================================================================46#47# Copyright (c) 202648# Simon-Pierre Boucher49#50# All rights reserved where applicable.51#52# This file is governed by the applicable project license,53# copyright notices, institutional agreements and relevant law.54#55# Unless expressly authorized by the applicable license or56# rights holder, copying, redistribution, sublicensing,57# publication and commercial exploitation are not permitted.58#59# Existing third-party rights and notices remain unaffected.60#61# =============================================================================62# CONFIDENTIALITY & SECURITY63# =============================================================================64#65# Classification   : Public66# Distribution     : Public repository; use is governed by the project license67# Security Contact : admin@spboucher.ai68#69# Access to this source does not independently grant permission70# to redistribute or disclose confidential materials.71#72# Do not embed credentials, private keys, access tokens,73# certificates or confidential personal information.74#75# =============================================================================76# LICENSE & MAINTENANCE77# =============================================================================78#79# License          : None specified (no license file in this repository)80# Maintainer       : Simon-Pierre Boucher81# Technical Contact: dev@spboucher.ai82#83# =============================================================================84# END OF SP BOUCHER SOURCE HEADER85# =============================================================================8687# ==============================================================================88# Author: Simon-Pierre Boucher <contact@spboucher.ai>89# File:   src/main.py (ka-discord)90# Desc:   Serveurs Discord via l'API PUBLIQUE d'invitation91#         /api/v9/invites/{code}?with_counts=true : membres approximatifs,92#         présents en ligne, nom/description/boosts du serveur. Le CODE d'invit93#         est SENSIBLE À LA CASSE → il vient de l'URL, pas du handle minusculé.94# ==============================================================================95from __future__ import annotations9697import asyncio98import json99100from apify import Actor101102from .net import Fetcher103104INVITE_API = ("https://discord.com/api/v9/invites/{code}"105              "?with_counts=true&with_expiration=true")106107108async def main() -> None:109    async with Actor:110        inp = await Actor.get_input() or {}111        # ici « usernames » = codes d'invitation (casse préservée par l'appelant)112        codes = [c.strip() for c in (inp.get("usernames") or []) if c.strip()]113        proxy = await Actor.create_proxy_configuration(114            actor_proxy_input=inp.get("proxyConfiguration"))115        fetcher = Fetcher(proxy, delay=float(inp.get("requestDelay") or 1))116        sem = asyncio.Semaphore(int(inp.get("concurrency") or 6))117118        async def one(code: str) -> None:119            async with sem:120                miss = {"kind": "profile", "platform": "discord",121                        "username": code, "found": False}122                try:123                    resp = await fetcher.get(124                        INVITE_API.format(code=code), session_id=code[:12],125                        headers={"Accept": "application/json"})126                except Exception as exc:127                    await Actor.push_data({**miss, "error": str(exc)[:200]})128                    return129                if resp.status_code == 404:130                    await Actor.push_data({**miss, "error": "invite_invalide"})131                    return132                if resp.status_code != 200:133                    await Actor.push_data(134                        {**miss, "error": f"http_{resp.status_code}"})135                    return136                try:137                    d = json.loads(resp.text)138                except Exception:139                    await Actor.push_data({**miss, "error": "bad_json"})140                    return141                guild = d.get("guild") or {}142                icon = guild.get("icon")143                gid = guild.get("id")144                await Actor.push_data({145                    "kind": "profile",146                    "platform": "discord",147                    "found": True,148                    "username": code,149                    "guild_id": gid,150                    "full_name": guild.get("name"),151                    "biography": guild.get("description"),152                    "followers": d.get("approximate_member_count"),  # membres153                    "members": d.get("approximate_member_count"),154                    "online": d.get("approximate_presence_count"),155                    "boosts": guild.get("premium_subscription_count"),156                    "verified": ("VERIFIED" in (guild.get("features") or [])),157                    "partnered": ("PARTNERED" in (guild.get("features") or [])),158                    "channel": (d.get("channel") or {}).get("name"),159                    "avatar": (f"https://cdn.discordapp.com/icons/{gid}/{icon}.png"160                               if gid and icon else None),161                })162163        await asyncio.gather(*[one(c) for c in codes])164        Actor.log.info(f"terminé : {len(codes)} invitations")165
Files main
Menu

Theme