Exemplo n.º 1
0
    def run(self, m):
        input = get_input(m, ignore_reply=False)
        if not input:
            return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

        lat, lon, locality, country = get_coords(input)

        url = 'http://api.wunderground.com/api/%s/webcams/conditions/forecast/q/%s,%s.json' % (
            self.bot.config.api_keys.weather_underground, lat, lon)

        data = send_request(url)

        try:
            weather = data.current_observation
            forecast = data.forecast.simpleforecast.forecastday
            webcams = data.webcams
        except:
            return self.bot.send_message(m, self.bot.trans.errors.no_results)

        title = self.bot.trans.plugins.weather.strings.title % (locality, country)
        temp = weather.temp_c
        feelslike = ""
        try:
            if (float(weather.feelslike_c) - float(weather.temp_c)) > 0.001:
                feelslike = self.bot.trans.plugins.weather.strings.feelslike % weather.feelslike_c
        except:
            pass

        # weather_string = weather.weather.title()
        weather_string = self.bot.trans.plugins.weather.strings[weather.icon]
        weather_icon = (self.get_weather_icon(weather.icon))
        humidity = weather.relative_humidity
        wind = format(float(weather.wind_kph) / 3.6, '.1f')

        if is_command(self, 1, m.content):
            message = u'%s\n%s %s%s\n🌡%sºC 💧%s 🌬%s m/s' % (
                remove_html(title), weather_icon, weather_string, feelslike, temp, humidity, wind)
            try:
                photo = get_streetview(lat, lon, self.bot.config.api_keys.google_developer_console)
            except Exception as e:
                print(e)
                photo = None

            if photo:
                return self.bot.send_message(m, photo, 'photo', extra={'caption': message})
            else:
                return self.bot.send_message(m, message, 'text', extra={'format': 'HTML'})

        elif is_command(self, 2, m.content):
            message = self.bot.trans.plugins.weather.strings.titleforecast % (locality, country)
            for day in forecast:
                weekday = self.bot.trans.plugins.weather.strings[day.date.weekday.lower()][:3]
                temp = day.low.celsius
                temp_max = day.high.celsius
                # weather_string = day.conditions.title()
                weather_string = self.bot.trans.plugins.weather.strings[day.icon]
                weather_icon = (self.get_weather_icon(day.icon))
                message += u'\n • <b>%s</b>: 🌡 %s-%sºC %s %s' % (weekday, temp, temp_max, weather_icon, weather_string)

            return self.bot.send_message(m, message, extra={'format': 'HTML'})
Exemplo n.º 2
0
    def send_message(self, message):
        if not message.extra:
            message.extra = {}

        if message.type != 'text' and message.content.startswith('http'):
            message.content = download(message.content)
        elif message.type != 'text' and not message.content.startswith('/'):
            message.content = self.sender.load_file(message.content)

        if not message.extra or not 'caption' in message.extra:
            message.extra['caption'] = None

        if message.type == 'text':
            self.sender.send_typing(self.peer(message.conversation.id), 1)

            if 'format' in message.extra and message.extra[
                    'format'] == 'Markdown':
                message.content = remove_markdown(message.content)
            elif 'format' in message.extra and message.extra[
                    'format'] == 'HTML':
                message.content = remove_html(message.content)

            try:
                self.sender.send_msg(self.peer(message.conversation.id),
                                     message.content,
                                     enable_preview=False)
            except Exception as e:
                logging.exception(e)

        elif message.type == 'photo':
            self.sender.send_typing(self.peer(message.conversation.id), 1)  # 7
            try:
                if message.reply:
                    self.sender.reply_photo(message.reply, message.content,
                                            message.extra['caption'])
                else:
                    self.sender.send_photo(self.peer(message.conversation.id),
                                           message.content,
                                           message.extra['caption'])
            except Exception as e:
                logging.exception(e)

        elif message.type == 'audio':
            self.sender.send_typing(self.peer(message.conversation.id), 1)  # 6
            try:
                if message.reply:
                    self.sender.reply_audio(message.reply, message.content)
                else:
                    self.sender.send_audio(self.peer(message.conversation.id),
                                           message.content)
            except Exception as e:
                logging.exception(e)

        elif message.type == 'document':
            self.sender.send_typing(self.peer(message.conversation.id), 1)  # 8
            try:
                if message.reply:
                    self.sender.reply_document(message.reply, message.content,
                                               message.extra['caption'])
                else:
                    self.sender.send_document(
                        self.peer(message.conversation.id), message.content,
                        message.extra['caption'])
            except Exception as e:
                logging.exception(e)

        elif message.type == 'sticker':
            if message.reply:
                self.sender.reply_file(message.reply, message.content)
            else:
                self.sender.send_file(self.peer(message.conversation.id),
                                      message.content)

        elif message.type == 'video':
            self.sender.send_typing(self.peer(message.conversation.id), 1)  # 4
            try:
                if message.reply:
                    self.sender.reply_video(message.reply, message.content,
                                            message.extra['caption'])
                else:
                    self.sender.send_video(self.peer(message.conversation.id),
                                           message.content,
                                           message.extra['caption'])
            except Exception as e:
                logging.exception(e)

        elif message.type == 'voice':
            self.sender.send_typing(self.peer(message.conversation.id), 5)
            try:
                if message.reply:
                    self.sender.reply_audio(message.reply, message.content)
                else:
                    self.sender.send_audio(self.peer(message.conversation.id),
                                           message.content)
            except Exception as e:
                logging.exception(e)

        elif message.type == 'location':
            self.sender.send_typing(self.peer(message.conversation.id), 1)  # 9
            if message.reply:
                self.sender.reply_location(message.reply,
                                           message.content['latitude'],
                                           message.content['longitude'])
            else:
                self.sender.send_location(self.peer(message.conversation.id),
                                          message.content['latitude'],
                                          message.content['longitude'])

        else:
            print('UNKNOWN MESSAGE TYPE: ' + message.type)
            logging.debug("UNKNOWN MESSAGE TYPE")
Exemplo n.º 3
0
    def run(self, m):
        input = get_input(m, ignore_reply=False)
        if not input:
            return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={"format": "HTML"})

        lat, lon, locality, country = get_coords(input)

        url = "http://api.wunderground.com/api/%s/webcams/conditions/forecast/q/%s,%s.json" % (
            self.bot.config.api_keys.weather_underground,
            lat,
            lon,
        )

        data = send_request(url)

        try:
            weather = data.current_observation
            forecast = data.forecast.simpleforecast.forecastday
            webcams = data.webcams
        except:
            return self.bot.send_message(m, self.bot.trans.errors.no_results)

        title = self.bot.trans.plugins.weather.strings.title % (locality, country)
        temp = weather.temp_c
        feelslike = ""
        if (float(weather.feelslike_c) - float(weather.temp_c)) > 0.001:
            feelslike = self.bot.trans.plugins.weather.strings.feelslike % weather.feelslike_c
        # weather_string = weather.weather.title()
        weather_string = self.bot.trans.plugins.weather.strings[weather.icon]
        weather_icon = self.get_weather_icon(weather.icon)
        humidity = weather.relative_humidity
        wind = format(float(weather.wind_kph) / 3.6, ".1f")

        if is_command(self, 1, m.content):
            message = u"%s\n%s %s%s\n🌡%sºC 💧%s 🌬%s m/s" % (
                remove_html(title),
                weather_icon,
                weather_string,
                feelslike,
                temp,
                humidity,
                wind,
            )
            try:
                photo = get_streetview(lat, lon, self.bot.config.api_keys.google_developer_console)
            except Exception as e:
                print(e)
                photo = None

            if photo:
                return self.bot.send_message(m, photo, "photo", extra={"caption": message})
            else:
                return self.bot.send_message(m, message, "text", extra={"format": "HTML"})

        elif is_command(self, 2, m.content):
            message = self.bot.trans.plugins.weather.strings.titleforecast % (locality, country)
            for day in forecast:
                weekday = self.bot.trans.plugins.weather.strings[day.date.weekday.lower()][:3]
                temp = day.low.celsius
                temp_max = day.high.celsius
                # weather_string = day.conditions.title()
                weather_string = self.bot.trans.plugins.weather.strings[day.icon]
                weather_icon = self.get_weather_icon(day.icon)
                message += u"\n • <b>%s</b>: 🌡 %s-%sºC %s %s" % (weekday, temp, temp_max, weather_icon, weather_string)

            return self.bot.send_message(m, message, extra={"format": "HTML"})
