Amazing-Python-Scripts
431 строка · 15.9 Кб
1# 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 WELCOME TO SPACE NEWS AI CHAT MODEL 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
2
3from IPython import display
4from PIL import Image
5from playsound import playsound
6from gtts import gTTS
7import matplotlib.pyplot as plt
8import urllib.request
9import random
10import io
11import os
12import requests
13import time
14import nasapy
15print('🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n')
16
17# Enter the API key of NASA
18Api_Key = input("Enter the NASA API Key: ")
19
20# Importing modules
21
22# Function which handles the news data of space
23
24
25def spaceNews():
26print("\nWhat day would you like to know ?")
27
28# Date must be between 16 Jun, 1995 and till now
29Date = input("Enter date {yyyy-mm-dd}: ")
30
31# Fetching data from the nasa
32print("\nExtracting Data......")
33print('\nPlease wait.....')
34Url = "https://api.nasa.gov/planetary/apod?api_key=" + str(Api_Key)
35Params = {'date': str(Date)}
36r = requests.get(Url, params=Params)
37
38# Parsing the fetched data for displaying
39Data = r.json()
40
41# Parsing the extracted data
42Info = Data['explanation']
43Title = Data['title']
44Image_Url = Data['url']
45
46# Requesting for the image url to fetch and store into an array
47Image_r = requests.get(Image_Url)
48FileName = str(Date) + '.jpg'
49# Saving the images
50with open(FileName, 'wb') as f:
51f.write(Image_r.content)
52img = Image.open(FileName)
53
54# Clear the terminal for the next operation
55os.system('cls' if os.name == 'nt' else 'clear')
56print('\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n')
57
58# Displaying the fetched data
59img.show()
60print(f"\nTitle of this news - {Title}")
61print(f"\nBrief Description \n\n{Info}")
62print(f"\nFileName - {FileName}\n")
63
64# Function which fetch the mars images of different dates
65
66
67def MarsImage():
68date = input("\nEnter date {yyyy-mm-dd}: ")
69url = f"https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?earth_date={date}&api_key={Api_Key}"
70
71# Fetching data from the internet
72r = requests.get(url)
73Data = r.json()
74
75# Extracting data from the fetched data set
76Photos = Data['photos'][0:9]
77
78if Data['photos'] == []:
79print("\nSorry, No data found!\n")
80else:
81for index, photo in enumerate(Photos):
82# Creating a lists of different values
83camera = photo['camera']
84full_camera_name = camera['full_name']
85date_of_photo = photo['earth_date']
86img_url = photo['img_src']
87
88# Downloading images from the internet
89p = requests.get(img_url)
90img = f'{index}.jpg'
91with open(img, 'wb') as file:
92file.write(p.content)
93os.startfile(img)
94
95print(
96f'Image "{index}.jpg" was captured with {full_camera_name} on {date_of_photo}\n')
97time.sleep(1)
98
99# Function whixh tells about the number of asteroids in a range of dates
100
101
102def Astro():
103# Getting two dates from the user for calculation of number of asteroids in between this range
104print("\nDifference between start and last dates is not more than 7 days.")
105print("\nNow, What's the start date ?")
106start_date = input("Enter date {yyyy-mm-dd}: ") # Start date
107print("\nAnd the last date ?")
108end_date = input("Enter date {yyyy-mm-dd}: ") # Last date
109
110url = f"https://api.nasa.gov/neo/rest/v1/feed?start_date={start_date}&end_date={end_date}&api_key={Api_Key}"
111
112# Fetching data from the internet
113r = requests.get(url)
114Data = r.json()
115
116# Clear the terminal for the next operation
117os.system('cls' if os.name == 'nt' else 'clear')
118print('\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n')
119
120# Extracting data from the fetched data set
121Total_Astro = Data['element_count']
122print(
123f"\nTotal no. of asteroid falling on The Earth between {start_date} and {end_date} is : {Total_Astro} Asteroids\n")
124
125# enumarating the objects one by one
126neo = Data['near_earth_objects']
127
128for body in neo[start_date]:
129# parsing the object to extract 3 different values
130id = body['id']
131name = body['name']
132absolute = body['absolute_magnitude_h']
133
134# Finally printing the extracted data in a readable format
135print(id, name, absolute)
136
137print("\nID & Name of those asteroids are listed above.\n")
138
139# Function which gives information about different solar bodies
140
141
142def SolarBodies():
143print("\nExtracting data.....\n")
144print("\nPlease wait.....\n")
145
146# Link of the website to fetch data of different bodies of the solar system
147url = "https://api.le-systeme-solaire.net/rest/bodies/"
148
149# Fetching data from the internet
150r = requests.get(url)
151Data = r.json() # Save the fetched data after converting into json object
152
153# Clear the terminal for the next operation
154os.system('cls' if os.name == 'nt' else 'clear')
155print('\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n')
156
157# Listing the data / parsing the data set
158bodies = Data['bodies']
159Number = len(bodies)
160print(f"\nNumber of bodies in solar system : {Number}")
161
162try:
163# Getting input from the user
164Body = input(
165"\nTell me the name of the solar bodies you want to know - ")
166while (Body == None):
167Body = input("\nPlease enter the valid input - ")
168Body = Body.replace(" ", "")
169
170print("\nPlease wait......\n")
171
172# Fetching data for that body
173body = Body
174url_2 = f"https://api.le-systeme-solaire.net/rest/bodies/{body}"
175rrr = requests.get(url_2) # Fetchin data from the internet
176data = rrr.json()
177
178# Listing / parsing the dataset
179mass_1 = data['mass']['massValue']
180mass = mass_1*1000 # Converting mass into the trillion tons unit
181volume = data['vol']['volValue']
182density = data['density']
183gravity = data['gravity']
184escape = data['escape']
185radius = data['meanRadius']
186avgTemp = data['avgTemp']
187str = data['bodyType']
188
189# Displaying the fetched data set in a readable / understandable format
190print(
191f"\n------------------------------- Data of {body} -------------------------------------")
192print(f"\n Mass of {body} - {mass} trillion tons")
193print(f"\n Volume of {body} - {volume} trillion kilometer^cube")
194print(
195f"\n Gravity of {body} - {gravity} meter per second^square")
196print(
197f"\n Escape Velocity of {body} - {escape} meter per second")
198print(
199f"\n Density of {body} - {density} gram per centimeter^cube")
200print(f"\n Radius of {body} - {radius} kilometer")
201print(f"\n Average Temperature of {body} - {avgTemp} Kelvin")
202print(f"\n Body Type of {body} - {str}")
203print(f"\n-----------------------------------------------------------------------------------\n")
204except:
205print(f"\nData of {body} is not found!\n")
206
207# Function which fetch the images of different solar bodies
208
209
210def solarBodiesPictures():
211# List of different images for displaying to the user
212# Keys of Google drive images
213imageKeys = [
214'1rktYDEGz0Y95PmN1sJlp0oWchznNBxFY',
215'18p1HDuJzf_0Xa-70pHm7okK_ZtcfnPs3',
216'1uc85CRZD9m_K5N3sZHh97l9laas9y5yd',
217'1zFsOQ3vca-x-UhUK7hzxdAQzuJ0tGIWr',
218'1Ae8zekmbQ7fhFA5ZpCSNVwnuBdDSLtxt',
219'1K6O-xQPz5G35pEtaYdjkRHdpk0ZtOTSd',
220'1SzvHL_E8uA7Hq7D3SDmIAmuaGyzpc4I_',
221'1WUn2Kq0iBnmajNHOggIAwt-nhefhZGOM',
222'16KhTjl3Mw9LZ-3Ed2Mic4E2HCb6KteZW',
223'1taq_Q1JjWKQqrWLB5Fpuhm1Utl8v0wtl',
224'1uySzU1yyDv5aQF9YEFcYQwUcmOlFV-j9',
225'1Sj0CtJ_kHbM03geTs-FmKPj5IBafdfyP',
226'1rgSN5kjGR_iR5w0vqAUDEEZbWctzn_dE',
227'1_zAN7M2pfw0tF5Lss453gjC41HZcxI74',
228'1Pa_x3o4FcYbnGyC2nibMYhg-ZGr-0EtC',
229'1mPlEp4Okgqad001RnOsjvSMyVZiKBFqY',
230'1PmBullQHcZhmP1ook9NKkYVaO9_vI1KH',
231'1dFXMnIePv6SgPtHKJNhllHd6o25IaUFO',
232'1eNJlTy5_MHaFMdH4FSXxwhGXsDriZmTF',
233'1wxokZjLeJKRufBwlzCqnTGVOi9Oj8D5v',
234'1urlYip6h0-su85TdWqa0vaXrOtEptbHD',
235'1j7jWkVihd2VFcr0C-FcKTFqptLu6cbwQ',
236'1IYcfhxtr02mYNAAjt1cpa196LDSBO3H-',
237'1zNQwgjG-_pBsDuvgXyjndA0ld51LQD1i',
238'1HktS1ginfv2QWOjLINa_r5IXJIv-nE_I'
239]
240
241print("Extracting data.....\n")
242# Selecting a random image key
243imageKey = random.choice(imageKeys)
244imageURL = f'https://drive.google.com/uc?export=view&id={imageKey}'
245
246# Fetch the image content
247response = requests.get(imageURL)
248image_content = response.content
249
250# Load the image using PIL
251image = Image.open(io.BytesIO(image_content))
252
253print('\nLoading image.....\n')
254# Display the image
255plt.figure()
256plt.imshow(image)
257plt.axis('off')
258plt.show()
259
260# This function will tells about the current news of space with images
261
262
263def todayNews():
264nasa = nasapy.Nasa(key=Api_Key)
265
266# Get today's date in YYYY-MM-DD format:
267d = input("Enter date {yyyy-mm-dd}: ")
268
269print('\nExtracting data....\n')
270print('\nPlease wait.....\n')
271
272# Get the image data:
273apod = nasa.picture_of_the_day(date=d, hd=True)
274
275# POINT A:
276# Check the media type available:
277if (apod["media_type"] == "image"):
278
279# POINT B:
280# Displaying hd images only:
281if ("hdurl" in apod.keys()):
282
283# POINT C:
284# Saving name for image:
285title = d + "_" + \
286apod["title"].replace(" ", "_").replace(":", "_") + ".jpg"
287
288# POINT D:
289# Path of the directory:
290image_dir = "./"
291
292# Checking if the directory already exists?
293dir_res = os.path.exists(image_dir)
294
295# If it doesn't exist then make a new directory:
296if (dir_res == False):
297os.makedirs(image_dir)
298
299# POINT E:
300# Retrieving the image:
301urllib.request.urlretrieve(
302url=apod["hdurl"], filename=os.path.join(image_dir, title))
303
304# Clear the terminal for the next operation
305os.system('cls' if os.name == 'nt' else 'clear')
306print('\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n')
307
308# POINT F:
309# Displaying information related to image:
310if ("date" in apod.keys()):
311print("Date image released: ", apod["date"])
312print("\n")
313
314if ("copyright" in apod.keys()):
315print("This image is owned by: ", apod["copyright"])
316print("\n")
317
318if ("title" in apod.keys()):
319print("Title of the image: ", apod["title"])
320print("\n")
321
322if ("explanation" in apod.keys()):
323print("Description for the image: ", apod["explanation"])
324print("\n")
325
326if ("hdurl" in apod.keys()):
327print("URL for this image: ", apod["hdurl"])
328print("\n")
329
330# POINT G:
331# Displaying main image:
332img = Image.open(os.path.join(image_dir, title))
333img.show()
334
335# Point H:
336# Text to Speech Conversion:
337# Take input from user:
338choice = input("\nPress * to hear the audio explanation : ")
339if (choice == "*"):
340# Text to be converted:
341mytext = apod["explanation"]
342
343# Creating an object:
344myobj = gTTS(text=mytext, lang="en", slow=False)
345
346# Generating audio file name:
347audio_title = d + "_" + apod["title"] + ".mp3"
348
349# Save the converted file:
350myobj.save(os.path.join(image_dir, audio_title))
351
352# Name of sound file:
353sound_file = os.path.join(image_dir, audio_title)
354
355# Playing the converted file
356display.display(display.Audio(sound_file, autoplay=True))
357
358playsound(f'.\\{audio_title}')
359
360# POINT I:
361# If media type is not image:
362else:
363print("\nSorry, Image not available!\n")
364time.sleep(2)
365
366# Function which keeps holding all the other functions
367
368
369def SpaceNews():
370while True:
371os.system('cls' if os.name == 'nt' else 'clear')
372
373print('''
374🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 WELCOME TO SPACE AI CHAT MODEL 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
375(By Avdhesh Varshney)
376(https://github.com/Avdhesh-Varshney)
377
3781. Today space news
3792. Any other day of space news
3803. Display mars images
3814. Display data of asteroids
3825. About any solar bodies
3836. Pictures of solar bodies
384
3850. To exit the program
386
387🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
388''')
389
390choice = input("Please enter your choice: ")
391while choice.isdigit() == False:
392choice = input("\nPlease enter value in the range [0-6] - ")
393choice = int(choice)
394
395# Clear the terminal for the next operation
396os.system('cls' if os.name == 'nt' else 'clear')
397print('\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n')
398
399if choice == 0:
400print("Thanks for coming !")
401print('\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n')
402time.sleep(2)
403exit()
404
405elif choice == 1:
406todayNews()
407
408elif choice == 2:
409spaceNews()
410
411elif choice == 3:
412MarsImage()
413
414elif choice == 4:
415Astro()
416
417elif choice == 5:
418SolarBodies()
419
420elif choice == 6:
421solarBodiesPictures()
422
423else:
424print("\nYou have entered the wrong choice !\n")
425
426print('\n🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀\n')
427input('\nPress "Enter" key to proceed further...\n')
428
429
430# Program starts
431SpaceNews()
432