from exceptions import NoArgError, WeatherStationNotFoundError


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_meteomedia(update, context):
    if len(context.args) == 0:
        raise NoArgError
    station_line = search_weather_station(' '.join(context.args))
    if station_line == 'none':
        raise WeatherStationNotFoundError
    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)