Exemplo n.º 4
0
    def run(self, m):
        input = get_input(m, ignore_reply=False)
        if not input:
            return self.bot.send_message(m, generate_command_help(self, m.content), extra={'format': 'HTML'})
            # return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

        status, values = get_coords(input, self.bot)

        if status == 'ZERO_RESULTS' or status == 'INVALID_REQUEST':
            return self.bot.send_message(m, self.bot.trans.errors.api_limit_exceeded, extra={'format': 'HTML'})
        elif status == 'OVER_DAILY_LIMIT':
            return self.bot.send_message(m, self.bot.trans.errors.no_results, extra={'format': 'HTML'})
        elif status == 'REQUEST_DENIED':
            return self.bot.send_message(m, self.bot.trans.errors.connection_error, extra={'format': 'HTML'})

        lat, lon, locality, country = values

        url = 'https://api.openweathermap.org/data/2.5/weather'
        params = {
            'APPID': self.bot.config.api_keys.open_weather,
            'lon': lon,
            'lat': lat,
            'units': 'metric',
            'lang': 'es'
        }

        data = send_request(url, params, bot=self.bot)
        logging.info(data)
        if not data or data.cod != 200:
            return self.bot.send_message(m, self.bot.trans.errors.no_results, extra={'format': 'HTML'})

        title = self.bot.trans.plugins.weather.strings.title % (
            locality, country)
        # weather_string = weather.weather.title()
        #weather_string = str(self.bot.trans.plugins.weather.strings[data.weather.id])
        weather_string = data.weather[0].main
        weather_icon = self.get_weather_icon(data.weather[0].icon)
        temp = round(data.main.temp, 1)
        humidity = data.main.humidity
        wind = data.wind.speed
        feelslike = ''
        # try:
        #     temp_c = mc.Temp(data.main.temp, 'c')
        #     feelslike_c = round(mc.heat_index(temperature=temp_c, humidity=humidity), 1)
        #     if (float(feelslike_c) - float(data.main.temp)) > 0.001:
        #         feelslike = self.bot.trans.plugins.weather.strings.feelslike % feelslike_c
        # except:
        #     pass

        if is_command(self, 1, m.content):
            message = u'%s\n%s %s%s\n🌡%sºC 💧%s%% 🌬%s m/s' % (
                remove_html(title), weather_icon, weather_string, feelslike, temp, humidity, wind)
            try:
                photo = get_streetview(
                    lat, lon, self.bot.config.api_keys.google_developer_console)
            except Exception as e:
                catch_exception(e, self.bot)
                photo = None

            if photo:
                return self.bot.send_message(m, photo, 'photo', extra={'caption': message})
            else:
                return self.bot.send_message(m, message, extra={'format': 'HTML'})

        elif is_command(self, 2, m.content):
            return self.bot.send_message(m, self.bot.trans.errors.not_implemented, extra={'format': 'HTML'})
