Artificial Intelligence (AI) is making waves in today’s tech industry. One of the most interesting applications of AI is web scraping, which involves extracting data from websites. In this article, we will explore how to use Python to build an AI that extracts data from Google, including your IP address, geolocation, and the temperature in your area using the requests.get module. We will also use the AI to download the classic ACDC-Shoot to thrill mp4 file and then use the pyttsx3 library to make the AI speak the results of the code.
Python is a popular programming language for building AI systems because of its flexibility and simplicity. It has a large number of libraries and tools that make it easy to develop complex algorithms and processes. One such library is requests, which is a Python package that makes it easy to send HTTP requests using Python. We will use requests to send a GET request to Google’s website to extract the necessary data.
First, let’s install the necessary libraries. We will need the requests, and pyttsx3 libraries. You can install them using pip, which is the package installer for Python. So, if you haven’t installed this libraries go ahead and open your command prompt (for Windows or Terminal in Linux and MACos) and run the following command:
pip install requests pyttsx3
Once the libraries are installed, let’s write the code to extract data from Google. Here’s the code:
import time
import pyttsx3
import requests
import sys
from datetime import date
from pygame import mixer
from datetime import datetime
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
# Giving order to play ‘Shoot To Thrill – AC DC’
mixer.init()
mixer.music.load(“AC DC – Shoot To Thrill.mp3”)
mixer.music.play()
jarvis = True #Set a variable True so it knows when to stop the song
# Getting time & date
current_time = datetime.now()
today = date.today().strftime(‘%d-%m-%y’)
day_now = datetime.today()
time_now = current_time.strftime(“%H:%M”)
# Here I take my city location from whereever we are located, using my IP adress
# Create a Function that finds my IP, so it can get connected to my location
def my_ip():
ip = requests.get(‘https://get.geojs.io/v1/ip.json’)
ipAdd = ip.json()[‘ip’]
return ipAdd
# Lookingg for my location
# Create a Function that finds my location via my IP adress
# It extract geo data from google’s wheather tab
def current_location():
url = ‘https://get.geojs.io/v1/ip/geo/’ + my_ip() + ‘.json’
geo_req = requests.get(url)
geo_data = geo_req.json()
my_current_location = geo_data.get(‘city’, ‘gr’)
return my_current_location
City = current_location()
# While loop so the song play as long as she is speaking the data
# Searching at Google for the weather forecast at my location
# Extract the usefull data from Google for the weather in my location, I use selenium for that
url_weather = (‘https://google.com/search?q=’ + ‘weather at ‘ + f'{ current_location() }’ + ”.join(sys.argv[1:]))
browser = wd.Chrome()
forecast_page = browser.get(url_weather)
Temp =browser.find_element( By.ID, ‘wob_tm’).text
That is the code to extract the data. I thought that it would be cooler if I could listen to the information I extracted and even better I want to listen to this every time I turn on my PC. So, I had my PC speak my data, while ACDC – Shoot ti thrill plays in the background. The code for this is:
# The While loop speaks the data I’ve collected before
while jarvis == True:
try:
# Start to play AC DC – Shoot To Thrill.mp3, sets the volume and speed of speech
mixer.music.get_busy()
engine = pyttsx3.init()
volume = engine.getProperty(‘volume’)
engine.setProperty(‘volume’, volume+10)
rate = engine.getProperty(‘rate’)
engine.setProperty(‘rate’, rate-20)
# Speak the welcome message and the extracted data
engine.say(‘Welcome bavk sir!’)
time.sleep(2)
engine.say(‘The system will be ready for you in a few moments’)
engine.say(“Sit back while you waiting”)
engine.say(‘Today is,’)
engine.say(today)
engine.say(” the time is”)
engine.say(time_now)
engine.say(f”and the current temperature at {City} is”)
engine.say(Temp)
engine.say(“Degree Celsius”)
engine.runAndWait()
jarvis = False # Variable gets False to exit the loop
except:
jarvis = False
In conclusion I have to say that after a good search I made it work, I also know its a simple AI project and looks a lot like Ironman’s Jarvis AI. That is why I’ve always liked how Tony Stark interact with his AI virtual assistant and this is a start of programming for me.
I hope you liked it, maybe you wanna try it at home, it will defiantly find your location and weather data. Stay thirsty for knowledge and see you at my next article!
Bye!