summaryrefslogtreecommitdiffstats
path: root/sl_bot.py
blob: 1fe5294f67cd3263834bbb6e2529a24897add0ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3
import os, httpx, json, hashlib, logging
from datetime import datetime
from telegram import (
    Update,
    InlineQueryResultArticle,
    InputTextMessageContent,
    InlineKeyboardButton,
    InlineKeyboardMarkup,
    LinkPreviewOptions,
)
from telegram.ext import (
    Application,
    InlineQueryHandler,
    ChosenInlineResultHandler,
    MessageHandler,
    CallbackQueryHandler,
    filters,
)
import io

logging.basicConfig(
    format="%(asctime)s | %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    level=logging.INFO,
)
log = logging.getLogger(__name__)

API_KEY = "dc56cf41ff66114030f7766e547f1d64d99356982093bb4b74506e88281ce198"
ALLOWED = {8066036773, 8546929916, 8303244085, 7661900680, 7316173289, 7838198850}
PROXY = "http://MW1BWBJK28_US_5_LOHMZVIP:[email protected]:8080"


import re


def process_response(text, fmt):
    if fmt == "json":
        try:
            data = json.loads(text)
            results = data.get("results", [])
            return json.dumps(results, indent=2)
        except:
            return text
    return text


def safe_filename(query):
    name = re.sub(r'[<>:"/\\|?*]', "_", query)
    name = re.sub(r"https?://", "", name)
    name = name.strip("._")[:50]
    return name or "results"


async def inline(update: Update, _):
    user = update.inline_query.from_user
    log.info(
        f"INLINE | user={user.id} ({user.username}) | query='{update.inline_query.query}'"
    )
    if user.id not in ALLOWED:
        return
    if not (query := update.inline_query.query.strip()):
        return
    formats = [
        ("ulp", "ULP", "URL:Login:Password format"),
        ("lp", "LP", "Login:Password format"),
        ("u", "URL", "URL only"),
        ("l", "Login", "Login only"),
        ("p", "Password", "Password only"),
        ("json", "JSON", "Full JSON format"),
    ]
    await update.inline_query.answer(
        [
            InlineQueryResultArticle(
                id=fmt_id,
                title=f"Search: {query} ({fmt_name})",
                description=fmt_desc,
                input_message_content=InputTextMessageContent(f"Searching: {query}..."),
                reply_markup=InlineKeyboardMarkup(
                    [[InlineKeyboardButton("Loading...", callback_data="noop")]]
                ),
            )
            for fmt_id, fmt_name, fmt_desc in formats
        ],
        cache_time=0,
    )


async def chosen(update: Update, context):
    user = update.chosen_inline_result.from_user
    result_id = update.chosen_inline_result.result_id
    log.info(
        f"CHOSEN | user={user.id} ({user.username}) | query='{update.chosen_inline_result.query}' | format={result_id}"
    )
    if user.id not in ALLOWED:
        return
    query = update.chosen_inline_result.query.strip()
    if not query:
        return
    try:
        inline_message_id = update.chosen_inline_result.inline_message_id
        result_id = update.chosen_inline_result.result_id
        fmt = result_id if result_id in ("ulp", "lp", "u", "l", "p", "json") else "ulp"
        loading_markup = InlineKeyboardMarkup(
            [[InlineKeyboardButton("⏳ Searching...", callback_data="noop")]]
        )
        if inline_message_id:
            await context.bot.edit_message_text(
                inline_message_id=inline_message_id,
                text=f"Searching: {query}...",
                reply_markup=loading_markup,
            )
        log.info(f"API | fetching format={fmt}")
        async with httpx.AsyncClient(proxy=PROXY, timeout=120) as client:
            resp = await client.get(
                "https://db.org.ai/sl/search",
                params={"query": query, "max_hits": 10000, "format": fmt},
                headers={"X-API-Key": API_KEY},
            )
            txt = (
                process_response(resp.text, fmt)
                if resp.status_code == 200
                else "No results"
            )
            lines = len(txt.splitlines())
            log.info(f"API | status={resp.status_code} | lines={lines}")

        if lines == 0:
            if inline_message_id:
                await context.bot.edit_message_text(
                    inline_message_id=inline_message_id,
                    text=f"❌ No results found for: {query}",
                )
            return

        uploading_markup = InlineKeyboardMarkup(
            [[InlineKeyboardButton("⏳ Uploading...", callback_data="noop")]]
        )
        if inline_message_id:
            await context.bot.edit_message_text(
                inline_message_id=inline_message_id,
                text=f"Uploading {lines} lines...",
                reply_markup=uploading_markup,
            )
        upload_headers = {"User-Agent": "curl/8.0.1"}
        try:
            upload = await client.post(
                "https://0x.at",
                files={"file": (f"{safe_filename(query)}.txt", txt.encode())},
                headers=upload_headers,
            )
            url = upload.text.strip()
        except Exception:
            async with httpx.AsyncClient(timeout=30, verify=False) as upload_client:
                upload = await upload_client.post(
                    "https://0x.at",
                    files={"file": (f"{safe_filename(query)}.txt", txt.encode())},
                    headers=upload_headers,
                )
                url = upload.text.strip()
        log.info(f"UPLOAD | url={url}")

        if inline_message_id:
            await context.bot.edit_message_text(
                inline_message_id=inline_message_id,
                text=f"🔗 {query} - {lines} lines ({fmt.upper()})",
                reply_markup=InlineKeyboardMarkup(
                    [[InlineKeyboardButton("📥 Download", url=url)]]
                ),
            )
            log.info(
                f"DONE | user={user.id} | query='{query}' | lines={lines} | url={url}"
            )
    except httpx.TimeoutException:
        log.error(f"TIMEOUT | user={user.id} | query='{query}'")
        if inline_message_id:
            await context.bot.edit_message_text(
                inline_message_id=inline_message_id,
                text=f"⏱️ Request timed out for: {query}",
            )
    except Exception as e:
        log.error(f"ERROR | user={user.id} | query='{query}' | error={e}")


