Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ChatBot-ALL-Project/lookups_all.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
56 lines (49 sloc)
2.59 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code written by Nathan starts here (weather_Lookup function modified by Lukas) | |
from weather import Weather | |
""" Weather-api is a library used to retrieve weather data from yahoo weather api and convert it to a dictionary. | |
Version: 0.0.3 (Must be this version otherwise doesn't work) Source: https://pypi.python.org/pypi/weather-api""" | |
from google import search | |
""" Google is a library used to scrape results from google searches. | |
Source : https://github.com/MarioVilas/google""" | |
import requests | |
"""library for opening URLs""" | |
from bs4 import BeautifulSoup | |
""" BeautifulSoup is a library used for parsing webpages to html, and obtaining data through html format | |
Source: https://pypi.python.org/pypi/beautifulsoup4""" | |
def searchResults(query): # Needs to return a string of results instead of a generator object | |
"""takes an input query, and returns number of websites and descriptions""" | |
search_results = "" | |
i=0 | |
for url in search(query, tld='com', lang='en', num = 1, stop=3, pause = 3.0): | |
#for each URL found in google search | |
response = requests.get(url) | |
soup = BeautifulSoup(response.text, "html.parser") | |
metas = soup.find_all('meta') | |
metasList = list(meta.attrs['content'] for meta in metas if 'name' in meta.attrs and meta.attrs['name'] == 'description') | |
#retrieve description of URL | |
#line 15-16 reference: https://stackoverflow.com/questions/38009787/how-to-extract-meta-description-from-urls-using-python, date: June 24 2016 | |
desc = ''.join(metasList) | |
#convert list to string | |
i+=1 | |
url_and_desc = "\n" + str(i) + ") " + url + "\n" + desc + "\n" | |
search_results += url_and_desc | |
return search_results | |
def weather_Lookup(area): # A problem with temp, some cities return the wrong temperature | |
"""takes an input if an area, outputs weather conditions based on location and time""" | |
# Returns dictionary in the following format: | |
# 'date': 'Sun, 19 Nov 2017 10:00 PM GMT', | |
# 'code': '26', | |
# 'text': 'Cloudy', | |
# 'temp': '43' | |
area.lower() | |
#for continuity | |
weather = Weather() | |
try: | |
location = weather.lookup_by_location(area) | |
condition = location.condition() | |
answer = "The date is: " + condition['date'] + ", the sky is " + condition['text'] + " and the temperature is " + str(int(round((int(condition['temp'])-32)*5/9, 0))) + " degrees celsius." | |
return answer | |
except AttributeError: | |
#if location does not exist | |
return "Location: ",area," does not exist; do you think I'm dumb?" | |
# Code written by Nathan ends here. |