File size: 5,807 Bytes
1f26706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Ultroid - UserBot
# Copyright (C) 2021-2025 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.


import asyncio
import os
import random
from random import shuffle
import aiohttp
import re
from telethon.tl.functions.photos import UploadProfilePhotoRequest
from PIL import Image

from pyUltroid.fns.helper import download_file, fast_download

from . import LOGS, get_help, get_string, udB, ultroid_bot, ultroid_cmd

__doc__ = get_help("help_autopic")


async def get_google_images(query: str):
    """Extract image URLs from Google Images search results with fallbacks"""
    
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    
    search_url = f"https://www.google.com/search?q={query}&tbm=isch"
    
    # Domains to exclude
    excluded_domains = [
        'gstatic.com',
        'google.com',
        'googleusercontent.com',
        'ssl.google.com'
    ]
    
    def is_valid_url(url):
        return not any(domain in url.lower() for domain in excluded_domains)
    
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(search_url, headers=headers) as response:
                html = await response.text()

        # Try to extract from search results div first
        img_urls = []
        search_pattern = r'<div id="search".*?>(.*?)</div>'
        search_match = re.search(search_pattern, html, re.DOTALL)
        if search_match:
            search_content = search_match.group(1)
            url_pattern = r'https://[^\"]*?\.(?:jpg|jpeg|png|webp)'
            url_matches = re.finditer(url_pattern, search_content, re.IGNORECASE)
            for url_match in url_matches:
                url = url_match.group(0)
                if url not in img_urls and is_valid_url(url):
                    img_urls.append(url)

        # Fallback to tdeeNb div if no results
        if not img_urls:
            pattern = r'<div jsname="tdeeNb"[^>]*>(.*?)</div>'
            matches = re.finditer(pattern, html, re.DOTALL)
            for match in matches:
                div_content = match.group(1)
                url_pattern = r'https://[^\"]*?\.(?:jpg|jpeg|png|webp)'
                url_matches = re.finditer(url_pattern, div_content, re.IGNORECASE)
                for url_match in url_matches:
                    url = url_match.group(0)
                    if url not in img_urls and is_valid_url(url):
                        img_urls.append(url)

        # Fallback to general image search if still no results
        if not img_urls:
            pattern = r"https://[^\"]*?\.(?:jpg|jpeg|png|webp)"
            matches = re.finditer(pattern, html, re.IGNORECASE)
            for match in matches:
                url = match.group(0)
                if url not in img_urls and is_valid_url(url):
                    img_urls.append(url)

        # Final fallback to data URLs if still no results
        if not img_urls:
            pattern = r'data:image/(?:jpeg|png|webp);base64,[^\"]*'
            matches = re.finditer(pattern, html, re.IGNORECASE)
            for match in matches:
                url = match.group(0)
                if url not in img_urls:
                    img_urls.append(url)

        return img_urls
                
    except Exception as e:
        print(f"Error fetching Google images: {e}")
        return []


@ultroid_cmd(pattern="autopic( (.*)|$)")
async def autopic(e):
    search = e.pattern_match.group(1).strip()
    if udB.get_key("AUTOPIC") and not search:
        udB.del_key("AUTOPIC")
        return await e.eor(get_string("autopic_5"))
    if not search:
        return await e.eor(get_string("autopic_1"), time=5)
    e = await e.eor(get_string("com_1"))
    images = await get_google_images(search)
    if not images:
        return await e.eor(get_string("autopic_2").format(search), time=5)
    await e.eor(get_string("autopic_3").format(search))
    udB.set_key("AUTOPIC", search)
    SLEEP_TIME = udB.get_key("SLEEP_TIME") or 1221
    while True:
        for lie in images:
            if udB.get_key("AUTOPIC") != search:
                return
            download_path, stime = await fast_download(lie, "resources/downloads/autopic.jpg")
            img = Image.open(download_path)
            img.save("resources/downloads/autopic.jpg")
            file = await e.client.upload_file("resources/downloads/autopic.jpg")
            await e.client(UploadProfilePhotoRequest(file=file))
            os.remove("resources/downloads/autopic.jpg")
            await asyncio.sleep(SLEEP_TIME)

        shuffle(images)


if search := udB.get_key("AUTOPIC"):
    images = {}
    sleep = udB.get_key("SLEEP_TIME") or 1221

    async def autopic_func():
        search = udB.get_key("AUTOPIC")
        if images.get(search) is None:
            images[search] = await get_google_images(search)
        if not images.get(search):
            return
        img = random.choice(images[search])
        filee, stime = await fast_download(img, "resources/downloads/autopic.jpg")
        img = Image.open(filee)
        img.save("resources/downloads/autopic.jpg")
        file = await ultroid_bot.upload_file("resources/downloads/autopic.jpg")
        await ultroid_bot(UploadProfilePhotoRequest(file=file))
        os.remove(filee)

    try:
        from apscheduler.schedulers.asyncio import AsyncIOScheduler

        schedule = AsyncIOScheduler()
        schedule.add_job(autopic_func, "interval", seconds=sleep)
        schedule.start()
    except ModuleNotFoundError as er:
        LOGS.error(f"autopic: '{er.name}' not installed.")