async def dm(update: Update, context):
    user = update.message.from_user
    if user.id not in ALLOWED:
        return
    query = update.message.text.strip()
    if not query:
        return
    log.info(f"DM | user={user.id} ({user.username}) | query='{query}'")

    format_buttons = InlineKeyboardMarkup(
        [
            [
                InlineKeyboardButton("ULP", callback_data=f"dm:ulp:{query}"),
                InlineKeyboardButton("LP", callback_data=f"dm:lp:{query}"),
                InlineKeyboardButton("JSON", callback_data=f"dm:json:{query}"),
            ],
            [
                InlineKeyboardButton("URL", callback_data=f"dm:u:{query}"),
                InlineKeyboardButton("Login", callback_data=f"dm:l:{query}"),
                InlineKeyboardButton("Password", callback_data=f"dm:p:{query}"),
            ],
        ]
    )
    await update.message.reply_text(
        f"Select format for: {query}",
        reply_markup=format_buttons,
    )


async def dm_callback(update: Update, context):
    query_obj = update.callback_query
    await query_obj.answer()

    user = query_obj.from_user
    if user.id not in ALLOWED:
        return

    data = query_obj.data
    if not data.startswith("dm:"):
        return

    parts = data.split(":", 2)
    if len(parts) != 3:
        return

    _, fmt, query = parts
    log.info(
        f"DM_CALLBACK | user={user.id} ({user.username}) | query='{query}' | format={fmt}"
    )

    await query_obj.edit_message_text(f"⏳ Searching: {query}...")

    try:
        async with httpx.AsyncClient(proxy=PROXY, timeout=120) as client:
            resp = await client.get(
                "https://db.org.ai/sl/search",
                params={"query": query, "max_hits": 10000, "format": fmt},
                headers={"X-API-Key": API_KEY},
            )
            txt = process_response(resp.text, fmt) if resp.status_code == 200 else ""
            lines = len(txt.splitlines())
            log.info(f"API | status={resp.status_code} | lines={lines}")

        if lines == 0:
            await query_obj.edit_message_text(f"❌ No results found for: {query}")
            log.info(f"DONE | user={user.id} | query='{query}' | no results")
            return

        await query_obj.edit_message_text(f"⏳ Uploading {lines} lines...")

        await query_obj.message.reply_document(
            document=io.BytesIO(txt.encode()),
            filename=f"{safe_filename(query)}.txt",
            caption=f"🔗 {query} - {lines} lines ({fmt.upper()})",
        )
        await query_obj.delete_message()
        log.info(
            f"DONE | user={user.id} | query='{query}' | lines={lines} | sent document"
        )
    except httpx.TimeoutException:
        log.error(f"TIMEOUT | user={user.id} | query='{query}'")
        await query_obj.edit_message_text(f"⏱️ Request timed out for: {query}")
    except Exception as e:
        log.error(f"ERROR | user={user.id} | query='{query}' | error={e}")
        await query_obj.edit_message_text(f"❌ Error: {e}")


app = (
    Application.builder()
    .token("8511396304:AAHn0da5vx7bLoMED7THD8on0mXNvNjY3Ks")
    .build()
)
app.add_handler(InlineQueryHandler(inline))
app.add_handler(ChosenInlineResultHandler(chosen))
app.add_handler(MessageHandler(filters.TEXT & filters.ChatType.PRIVATE, dm))
app.add_handler(CallbackQueryHandler(dm_callback, pattern="^dm:"))
app.run_polling()