1
+ #Copyright © 2024 Damantha Jasinghe
2
+ #For the repository at https://github.com/Damantha126/PySDbots
3
+
4
+ import sys
1
5
import requests
6
+ from datetime import date
7
+ from .__version__ import __version__
8
+
9
+ # HTTP request headers
10
+ headers = {
11
+ 'Content-Type' : 'application/json' ,
12
+ 'User-Agent' : 'pysdbots'
13
+ }
14
+
15
+ # URL for fetching configurations
16
+ CONFIGS = "https://gist.githubusercontent.com/Damantha126/b18e14d0d04f25327773e0ab54cc090a/raw/config.json"
17
+ # Fetch configurations from the URL
18
+ req = requests .get (CONFIGS ).json ()
19
+
20
+ # Main class for interacting with SD API
21
+ class SDAPI :
22
+ notice_displayed = False
23
+ """Main class for interacting with SD API."""
24
+ def __init__ (self ) -> None :
25
+ # Initialize notice_displayed flag
26
+ self .notice_displayed = False
27
+ # Base URL and version information
28
+ self .BASE = req ["BASE_URL" ]
29
+ self .VERSION = __version__
30
+ self .client = requests
31
+ self .api_error = "Failed to retrieve data from the SD API. Please ensure that the API is operational and that your request parameters are correct. If the issue persists, contact support for assistance."
32
+
33
+
34
+ # Display notice only once
35
+ if not self .notice_displayed :
36
+ self ._start ()
37
+
38
+ def _start (self ):
39
+ """Display version information and update notice.
40
+
41
+ Prints information about the current version of PySDBots and displays a notification if a newer version is available.
42
+
43
+ This method checks the latest available version of PySDBots by querying a remote configuration source.
44
+ """
45
+ # Set notice_displayed flag to True
46
+ self .notice_displayed = True
47
+ year = date .today ().year
48
+ # Print version information
49
+ print (
50
+ f'PySDBots v{ __version__ } , Copyright (C) '
51
+ f'2022-{ year } Damantha Jasinghe <https://github.com/Damantha126>\n '
52
+ 'Licensed under the terms of the MIT License, '
53
+ 'Massachusetts Institute of Technology (MIT)\n ' ,
54
+ )
55
+ # Check for newer version and print update notice
56
+ if req ["LAST_VERSION" ] != __version__ :
57
+ text = f'Update Available!\n ' \
58
+ f'New PySDBots v{ req ["LAST_VERSION" ]} ' \
59
+ f'is now available!\n '
60
+ if not sys .platform .startswith ('win' ):
61
+ print (f'\033 [93m{ text } \033 [0m' )
62
+ else :
63
+ print (text )
64
+
65
+ def version (self ):
66
+ return (self .VERSION )
67
+
68
+ def _fetch_data (self , method , url , data = None ):
69
+ """
70
+ Make a request with the specified method.
71
+
72
+ Args:
73
+ method (str): The HTTP method ('GET' or 'POST').
74
+ url (str): The URL to send the request to.
75
+ data (dict, optional): The JSON data to send in the request body (for 'POST' requests).
76
+
77
+ Returns:
78
+ dict: The JSON response from the API.
79
+
80
+ Raises:
81
+ requests.exceptions.RequestException: If the request encounters an error (e.g., network issues, server errors).
82
+ """
83
+ try :
84
+ if method == "GET" :
85
+ # Send a GET request with the specified URL and headers
86
+ response = self .client .get (url )
87
+ elif method == "POST" :
88
+ # Send a POST request with the specified URL, headers, and JSON data
89
+ response = self .client .post (url = url , headers = headers , json = data )
90
+
91
+ # Check for errors in the response (e.g., 4xx or 5xx status codes)
92
+ response .raise_for_status ()
93
+ # Parse the JSON response and return it
94
+ return response .json ()
95
+
96
+ except requests .exceptions .RequestException as e :
97
+ # Re-raise the request exception if an error occurs
98
+ print (f"Error occurred: { e } \n " , self .api_error )
99
+ return None
100
+
101
+ def anime_logo (self , name ):
102
+ """Generate images with anime characters.
103
+
104
+ Args:
105
+ name (str): The name to include in the generated image.
106
+
107
+ Returns:
108
+ str: URL of the generated image hosted on telegra.ph.
109
+
110
+ This method generates an image with anime characters based on the provided name and returns the URL of the generated image hosted on telegra.ph.
111
+ """
112
+ try :
113
+ return self ._fetch_data ("GET" , f"{ self .BASE } /anime-logo?name={ name } " )
114
+ except :
115
+ raise Exception (self .api_error )
116
+
117
+ def tiktok (self , url ):
118
+ """Get TikTok video information from a URL.
119
+
120
+ Args:
121
+ url (str): The URL of the TikTok video.
122
+
123
+ Returns:
124
+ dict: JSON object containing information about the TikTok video.
125
+
126
+ This method retrieves information about a TikTok video from the provided URL and returns it as a JSON object.
127
+ """
128
+ try :
129
+ return self ._fetch_data ("GET" , f"{ self .BASE } /tiktok?url={ url } " )
130
+ except :
131
+ raise Exception (self .api_error )
132
+
133
+ def apod (self ):
134
+ """Retrieve Astronomy Picture of the Day (APOD) from NASA.
135
+
136
+ Returns:
137
+ dict: JSON object with information about the APOD, including date, explanation, image URL, and title.
138
+ """
139
+ try :
140
+ return self ._fetch_data ("GET" , f"{ self .BASE } /apod" )
141
+ except :
142
+ raise Exception (self .api_error )
143
+
144
+ def detect_lang (self , text ):
145
+ """Detect the language of a given text.
146
+
147
+ Args:
148
+ text (str): The text to analyze for language detection.
149
+
150
+ Returns:
151
+ dict: JSON object with the detected language.
152
+ """
153
+ try :
154
+ return self ._fetch_data ("GET" , f"{ self .BASE } /detect?text={ text } " )
155
+ except :
156
+ raise Exception (self .api_error )
157
+
158
+ def write (self , text ):
159
+ """Generate a note from the provided text.
160
+
161
+ Args:
162
+ text (str): The text content of the note.
163
+
164
+ Returns:
165
+ str: URL of the generated note.
166
+ """
167
+ try :
168
+ body = {"text" : f"{ text } " }
169
+ return self ._fetch_data ("POST" , f"{ self .BASE } /write" , data = body )
170
+ except :
171
+ raise Exception (self .api_error )
172
+
173
+ def chk (self , cc ):
174
+ """Check information related to a given credit card.
175
+
176
+ Args:
177
+ cc (str): The credit card to check.
2
178
179
+ Returns:
180
+ dict: JSON object with information related to the credit card.
181
+ """
182
+ try :
183
+ return self ._fetch_data ("GET" , f"{ self .BASE } /chk?cc={ cc } " )
184
+ except :
185
+ raise Exception (self .api_error )
3
186
4
- headers = {'Content-Type' : 'application/json' }
187
+ def sk_checker (self , key ):
188
+ """Check information related to a Stripe API Keys.
5
189
190
+ Args:
191
+ key (str): The sk key to check.
6
192
7
- def anime_logo (name ):
8
- API = f"https://api.sdbots.tech/anime-logo?name={ name } "
9
- req = requests .get (API ).url
10
- return (req )
193
+ Returns:
194
+ dict: JSON object with information related to the Stripe API Keys.
195
+ """
196
+ try :
197
+ return self ._fetch_data ("GET" , f"{ self .BASE } /sk?key={ key } " )
198
+ except :
199
+ raise Exception (self .api_error )
11
200
12
- def tiktok (url ):
13
- API = f"https://api.sdbots.tech/tiktok?url={ url } "
14
- req = requests .get (API ).json ()
15
- return (req )
201
+ def lyrics (self , song ):
202
+ """Retrieve lyrics for a given song.
16
203
17
- def apod ():
18
- API = "https://api.sdbots.tech/apod"
19
- req = requests .get (API ).json ()
20
- return (req )
204
+ Args:
205
+ song (str): The name of the song.
21
206
22
- def detect_lang (text ):
23
- req = requests .get (f"https://api.sdbots.tech/detect?text={ text } " ).json ()
24
- return (req )
207
+ Returns:
208
+ dict: JSON object with the lyrics of the song.
209
+ """
210
+ try :
211
+ return self ._fetch_data ("GET" , f"{ self .BASE } /lyrics?song={ song } " )
212
+ except :
213
+ raise Exception (self .api_error )
25
214
26
- def write (text ):
27
- body = {
28
- "text" : f"{ text } "
29
- }
30
- url = "https://api.sdbots.tech/write"
31
- req = requests .post (url = url , headers = headers , json = body ).url
32
- return (req )
215
+ def ipinfo (self , ip ):
216
+ """Retrieve information about a given IP address.
33
217
34
- def chk (cc ):
35
- API = f"https://api.sdbots.tech/chk?cc={ cc } "
36
- req = requests .get (API ).json ()
37
- return (req )
218
+ Args:
219
+ ip (str): The IP address to retrieve information for.
38
220
39
- def sk_checker (key ):
40
- req = requests .get (f"https://api.sdbots.tech/sk?key={ key } " ).json ()
41
- return (req )
221
+ Returns:
222
+ dict: JSON object with information about the IP address.
223
+ """
224
+ try :
225
+ return self ._fetch_data ("GET" , f"{ self .BASE } /ipinfo?ip={ ip } " )
226
+ except :
227
+ raise Exception (self .api_error )
42
228
43
- def lyrics (song ):
44
- req = requests .get (f"https://api.sdbots.tech/lyrics?song={ song } " ).json ()
45
- return (req )
229
+ def hirunews (self ):
230
+ """Retrieve news from Hiru News.
46
231
47
- def ipinfo (ip ):
48
- req = requests .get (f"https://api.sdbots.tech/ipinfo?ip={ ip } " ).json ()
49
- return (req )
232
+ Returns:
233
+ dict: JSON object with news information.
234
+ """
235
+ try :
236
+ return self ._fetch_data ("GET" , f"{ self .BASE } /hirunews" )
237
+ except :
238
+ raise Exception (self .api_error )
50
239
51
- def hirunews ():
52
- req = requests .get ("https://api.sdbots.tech/hirunews" ).json ()
53
- return (req )
240
+ def logohq (self , text ):
241
+ """Generate a high-quality logo from the provided text.
54
242
55
- def logohq (text ):
56
- req = requests .get (f"https://api.sdbots.tech/logohq?text={ text } " ).url
57
- return (req )
243
+ Args:
244
+ text (str): The text content of the logo.
58
245
59
- def fakeinfo ():
60
- req = requests .get ("https://api.sdbots.tech/fakeinfo" ).json ()
61
- return (req )
246
+ Returns:
247
+ str: URL of the generated logo.
248
+ """
249
+ try :
250
+ return self ._fetch_data ("GET" , f"{ self .BASE } /logohq?text={ text } " )
251
+ except :
252
+ raise Exception (self .api_error )
62
253
254
+ def fakeinfo (self ):
255
+ """Retrieve fake information.
63
256
64
- print ("SD Bots API -> https://docs.sdbots.tech" )
257
+ Returns:
258
+ dict: JSON object with fake information.
259
+ """
260
+ try :
261
+ return self ._fetch_data ("GET" , f"{ self .BASE } /fakeinfo" )
262
+ except :
263
+ raise Exception (self .api_error )
264
+
0 commit comments