Added ability to query for weather
Uses meteomedia.de and a list of stations and their IDs to reply a fancy picture url with weather information.
This commit is contained in:
		
							parent
							
								
									bb1a7ce99a
								
							
						
					
					
						commit
						db06e812ee
					
				
					 2 changed files with 1067 additions and 4 deletions
				
			
		
							
								
								
									
										34
									
								
								bot.py
									
										
									
									
									
								
							
							
						
						
									
										34
									
								
								bot.py
									
										
									
									
									
								
							| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import requests
 | 
					 | 
				
			||||||
import re
 | 
					import re
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import requests
 | 
				
			||||||
from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup
 | 
					from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup
 | 
				
			||||||
from telegram.ext import Updater, CommandHandler, InlineQueryHandler, CallbackQueryHandler
 | 
					from telegram.ext import Updater, CommandHandler, InlineQueryHandler, CallbackQueryHandler
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,7 +29,7 @@ class Query:
 | 
				
			||||||
        argument_names = re.findall(r' to | in | times ', request_tmp)
 | 
					        argument_names = re.findall(r' to | in | times ', request_tmp)
 | 
				
			||||||
        arguments = re.split(r' to | in | times ', request_tmp)
 | 
					        arguments = re.split(r' to | in | times ', request_tmp)
 | 
				
			||||||
        if not arguments[0].isdigit():
 | 
					        if not arguments[0].isdigit():
 | 
				
			||||||
            reply = search_station(arguments[0])
 | 
					            reply = search_vvs_station(arguments[0])
 | 
				
			||||||
            if len(reply) == 1:
 | 
					            if len(reply) == 1:
 | 
				
			||||||
                self.station_id = reply[0]['stationId']
 | 
					                self.station_id = reply[0]['stationId']
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
| 
						 | 
					@ -77,7 +78,7 @@ def handle_multiple_stations_reply(update, context):
 | 
				
			||||||
    del sent_multiple_station_message_ids[update.effective_chat['id']]
 | 
					    del sent_multiple_station_message_ids[update.effective_chat['id']]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def search_station(query):
 | 
					def search_vvs_station(query):
 | 
				
			||||||
    request = requests.get("https://efa-api.asw.io/api/v1/station/?search=" + query)
 | 
					    request = requests.get("https://efa-api.asw.io/api/v1/station/?search=" + query)
 | 
				
			||||||
    if request.status_code != 200:
 | 
					    if request.status_code != 200:
 | 
				
			||||||
        raise ServerCommunicationError
 | 
					        raise ServerCommunicationError
 | 
				
			||||||
| 
						 | 
					@ -121,7 +122,7 @@ def get_vvs_departures(query):
 | 
				
			||||||
    reply.append("Next departures for station: " + request.json()[0]['stopName'])
 | 
					    reply.append("Next departures for station: " + request.json()[0]['stopName'])
 | 
				
			||||||
    printed_departures = 0
 | 
					    printed_departures = 0
 | 
				
			||||||
    for station in request.json():
 | 
					    for station in request.json():
 | 
				
			||||||
        if station['direction'].casefold().find(query.destination.casefold()) != -1 :
 | 
					        if station['direction'].casefold().find(query.destination.casefold()) != -1:
 | 
				
			||||||
            if query.line == '' or (query.line != -1 and station['number'] == query.line):
 | 
					            if query.line == '' or (query.line != -1 and station['number'] == query.line):
 | 
				
			||||||
                reply.append(
 | 
					                reply.append(
 | 
				
			||||||
                    "Line " + station['number'] + " to \"" + station['direction'] + "\" at "
 | 
					                    "Line " + station['number'] + " to \"" + station['direction'] + "\" at "
 | 
				
			||||||
| 
						 | 
					@ -154,6 +155,30 @@ def get_station_id_list(name):
 | 
				
			||||||
    return request.json()
 | 
					    return request.json()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def search_weather_station(query: str):
 | 
				
			||||||
 | 
					    station_file = open('stations', 'r')
 | 
				
			||||||
 | 
					    for line in station_file:
 | 
				
			||||||
 | 
					        if line.casefold().find(query.casefold()) != -1:
 | 
				
			||||||
 | 
					            station_file.close()
 | 
				
			||||||
 | 
					            return line
 | 
				
			||||||
 | 
					    return 'none'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def handle_weather(update, context):
 | 
				
			||||||
 | 
					    if len(context.args) == 0:
 | 
				
			||||||
 | 
					        update.message.reply_text('Please provide a name!')
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					    station_line = search_vvs_station(' '.join(context.args))
 | 
				
			||||||
 | 
					    if station_line == 'none':
 | 
				
			||||||
 | 
					        update.message.reply_text('No weather station matching this name found!')
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					    separator_index = station_line.find(' ')
 | 
				
			||||||
 | 
					    update.message.reply_text('Weather for ' + station_line[separator_index+1:-1]
 | 
				
			||||||
 | 
					                              + ':\nhttp://wetterstationen.meteomedia.de/messnetz/vorhersagegrafik/' +
 | 
				
			||||||
 | 
					                              station_line[:separator_index] + '.png',
 | 
				
			||||||
 | 
					                              disable_notification=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def error_callback(update, context):
 | 
					def error_callback(update, context):
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        raise context.error
 | 
					        raise context.error
 | 
				
			||||||
| 
						 | 
					@ -172,6 +197,7 @@ def __main__():
 | 
				
			||||||
    inline_station_search_handler = InlineQueryHandler(inline_station_search)
 | 
					    inline_station_search_handler = InlineQueryHandler(inline_station_search)
 | 
				
			||||||
    dispatcher.add_handler(inline_station_search_handler)
 | 
					    dispatcher.add_handler(inline_station_search_handler)
 | 
				
			||||||
    dispatcher.add_handler(CommandHandler('vvs', handle_vvs))
 | 
					    dispatcher.add_handler(CommandHandler('vvs', handle_vvs))
 | 
				
			||||||
 | 
					    dispatcher.add_handler(CommandHandler('weather', handle_weather))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    dispatcher.add_error_handler(error_callback)
 | 
					    dispatcher.add_error_handler(error_callback)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue