useful.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:PTSCogs 作者: PlanetTeamSpeakk 项目源码 文件源码
def time(self, ctx, *, place):
        """Get the time of a place somewhere on the earth
        Example:
        [p]time Los Angeles
        [p]time Amsterdam, Netherlands"""
        if "geocodingkey" not in self.settings or self.settings['geocodingkey'] == "key_here":
            await self.bot.say("The geocoding key is not yet set if you're my owner you can set it with {}setgeocodingkey.".format(ctx.prefix))
        elif "timezonekey" not in self.settings or self.settings['timezonekey'] == "key_here":
            await self.bot.say("The timezone key is not yet set if you're my owner you can set it with {}settimezonekey.".format(ctx.prefix))
        else:
            message = await self.bot.say("Getting data...")
            request = requests.get("https://maps.googleapis.com/maps/api/geocode/json?address={}&key={}".format(place, self.settings['geocodingkey'])).json()
            if request['status'] == "ZERO_RESULTS":
                await self.bot.edit_message(message, "Could not find any results for **{}**.".format(place))
            elif request['status'] == "OK":
                lng = request['results'][0]['geometry']['location']['lng']
                lat = request['results'][0]['geometry']['location']['lat']
                fulladdr = request['results'][0]['formatted_address']
                timestamp = int(datetime.datetime.utcnow().timestamp())
                request = requests.get("https://maps.googleapis.com/maps/api/timezone/json?location={},{}&timestamp={}&key={}".format(lat, lng, timestamp, self.settings['timezonekey'])).json()
                if request['status'] != "OK":
                    await self.bot.say("An unknown error occured while getting the time and timezone from the Google API.")
                else:
                    timestamp += request['dstOffset'] + request['rawOffset']
                    time = datetime.datetime.fromtimestamp(timestamp)
                    await self.bot.edit_message(message, "**{}**\n\t{} ({})".format(time.strftime("%d %b %Y %H:%M:%S"), fulladdr, request['timeZoneName']))
            else:
                await self.bot.say("An unknown error occured while getting the longitude and latitude from the Google API.")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号