def get_btc_eth(C_3_URL, C_4_URL, LOCAL_CUR, color): C_URL_3 = C_3_URL + str(LOCAL_CUR) + '.json' C_URL_4 = C_4_URL + str(LOCAL_CUR).lower() # print(C_URL_1) error_connect = True while error_connect is True: try: # HTTP request # print('Attempting to connect to OWM.') response_c_3 = requests.get(str(C_URL_3)) response_c_4 = requests.get(str(C_URL_4)) error_connect = None except: # Call function to display connection error print('Connection error.') # error_connect = None error = True d_f.display_error(' CRYPTO CONNECTION', color) # break # delete the comment below # error = None while error is None: # Check status of code request if response_c_3.status_code == 200 and response_c_4.status_code == 200: # print('Connection to Open Weather successful.') c_3_data = response_c_3.json() c_4_data = response_c_4.json() bitcoin_exchange = "1 BTC: " + \ str("{:.2f}".format(float(c_3_data['bpi'][str(LOCAL_CUR)]['rate_float']))) eth_exchange = "1 ETH: " + \ str("{:.2f}".format(float(c_4_data['ethereum'][str(LOCAL_CUR).lower()]))) return bitcoin_exchange, eth_exchange error = True else: d_f.display_error('HTTP CRYPTO', color)
def get_stock_week(ST_URL, ST_API, ST_C, st_base, LOCAL_CUR, color): st_date = get_year() URL_ST = ST_URL + ST_C + "/" + str(st_date) + "?apiKey=" + str(ST_API) # print(URL_ST) error_connect = True while error_connect is True: try: # HTTP request # print('Attempting to connect to OWM.') response_st = requests.get(str(URL_ST)) error_connect = None except: # Call function to display connection error print('Connection error.') # error_connect = None # error = True d_f.display_error(' STOCK CONNECTION', color) # break # delete the comment below # error = None while error == None: # Check status of code request if response_st.status_code == 200: # print('Connection to Open Weather successful.') st_data = response_st.json() st_open = st_data["open"] st_symbol = st_data["symbol"] st_close = st_data["close"] stocks = [] stocks.append("-" + st_symbol + ":") stocks.append("O: " + str("{:.2f}".format((float(st_open) * float(st_base))))) stocks.append("C: " + str("{:.2f}".format((float(st_close) * float(st_base))))) return stocks error = True else: d_f.display_error('HTTP STOCK', color)
def get_currency(C_1_URL, C_1_API, LOCAL_CUR, C_CHECK, color): C_URL_1 = str(C_1_URL) + '/api/v7/convert?q=' + \ str(C_CHECK) + '_' + str(LOCAL_CUR) + '&compact=ultra&apiKey=' + str(C_1_API) # print(C_URL_1) error_connect = True while error_connect is True: try: # HTTP request # print('Attempting to connect to OWM.') response_c_1 = requests.get(str(C_URL_1)) error_connect = None except: # Call function to display connection error print('Connection error.') # error_connect = None error = True d_f.display_error(' CURRENCY CONNECTION', color) # break # delete the comment below # error = None while error is None: # Check status of code request if response_c_1.status_code == 200: # print('Connection to Open Weather successful.') c_1_data = response_c_1.json() cur_exch = '' cur_exch = "{:.4f}".format( float(c_1_data[str(C_CHECK) + '_' + str(LOCAL_CUR)])) return cur_exch error = True else: d_f.display_error('HTTP CURRENCY', color)
def get_transit(TRANSLINK_URL, T_STOP, T_API_KEY, T_BUS, T_BUS_TIME, color): T_URL = TRANSLINK_URL+T_STOP + '/estimates?apiKey=' + \ T_API_KEY + '&count=' + T_BUS + '&timeframe=' + T_BUS_TIME # print(T_URL) error_connect = True while error_connect is True: try: # HTTP request # print('Attempting to connect to Translink.') response_t = requests.get(str(T_URL), headers={'Accept': 'application/JSON'}) # print('Connection to Translink successful.') error_connect = None except: # Call function to display connection error print('Connection error.') d_f.display_error('TRANSIT CONNECTION', color) error = None while error == None: # Check status of code request if response_t.status_code == 200: # print('Connection to Translink successful.') t_data = response_t.json() # print(t_data) t_route_no = t_data[0]['RouteNo'] t_schedules = t_data[0]['Schedules'] bus_sch = [] est_counter = 0 if int(t_schedules[0]['ExpectedCountdown']) >= 0: t_route_dest_0 = route_format(t_schedules[0]['Destination']) t_sch_elt_t_0 = ELT_format(t_schedules[0]['ExpectedLeaveTime']) t_sch_EC_0, EC_0_time = EC_format( t_schedules[0]['ExpectedCountdown']) bus_sch.append( str(t_route_no) + "-" + str(t_route_dest_0) + " in " + str(t_sch_EC_0) + EC_0_time + " at " + str(t_sch_elt_t_0)) est_counter = est_counter + 1 if int(t_schedules[1]['ExpectedCountdown']) >= 0: t_route_dest_1 = route_format(t_schedules[1]['Destination']) t_sch_elt_t_1 = ELT_format(t_schedules[1]['ExpectedLeaveTime']) t_sch_EC_1, EC_1_time = EC_format( t_schedules[1]['ExpectedCountdown']) bus_sch.append( str(t_route_no) + "-" + str(t_route_dest_1) + " in " + str(t_sch_EC_1) + EC_1_time + " at " + str(t_sch_elt_t_1)) est_counter = est_counter + 1 if int(t_schedules[2] ['ExpectedCountdown']) >= 0 and est_counter < 2: t_route_dest_2 = route_format(t_schedules[2]['Destination']) t_sch_elt_t_2 = ELT_format(t_schedules[2]['ExpectedLeaveTime']) t_sch_EC_2, EC_2_time = EC_format( t_schedules[2]['ExpectedCountdown']) bus_sch.append( str(t_route_no) + "-" + str(t_route_dest_2) + " in " + str(t_sch_EC_2) + EC_2_time + " at " + str(t_sch_elt_t_2)) est_counter = est_counter + 1 t_data.clear() t_schedules.clear() return bus_sch error = True else: # Call function to display HTTP error d_f.display_error('HTTP TRANSIT', color)
def get_news(NEWS_URL, NEWS_API, NEWS_SOURCES, news_country, news_num, color): if news_num == 0: news_URL = str(NEWS_URL) + "country=" + str( news_country).lower() + "&apiKey=" + str(NEWS_API) elif news_num == 1: news_URL = str(NEWS_URL) + "sources=" + str( NEWS_SOURCES) + "&apiKey=" + str(NEWS_API) # print(news_URL) error_connect = True # print(news_URL) while error_connect == True: try: # HTTP request # print('Attempting to connect to OWM.') response_n = requests.get(str(news_URL)) error_connect = None except: # Call function to display connection error print('Connection error.') # error_connect = None # error = True d_f.display_error(' NEWS CONNECTION', color) # break # delete the comment below # error = None while error == None: # Check status of code request if response_n.status_code == 200: # print('Connection to Open Weather successful.') n_data = response_n.json() # print(n_data) news_items = [] for x in range(0, 5): chk_str = int(len(str(n_data["articles"][x]["title"]))) chk_str_1 = chk_str # print(x) #print("before: " + str(chk_str)) # 43 check = False if chk_str > 48: chk_str = 48 else: chk_str = chk_str check = True #print("after: " + str(chk_str)) while check is False: if str(n_data["articles"][x]["title"])[chk_str] != " ": chk_str = chk_str - 1 #print("space_false: " + str(chk_str)) check = False else: chk_str = chk_str #print("space_true: " + str(chk_str)) check = True if chk_str_1 >= 48: news_items.append( str(x + 1) + "- " + str(n_data["articles"][x]["title"])[0:chk_str] + " ") if chk_str_1 > 92: news_items.append( str(n_data["articles"][x]["title"])[chk_str + 1:91] + " ") else: news_items.append( str(n_data["articles"][x]["title"])[chk_str + 1:chk_str_1] + " ") else: news_items.append( str(x + 1) + "- " + str(n_data["articles"][x]["title"])[0:chk_str] + " ") # print(news_items[x]) return news_items error = True else: d_f.display_error('NEWS HTTP', color)