def get_most_popular_stories(type, period): url2 = f'https://api.nytimes.com/svc/mostpopular/v2/{type}/{period}.json?api-key=' + api_key response_most_pop = requests.get(url2).json() main_functions.save_to_file(response_most_pop, 'JSON_files/most_pop.json') my_pop_articles = main_functions.read_from_file('JSON_files/most_pop.json') return my_pop_articles #print(get_top_stories("arts")) #to test the functions
def api_call(api_type, type, time_period): if (api_type == 'stories'): url = 'https://api.nytimes.com/svc/topstories/v2/'+ type + '.json?api-key=' + api_key response = requests.get(url).json() main_functions.save_to_file(response, "JSON_Files/response.json") elif (api_type == 'articles'): url = 'https://api.nytimes.com/svc/mostpopular/v2/' + type + '/' + time_period + '.json?api-key=' + api_key response = requests.get(url).json() main_functions.save_to_file(response, "JSON_Files/article_response.json")
def url_name(topic): url = "https://api.nytimes.com/svc/topstories/v2/{0}.json?api-key=".format( topic) url = url + api_key response = requests.get(url).json() main_functions.save_to_file(response, "JSON_FILES/response.json") my_articles = main_functions.read_from_file("JSON_Files/response.json") str1 = "" for i in my_articles["results"]: str1 = str1 + i["abstract"] return str1
def mostPop(option1, option2): url = "https://api.nytimes.com/svc/mostpopular/v2/{0}/{1}.json?api-key=".format( option1, option2) url = url + api_key response = requests.get(url).json() main_functions.save_to_file(response, "JSON_FILES/response.json") popArticles = main_functions.read_from_file("JSON_Files/response.json") str1 = "" for i in popArticles["results"]: str1 = str1 + i["abstract"] return str1
def currency_converter(cost, currency): float_cost = float(cost) url = "http://api.currencylayer.com/live?access_key=37a47b066cccdff362e4e654150c43fd" response = requests.get(url).json() main_functions.save_to_file(response, "JSON_Documents/response.json") currencies = main_functions.read_from_file("JSON_Documents/response.json") if currency != 'USD': float_cost = float(cost) / float( currencies["quotes"]["USD" + currency]) converted_cost = "{:.2f}".format(float_cost) return converted_cost
def partA(): response2 = requests.get(new_url).json() main_functions.save_to_file(response2, "JSON_Files/response.json") if user_name and options: st.subheader("II - Frequency Distribution") agree = st.checkbox("Click here to generate the frequency distribution") if agree: st.plotly_chart(fig) st.subheader("III - Wordcloud") agree = st.checkbox("Click here to generate wordcloud") if agree: st.image(wordcloud.to_array()) st.write("Wordcloud generated for " + options + " topic")
class Expenses(FlaskForm): list_URL = "http://api.currencylayer.com/list?access_key=7edb40d73b258f5cf730f823f226aa7e" response = requests.get(list_URL).json() # retrieve the list of names and save it in a json file currency_list_names_file_name = "JSON_Files\\currency_list_names.json" main_functions.save_to_file(response, currency_list_names_file_name) # read the json file and extract all the names in an array of tuples currency_names = main_functions.read_from_file( currency_list_names_file_name) name_list = [] for x in currency_names[ "currencies"]: # range(len(currency_names['currencies'])): name_list.append((x, currency_names['currencies'][x])) # instantiate the required input fields and add the current parameters for the genre options currency = SelectField(choices=[(x, y) for x, y in name_list]) categories = [ 'Food & Drinks', 'Health', 'Travel', 'Entertainment', 'Shopping', 'Services' ] description = StringField('Description') category = SelectField('Categories', choices=categories) cost = DecimalField('Cost ($)') date = DateField('Date', format='%m/%d/%Y')
def currency_converter(cost, currency): url = "http://api.currencylayer.com/live?" my_key_dict = main_functions.read_from_file("JSON_Files/api_keys.json") my_key = "access_key=" + my_key_dict["my_currency_key"] final_url = url + my_key get_api = requests.get(final_url).json() main_functions.save_to_file(get_api, "JSON_Files/response.json") api_response_dic = main_functions.read_from_file( "JSON_Files/response.json") if currency == "USD": converted_cost = float(cost) else: countries = api_response_dic['quotes'] quote = countries[currency] converted_cost = float(cost) / float(quote) return round(converted_cost, 2)
def currency_converter(cost, currency): url = "http://api.currencylayer.com/live?access_key=7edb40d73b258f5cf730f823f226aa7e" response = requests.get(url).json() # create the key for the exchange rate conversion_to_find = "USD" + currency # save the response to a json file response_file_name = "JSON_Files\\currency_list_rates.json" main_functions.save_to_file(response, response_file_name) # read the json file and extract all the names in an array of tuples currency_rates = main_functions.read_from_file(response_file_name) # extract the relevant exchange rate rate = currency_rates['quotes'][conversion_to_find] # exchange formula converted_cost = cost / Decimal(str(rate)) return converted_cost
def get_json(request_url): request_response = requests.get(request_url).json() main_functions.save_to_file(request_response, "JSON_Files/response.json")
def get_top_stories(option): url = f'https://api.nytimes.com/svc/topstories/v2/{option}.json?api-key=' + api_key response = requests.get(url).json() main_functions.save_to_file(response, 'JSON_files/response.json') my_articles = main_functions.read_from_file('JSON_files/response.json') return my_articles
def partB(): response = requests.get(com_shared_url).json() main_functions.save_to_file(response, "JSON_Files/response_two.json") st.image(wordcloud_two.to_array())
def save_response(url, fileName): #st.write("Entering saveResponse function...") response = requests.get(url).json() main_functions.save_to_file(response, "JSON_documents/" + fileName)
import main_functions import requests url = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosty/photos?sol=1000&api_key=" nasa_api_dict = main_functions.read_from_file("nasa_api.json") my_api_key = nasa_api_dict["nasa_api.json"] url2 = url + my_api_key mars_pics = requests.get(url2).json main_functions.save_to_file(mars_pics, "mars_pics.json") mars_pics = main_functions.read_from_file("mars_pics.json") print(mars_pics.keys()) print(type(mars_pics["photos"])) print(mars_pics["photos"][0].keys()) print(len(mars_pics["photos"])) print(type(mars_pics["photos"][0]["camera"])) print(mars_pics["photos"][0]["camera"].keys()) for i in mars_pics["photos"]: print(i["camera"]["full_name"])
'orbituaries', 'opinion', 'politics', 'realestate', 'science', 'sports', 'sundayreview', 'technology', 'theater', 't-magazine' , 'travel', 'upshot', 'us', 'world') ) stopwords = stopwords.words("english") # ensures the checkboxes only appear if the topic is selected to avoid a bad url error if interest != "": # complete the url using the chosen topic topStoriesURL = "https://api.nytimes.com/svc/topstories/v2/" + interest + ".json?api-key=" + api_key # request the result of the url response = requests.get(topStoriesURL).json() # store the result in a json file and read the file contents main_functions.save_to_file(response, "JSON_Files/topStories.json") topStoriesOutput = main_functions.read_from_file("JSON_Files/topStories.json") # the following block of code cleans up the list variable so only desirable words are left toProcess = "" for i in topStoriesOutput["results"]: toProcess = toProcess + i["abstract"] words = word_tokenize(toProcess) fdist = FreqDist(words) words_no_punc = [] for w in words: if w.isalpha(): words_no_punc.append(w.lower())
params = { 'q': '', 'location': 'United States', 'search_engine': 'google.com', 'gl': 'US', 'hl': 'en', 'num': '10' } #Asks user for beauty product productQ = input("What beauty product will you like to look for? : ") print('Your product is', productQ) params['q'] = productQ #This will use request lib to make a custom url request. #params will add "?" before first parameter "q". #Output will be handled by .json function so that we may save it using main_functions/ response = requests.get('https://app.zenserp.com/api/v2/search', headers=headers, params=params).json() #if statement will check to see if the file name has not been created. #If the file exists it will be named after the past index location of 'i'. i = 0 if os.path.exists("results_json%s.json" % i): i += 1 fileName = "results_json%s.json" % i main_functions.save_to_file(response, fileName) else: fileName = "results_json%s.json" % i main_functions.save_to_file(response, fileName)
option = st.selectbox( 'Select a topic', ['', 'arts', 'automobiles', 'books', 'business', 'fashion', 'food', 'health', 'home', 'insider', 'magazine', 'movies', 'nyregion', 'obituaries', 'opinion', 'politics', 'realestate', 'science', 'sports', 'sundayreview', 'technology', 'theater', 't-magazine', 'travel', 'upshot', 'us', 'world'] ) if option != '': st.write("Hi", name, "you selected the", option, "topic.") st.header('II - Frequency Distribution') url = "https://api.nytimes.com/svc/topstories/v2/" + option + ".json?api-key=" + api_key response = requests.get(url).json() main_functions.save_to_file(response, "JSON_Files/response.json") my_articles = main_functions.read_from_file("JSON_Files/response.json") str1 = "" for i in my_articles["results"]: str1 = str1 + i["abstract"] words = word_tokenize(str1) fdist = FreqDist(words) words_no_punc = [] for w in words:
option = st.selectbox("Please select your favorite topic", [ "arts", "automobiles", "books", "business", "fashion", "food", "health", "home", "insider", "magazine", "movies", "nyregion", "obituaries", "opinion", "politics", "realestate", "science", "sports", "sundayreview", "technology", "theater", "t-magazine", "travel", "upshot", "us", "world" ]) name.capitalize() + ', you selected :', option + '.' api_key_dict = main_functions.read_from_file("JSON_Files/api_key.json") api_key = api_key_dict["my_key"] url = "https://api.nytimes.com/svc/topstories/v2/" + option + ".json?api-key=" + api_key response = requests.get(url).json() main_functions.save_to_file(response, "JSON_Files/response.json") my_articles = main_functions.read_from_file("JSON_Files/response.json") str1 = "" for i in my_articles["results"]: str1 = str1 + i["abstract"] sentences = sent_tokenize(str1) words = word_tokenize(str1) fdist = FreqDist(words) words_no_punc = [] for w in words: if w.isalpha():
user_name = st.text_input("Please enter your name:") option = st.selectbox("Please select a topic:", [ "", "arts", "automobiles", "books", "business", "fashion", "food", "health", "home", "insider", "magazine", "movies", "nyregion", "obituaries", "opinion", "politics", "realestate", "science", "sports", "sundayreview", "technology", "theater", "t-magazine", "travel", "upshot", "us", "world" ]) api_key_dict = main_functions.read_from_file("JSON_Files/api_key.json") api_key = api_key_dict['my_key'] url = "https://api.nytimes.com/svc/topstories/v2/" + option + ".json?api-key=" + api_key top_stories = requests.get(url).json() main_functions.save_to_file(top_stories, "JSON_Files/top_stories.json") top_stories_articles = main_functions.read_from_file( "JSON_Files/top_stories.json") if user_name and (option != ""): st.write("Hi " + user_name + "! Your current selection is " + option + ".") st.subheader("II - Frequency Distribution") str1 = "" if st.checkbox("Click here to generate frequency distribution"): if option != "": for i in top_stories_articles["results"]: str1 = str1 + i["abstract"] words = word_tokenize(str1) words_no_punc = []
import main_functions import requests print("Exercise 1 - How many humans are in space right now?") url1 = "http://api.open-notify.org/astros.json" astronauts_json = requests.get(url1).json() main_functions.save_to_file(astronauts_json, "astros.json")
full_name = st.text_input("Enter name") topics = st.selectbox("Pick a topic.", [ "", "arts", "automobiles", "books", "business", "fashion", "food", "health", "home", "insider", "magazine", "movies", "ny region", "obituaries", "opinion", "politics", "real estate", "science", "sports", "sunday review", "technology", "theater", "t-magazine", "travel", "upshot", "us", "world" ]) if full_name and topics: api_key_dict = main_functions.read_from_file("json_files/api_key.json") api_key = api_key_dict["my_key"] url = "https://api.nytimes.com/svc/topstories/v2/" + topics + ".json?api-key=" + api_key response = requests.get(url).json() main_functions.save_to_file(response, "json_files/response.json") results = main_functions.read_from_file("json_files/response.json") firststring = "" for i in results["results"]: firststring = firststring + i["abstract"] words = word_tokenize(firststring) no_punc = [] for w in words: if w.isalpha(): no_punc.append(w.lower()) fdist2 = FreqDist(no_punc) stopword = stopwords.words('english')
st.set_option('deprecation.showPyplotGlobalUse', False) # Verify that a name was inserted and a topic was selected if len(name) > 0 and len(topics) > 0: # Display message showing user's name and the topic selected st.write("Hi {}, you selected the {} topic.".format(name, topics)) # Extract information from API using the topic selected and the API_key url = "https://api.nytimes.com/svc/topstories/v2/" + topics + ".json?api-key=" + my_api_key # Make the request response = requests.get(url).json() # Save information to my_response.json file main_functions.save_to_file(response, "JSON_Files/top_stories.json") # Get information from my_response.json my_articles = main_functions.read_from_file("JSON_Files/top_stories.json") # Save the extracted information str1 = "" for i in my_articles["results"]: str1 = str1 + i["abstract"] # Get words from the text words = word_tokenize(str1) # Get rid of the punctuation marks words_no_punctuation = []
current_stock = float(stocks["stocks"][counter]["change"]) for element in bottom_5: changes.append(element["change"]) for element in bottom_5: if element["change"] == max( changes) and current_stock < element["change"]: element["change"] = current_stock element["name"] = stocks["stocks"][counter]["name"] break else: bottom_5.append({ "name": stocks["stocks"][counter]["name"], "change": float(stocks["stocks"][counter]["change"]) }) counter += 1 return bottom_5 top = get_top5(full_stock_info) bottom = get_bottom5(full_stock_info) print("Top stocks: ", top) print("Bottom stocks: ", bottom) full_stock_info["top"] = top full_stock_info["bottom"] = bottom main_functions.save_to_file(full_stock_info, "stock_data.json")