Exemplo n.º 5
0
    def run(self, m):
        input = get_input(m, ignore_reply=False)
        if not input:
            return self.bot.send_message(
                m,
                self.bot.trans.errors.missing_parameter,
                extra={'format': 'HTML'})

        status, values = get_coords(input, self.bot)

        if status == 'ZERO_RESULTS' or status == 'INVALID_REQUEST':
            return self.bot.send_message(
                m,
                self.bot.trans.errors.api_limit_exceeded,
                extra={'format': 'HTML'})
        elif status == 'OVER_DAILY_LIMIT':
            return self.bot.send_message(m,
                                         self.bot.trans.errors.no_results,
                                         extra={'format': 'HTML'})
        elif status == 'REQUEST_DENIED':
            return self.bot.send_message(
                m,
                self.bot.trans.errors.connection_error,
                extra={'format': 'HTML'})

        lat, lon, locality, country = values

        url = 'http://api.wunderground.com/api/%s/webcams/conditions/forecast/q/%s,%s.json' % (
            self.bot.config.api_keys.weather_underground, lat, lon)

        data = send_request(url)
        if not data or not 'current_observation' in data:
            return self.bot.send_message(m,
                                         self.bot.trans.errors.no_results,
                                         extra={'format': 'HTML'})

        weather = data.current_observation
        forecast = data.forecast.simpleforecast.forecastday
        webcams = data.webcams

        title = self.bot.trans.plugins.weather.strings.title % (locality,
                                                                country)
        temp = weather.temp_c
        feelslike = ""
        try:
            if (float(weather.feelslike_c) - float(weather.temp_c)) > 0.001:
                feelslike = self.bot.trans.plugins.weather.strings.feelslike % weather.feelslike_c
        except:
            pass

        # weather_string = weather.weather.title()
        if weather.icon == '':
            weather.icon = 'clear'
        weather_string = self.bot.trans.plugins.weather.strings[weather.icon]
        weather_icon = self.get_weather_icon(weather.icon)
        humidity = weather.relative_humidity
        wind = format(float(weather.wind_kph) / 3.6, '.1f')

        if is_command(self, 1, m.content):
            message = u'%s\n%s %s%s\n🌡%sºC 💧%s 🌬%s m/s' % (
                remove_html(title), weather_icon, weather_string, feelslike,
                temp, humidity, wind)
            # try:
            #     photo = get_streetview(lat, lon, self.bot.config.api_keys.google_developer_console)
            # except Exception as e:
            #     catch_exception(e, self.bot)
            photo = None

            if photo:
                return self.bot.send_message(m,
                                             photo,
                                             'photo',
                                             extra={'caption': message})
            else:
                return self.bot.send_message(m,
                                             message,
                                             extra={'format': 'HTML'})

        elif is_command(self, 2, m.content):
            message = self.bot.trans.plugins.weather.strings.titleforecast % (
                locality, country)
            for day in forecast:
                weekday = self.bot.trans.plugins.weather.strings[
                    day.date.weekday.lower()][:3]
                temp = day.low.celsius
                temp_max = day.high.celsius
                # weather_string = day.conditions.title()
                weather_string = self.bot.trans.plugins.weather.strings[
                    day.icon]
                weather_icon = (self.get_weather_icon(day.icon))
                message += u'\n • <b>%s</b>: 🌡 %s-%sºC %s %s' % (
                    weekday, temp, temp_max, weather_icon, weather_string)

            return self.bot.send_message(m, message, extra={'format': 'HTML'})
Exemplo n.º 6
0
    def run(self, m):
        input = get_input(m, ignore_reply=False)
        if not input:
            return self.bot.send_message(m,
                                         generate_command_help(
                                             self, m.content),
                                         extra={'format': 'HTML'})
            # return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

        url = 'https://duckduckgo.com/'
        params = {'q': input}
        res = requests.post(url, data=params)
        searchObj = re.search(r'vqd=([\d-]+)\&', res.text, re.M | re.I)
        if not searchObj:
            return self.bot.send_message(m,
                                         self.bot.trans.errors.unknown,
                                         extra={'format': 'HTML'})

        headers = {
            'authority': 'duckduckgo.com',
            'accept': 'application/json, text/javascript, */*; q=0.01',
            'sec-fetch-dest': 'empty',
            'x-requested-with': 'XMLHttpRequest',
            'user-agent':
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'referer': 'https://duckduckgo.com/',
            'accept-language': self.bot.config.locale + ';q=0.9',
        }

        params = (
            ('l', self.bot.config.locale),
            ('o', 'json'),
            ('q', input),
            ('vqd', searchObj.group(1)),
            ('f', ',,,'),
            ('p', '1'),
            ('v7exp', 'a'),
        )

        requestUrl = url + "d.js"

        data = send_request(requestUrl, headers=headers, params=params)

        if not data or not 'results' in data:
            return self.bot.send_message(
                m,
                self.bot.trans.errors.connection_error,
                extra={'format': 'HTML'})

        if len(data.results) == 0:
            return self.bot.send_message(m,
                                         self.bot.trans.errors.no_results,
                                         extra={'format': 'HTML'})

        if not is_command(self, 2, m.content):
            text = self.bot.trans.plugins.web_search.strings.results % input
            limit = 8
            for item in data.results:
                if 't' in item:
                    item['t'] = remove_html(item['t'])
                    if len(item['t']) > 26:
                        item['t'] = item['t'][:23] + '...'
                    text += '\n • <a href="%s">%s</a>' % (item['u'], item['t'])
                    limit -= 1
                    if limit <= 0:
                        break

            self.bot.send_message(m,
                                  text,
                                  extra={
                                      'format': 'HTML',
                                      'preview': False
                                  })

        else:
            text = data.results[0]['u']

            self.bot.send_message(m,
                                  text,
                                  extra={
                                      'format': 'HTML',
                                      'preview': True
                                  })