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 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 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 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 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 generate_data(type, response_type): if (response_type == 'stories'): response = main_functions.read_from_file("JSON_Files/response.json") elif (response_type == 'articles'): response = main_functions.read_from_file("JSON_Files/article_response.json") str1 = "" for i in response["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(): words_no_punc.append(w.lower()) fdist2 = FreqDist(words_no_punc) stopwording = stopwords.words("english") cleanwords = [] for w in words_no_punc: if w not in stopwording: cleanwords.append(w) fdist3 = FreqDist(cleanwords) fdist3.most_common(10) wordcloud = WordCloud().generate(str1) save_image = wordcloud.to_file('IMAGE_Files/cloud.png') save_image data = fdist3.most_common(10) ind = np.arange(len(data)) names, values = zip(*data) if (type == 'graph'): dframe = pd.DataFrame({ 'words': names, 'count': values }) fig = px.line(dframe, x='words', y='count', title='Top 10 Most Common Words') st.plotly_chart(fig) if (type == 'wordcloud'): st.image(Image.open('IMAGE_Files/cloud.png'), caption='Wordcloud', use_column_width = True)
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
st.title("COP 4813 - Project 1") #Part 1 st.write("Part 1 - Top Stories API") name = st.text_input("Please enter your name here") 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)
import json 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() astronauts_filename = "astronauts.json" #main_functions.save_to_file(astronauts_json,astronauts_filename) astronauts_in_space = main_functions.read_from_file(astronauts_filename) print(type(astronauts_in_space)) print(astronauts_in_space.keys()) print("The names of the people in space are: \n") print(type(astronauts_in_space["people"])) for i in astronauts_in_space["people"]: print(i["name"]) for j in sorted(astronauts_in_space['people'], key=lambda x: x["name"]): print(j["name"])
import json import requests import main_functions print("Exercise 3 - Mars Rover Photos API: \n") api_key = main_functions.read_from_file("nasa_api_key.json") my_api_key = api_key["nasa_api_key"] url4 = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=1000&api_key=" url5 = url4 + my_api_key #mars_pics=requests.get(url5).json() #main_functions.save_to_file(mars_pics,"mars_pics.json") mars_pics = main_functions.read_from_file("mars_pics.json") print(type(mars_pics)) print(mars_pics.keys()) print(mars_pics["photos"][0].keys()) #Print how many pictures we have: print(len(mars_pics["photos"])) for i in mars_pics["photos"]: print(i["camera"]["full_name"]) #use FHAZ, RHAZ for j in mars_pics["photos"]:
######################### Imports ################################ import requests import nltk import streamlit as st import main_functions import pandas as pd from nltk import word_tokenize from nltk.probability import FreqDist from nltk.corpus import stopwords from wordcloud import WordCloud import matplotlib.pyplot as plt ######################### Variables ################################ apiKey = main_functions.read_from_file("JSON_Files/api_key.json")["my_key"] urlTopBase = "https://api.nytimes.com/svc/topstories/v2/" urlTopEnd = ".json?api-key=" + apiKey #urlTop = urlTopBase + topic_choice + urlTopEnd urlPopBase = "https://api.nytimes.com/svc/mostpopular/v2/" urlPopEnd = ".json?api-key=" + apiKey #urlPop= urlPopBase + form +"/" + days + urlPopEnd topicsStr = "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" topicsStr = topicsStr.split(",") topics = [""] for t in topicsStr: topics.append(t) ######################### Methods ################################
def get_abstracts(): my_articles = main_functions.read_from_file("JSON_Files/response.json") all_abstracts = "" for a in my_articles["results"]: all_abstracts = all_abstracts + a["abstract"] return all_abstracts
from flask import Flask, render_template, request, url_for from flask_pymongo import PyMongo from flask_wtf import FlaskForm from wtforms import StringField, DecimalField, SelectField, DateField import main_functions # Passes database user without directly revealing it user_info = main_functions.read_from_file("MongoUser.json") username = user_info["username"] password = user_info["password"] # Links to MongoDB database app = Flask(__name__) app.config["SECRET_KEY"] = "KvloanMPIztpjVP8o7cPt-a-wG20Rqe5GvGvskbP" app.config["MONGO_URI"] = "mongodb+srv://{0}:{1}@project3.5h3on.mongodb.net/" \ "db?retryWrites=true&w=majority".format(username, password) mongo = PyMongo(app) # Creates a list of categories category_list = [ "Employment", "Supplies", "Utilities", "Rent", "Internet", "Phone", "Advertising", "Events", "Other" ] # Recreates the category list for use as a flask field category_list_long = [] for i in category_list: category_list_long.append((i, i))
import requests import streamlit as st import pandas as pd import main_functions from wordcloud import WordCloud import matplotlib.pyplot as plt # nltk.download("punkt") # nltk.download("stopwords") #Author: Andy Naranjo COP4813 # Get API Key api_key_dic = main_functions.read_from_file("JSON_Files/api_key.json") api_key = api_key_dic["my_key"] # Intro body st.title("COP 4813 - Web Application Programming") st.title("Project - 1") st.subheader("Part A - The Stories API") st.write( "This app uses the Top Stories API to display the most common words used in the top current articles based on a" " specified topic selected by the user. The data is displayed as a line chart and as a wordcloud image." ) # Creates and input field and the topic options select box st.subheader("I - Topic Selection") userName = st.text_input(label="Please enter your name:", value='', max_chars=None, key=None, type='default')
from nltk import word_tokenize from nltk import pprint from nltk.probability import FreqDist from nltk.corpus import stopwords import main_functions from wordcloud import WordCloud import matplotlib.pyplot as plt import streamlit as st import numpy as np import pandas as pd # nltk.download("punkt") # nltk.download("stopwords") # extract the api key fromthe json file api_key_dict = main_functions.read_from_file("JSON_Files/api_key.json") api_key = api_key_dict["my_key"] # general formatting for the web page using streamlit st.title("COP 4813 - Web Application Programming") st.title("Project 1") st.header("Part A - The Stories API") st.subheader("This app uses the Top Stories API to display the most common words used in the top current articles " "based on a specified topic selected by the user. The data is displayed as a line chart and as a " "wordcloud image.") st.subheader("") st.header("**I - Topic Selection**") # input for the first portion, the users name and the desired topic name = st.text_input("Please enter your name")
import main_functions import requests import os.path #Deserializes api key that is in .json format myKey = main_functions.read_from_file("api_key.json") api_key = myKey['apikey'] #The first header requests data in .json and the second passes the deserialized api key headers = {'Content-Type': 'application/json', 'apikey': api_key} #Within the response lib you can pass headers and params as a dic. Can also be a tuple. 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,
import json import main_functions import requests my_nasa_api = main_functions.read_from_file(("nasa_api.json")) my_nasa_api = my_nasa_api["nasa_api"] url = "https://api.nasa.gov/planetary/apod?api_key=" url2 = url + my_nasa_api # apod = requests.get(url2).json() # main_functions.save_to_file(apod,"apod.json") apod = main_functions.read_from_file("apod.json") print(type(apod)) print(apod.keys()) print(apod["url"]) print(apod["explanation"])
import json import main_functions import requests url = "http://api.open-notify.org/astros.json" # astronauts_in_space = requests.get(url).json() astronauts_filename = "astronauts.json" # astronauts_dict = main_functions.save_to_file(astronauts_in_space,astronauts_filename) astronauts_dict = main_functions.read_from_file(astronauts_filename) print(type(astronauts_dict)) print(astronauts_dict.keys()) print(len(astronauts_dict)) print(type(astronauts_dict["message"])) print(type(astronauts_dict["people"])) print(type(astronauts_dict["number"])) for i in astronauts_dict["people"]: print(i["name"])
import requests import json import main_functions import nltk from nltk import sent_tokenize from nltk import word_tokenize from nltk.probability import FreqDist from nltk.corpus import stopwords from pprint import pprint from wordcloud import WordCloud import matplotlib.pyplot as plt #nltk.download("punkt") #nltk.download("stopwords") api_key_dict = main_functions.read_from_file( "JSON_File/api_key.json") # Assigns api_key_dict with the value that # read_from_file method will return from # the main_functions api_key = api_key_dict["my_key"] # Assigns api_key with the value inside of # json file that has the keyword "my_key" st.title("COP4813 - Web Application Programming") # Displays a title st.title("Project 1") # Displays a title st.header("Part A - The Stories API") # Displays a header st.write( "This app uses the Top Stories API to display the most common words used in the top current" " articles based on a specific topic selected by the user. The data is displayed as a line chart and"
import streamlit as st import main_functions import requests st.title("Detect languages") st.header("Please type a sentence in any language") lang_list = main_functions.read_from_file("lang_list.json") sent = st.text_input("Enter sentence") if sent: sent = sent.replace(" ", "+") api_key = main_functions.read_from_file("api_key.json") my_key = api_key["my_key"] url = "https://ws.detectlanguage.com/0.2/detect?q=" + sent + "&key=" + my_key response = requests.get(url).json() conf = 10000 lang = "" for i in response["data"]["detections"]: if i['confidence'] < conf: conf, lang = i['confidence'], i['language'] for i in lang_list: if i["code"] == lang: lang = i["name"].title()
st.text( "in the top current articles based on a specified topic selected by the user" ) st.text("The data is displayed as a line chart and as a wordcloud image.") st.header("I - Topic Selection") 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():
from flask import Flask, render_template, request, url_for from flask_pymongo import PyMongo from flask_wtf import FlaskForm import requests from wtforms import StringField, DecimalField, SelectField, DateField import main_functions credentials = main_functions.read_from_file("credentials.json") password = credentials["password"] connection_string = "mongodb+srv://Jordy:{0}@project3.rqprq.mongodb.net/Project3?retryWrites=true&w=majority".format( password) app = Flask(__name__) app.config["SECRET_KEY"] = "COP4813" app.config["MONGO_URI"] = connection_string mongo = PyMongo(app) class Expenses(FlaskForm): description = StringField('Description') category = SelectField('Category', choices=[('日本語', '日本語'), ('ストロングゼロ', 'ストロングゼロ'), ('ライトノベル', 'ライトノベル'), ('𠮷野家', '𠮷野家'), ('大学', '大学'), ('コンピューター', 'コンピューター'), ('フライト', 'フライト'), ('電話', '電話'), ('動機', '動機'), ('生活', '生活')]) cost = DecimalField('Cost') date = DateField('Date') # TO BE COMPLETED (please delete the word pass above)
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
import requests import main_functions from flask import Flask, render_template, request from flask_pymongo import PyMongo from flask_wtf import FlaskForm from wtforms import StringField, DecimalField, SelectField, DateField app = Flask(__name__) credentials = main_functions.read_from_file("JSON_Documents/credentials.json") username = credentials["username"] password = credentials["password"] app.config["SECRET_KEY"] = "010296db" app.config[ "MONGO_URI"] = "mongodb+srv://{0}:{1}@cluster0.rhhi3.mongodb.net/db?retryWrites=true&w=majority".format( username, password) mongo = PyMongo(app) class Expenses(FlaskForm): description = StringField('description') category = SelectField('category', choices=[('rent', 'Rent'), ('electricity', 'Electricity'), ('water', 'Water'), ('insurance', 'Insurance'), ('internet', 'Internet'), ('restaurants', 'Restaurants'), ('groceries', 'Groceries'), ('gas', 'Gas'), ('college', 'College'), ('party', 'Party'), ('mortgage', 'Mortgage')]) cost = DecimalField('cost', places=2)
import json import main_functions import requests print("Example 1 - How many astronauts are in space right now") url = "http://api.open-notify.org/astros.json" astronauts_json = requests.get(url).json() #main_functions.save_to_file("astronauts.json", astronauts_json) astronauts_in_space = main_functions.read_from_file("astronauts.json") for i in astronauts_in_space['people']: print(i['name'])
from nltk import sent_tokenize from nltk import word_tokenize from nltk.probability import FreqDist from nltk.corpus import stopwords import main_functions from pprint import pprint from wordcloud import WordCloud import matplotlib.pyplot as plt from bokeh.plotting import figure from PIL import Image import time import numpy as np import pandas as pd import plotly.express as px api_key_dict = main_functions.read_from_file("JSON_Files/api_key.json") api_key = api_key_dict["my_key"] 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 generate_data(type, response_type): if (response_type == 'stories'):
import json import main_functions import requests api_key_dict = main_functions.read_from_file('JSON_files/api_key.json') api_key = api_key_dict['my_key'] #Top stories 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 #Most viewed, shared, or emailed articles 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
import streamlit as st import nltk from nltk import word_tokenize from nltk.probability import FreqDist from nltk.corpus import stopwords import requests import main_functions from wordcloud import WordCloud import matplotlib.pyplot as plt nltk.download("punkt") nltk.download("stopwords") # Extract API_key from api_key.jason api_key_dict = main_functions.read_from_file("JSON_Files/api_key.json") my_api_key = api_key_dict["my_api_key"] # Display Title st.title("COP 4813 - Web Application Programming") st.title("Project 1") # Part A - The Stories API st.header("Part A - The Stories API") st.write( "This app uses the top stories API to display the most common words used in the top current " "articles based on a specific topic selected by the user. The data is displayed as a line chart and " "as a wordcloud image.") # Topic Selection
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"])