File size: 1,443 Bytes
96820f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import discord
from discord import app_commands
import random

@client.tree.command(name="amir", description="amir")
async def tiktok(interaction: discord.Interaction, keyword: str):
    await interaction.response.defer()

    conn = http.client.HTTPSConnection("tiktok-scraper7.p.rapidapi.com")

    headers = {
        'x-rapidapi-key': "2801f98bdcmshf49ad6c013c8db8p11a5e9jsn87d5139bc47a",
        'x-rapidapi-host': "tiktok-scraper7.p.rapidapi.com"
    }

    conn.request("GET", f"/feed/search?keywords={keyword}&region=us&count=10&cursor=0&publish_time=0&sort_type=0", headers=headers)

    res = conn.getresponse()
    data = res.read()

    try:
        json_data = json.loads(data.decode("utf-8"))
        videos = json_data.get("data", {}).get("videos", [])

        if videos:
            random_video = random.choice(videos)
            video_url = random_video.get("play_url", "?")
            description = random_video.get("title", "?")

            embed = discord.Embed(title=f" Search: {keyword}", description=description, color=0xFF0000)
            embed.add_field(name="url", value=video_url)
            
            await interaction.followup.send(embed=embed)
        else:
            await interaction.followup.send(f"none: {keyword}")
    except json.JSONDecodeError:
        await interaction.followup.send("errer.")
    except Exception as e:
        await interaction.followup.send(f"An error occurred: {str(e)}")