botbotbotbot / amir.py
coollsd's picture
Create amir.py
96820f4 verified
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)}")