coollsd commited on
Commit
96820f4
·
verified ·
1 Parent(s): f760710

Create amir.py

Browse files
Files changed (1) hide show
  1. amir.py +39 -0
amir.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import discord
2
+ from discord import app_commands
3
+ import random
4
+
5
+ @client.tree.command(name="amir", description="amir")
6
+ async def tiktok(interaction: discord.Interaction, keyword: str):
7
+ await interaction.response.defer()
8
+
9
+ conn = http.client.HTTPSConnection("tiktok-scraper7.p.rapidapi.com")
10
+
11
+ headers = {
12
+ 'x-rapidapi-key': "2801f98bdcmshf49ad6c013c8db8p11a5e9jsn87d5139bc47a",
13
+ 'x-rapidapi-host': "tiktok-scraper7.p.rapidapi.com"
14
+ }
15
+
16
+ conn.request("GET", f"/feed/search?keywords={keyword}&region=us&count=10&cursor=0&publish_time=0&sort_type=0", headers=headers)
17
+
18
+ res = conn.getresponse()
19
+ data = res.read()
20
+
21
+ try:
22
+ json_data = json.loads(data.decode("utf-8"))
23
+ videos = json_data.get("data", {}).get("videos", [])
24
+
25
+ if videos:
26
+ random_video = random.choice(videos)
27
+ video_url = random_video.get("play_url", "?")
28
+ description = random_video.get("title", "?")
29
+
30
+ embed = discord.Embed(title=f" Search: {keyword}", description=description, color=0xFF0000)
31
+ embed.add_field(name="url", value=video_url)
32
+
33
+ await interaction.followup.send(embed=embed)
34
+ else:
35
+ await interaction.followup.send(f"none: {keyword}")
36
+ except json.JSONDecodeError:
37
+ await interaction.followup.send("errer.")
38
+ except Exception as e:
39
+ await interaction.followup.send(f"An error occurred: {str(e)}")