class NoconocoStream: def __init__(self, owner='', conf_path=None): self.__account = Account('noconoco_bot', conf_path) self.__bots = { 'chat': NoconocoChat(), 'horse': NoconocoHorse(owner), 'horse_profile': NoconocoHorseProfile(), 'recipe': NoconocoRecipe(), 'stock': NoconocoStock(), 'weather': NoconocoWeather() } def get_info(self): return self.__account.info() def post(self, message, in_reply_to_status_id=None): self.__account.post(message, in_reply_to_status_id) def get_mentions(self): return self.__account.unread_mention() def stream_user_timeline(self): self.__account.userstream( NoconocoStreamListener(account=self, bots=self.__bots)) def get_error_message(self, target): return target + 'とか,そんな言葉知らないしー' + self.get_datetime() def get_datetime(self): d = datetime.datetime.today() return ' (' + d.strftime("%H:%M:%S") + ')'
class NoconocoWeather: def __init__(self, conf_path=None): lines = open(location_path).readlines() self.__locations = {k:'%06d'%int(v) for k,v in [line.strip().split(',') for line in lines]} self.__account = Account('noconoco_bot', conf_path) def get_info(self): return self.__account.info() def post(self, message, in_reply_to_status_id=None): self.__account.post(message, in_reply_to_status_id) def reply(self, mention): message = self.get_reply_message(mention) self.__account.post(message, mention.id_str) def get_mentions(self): return self.__account.unread_mention() def get_reply_message(self, mention): location = (mention.text.split(' ')[1]).encode('utf-8') return '@' + mention.user.screen_name.encode('utf-8') +\ ' ' + self.get_weather_message(location) def get_weather_message(self, location): location_code = self.encode_location(location) if location_code is None: return self.get_error_message(location) else: info = self.get_weather_info(location_code) message = (info['location']['city']).encode('utf-8') + 'の天気をお知らせするしー\n' for i in range(0, 2): date = (info['forecasts'][i]['dateLabel']).encode('utf-8') weather = (info['forecasts'][i]['telop']).encode('utf-8') temp = info['forecasts'][i]['temperature']['max'] if temp is not None: temp = (temp['celsius']).encode('utf-8') + '度だ' else: temp = 'よくわかんない' message = message + date + 'の天気は「' + weather + '」で最高気温は' + temp + 'し\n' return message + 'そんなことより早くあたしを撫でればいいし' + self.get_datetime() def get_weather_info(self, location): url = api_base_url + '?city=%s' % location res = urllib2.urlopen(url) return json.loads(res.read()) def encode_location(self, location): if location in self.__locations: return self.__locations[location] else: return None def get_error_message(self, location): return location + 'とか,そんな場所知らないしー' + self.get_datetime() def stream_user_timeline(self): self.__account.userstream(NoconocoWeatherStreamListener(bot=self)) def get_datetime(self): d = datetime.datetime.today() return ' (' + d.strftime("%H:%M:%S") + ')'
class NoconocoStock: def __init__(self, conf_path=None): self.__account = Account('noconoco_bot', conf_path) def get_info(self): return self.__account.info() def post(self, message, in_reply_to_status_id=None): self.__account.post(message, in_reply_to_status_id) def reply(self, mention): message = self.get_reply_message(mention) self.__account.post(message, mention.id_str) def get_mentions(self): return self.__account.unread_mention() def get_reply_message(self, mention): reply_to = '@' + mention.user.screen_name.encode('utf-8') target = (mention.text.split(' ')[1]).encode('utf-8') stock_id, stock_name = self.get_stock_id_name(target) return reply_to + ' ' + self.get_stock_message(stock_id, stock_name) def get_stock_message(self, stock_id, stock_name): q = jsm.Quotes() d = q.get_price(stock_id) price = self.get_stock_price(stock_id) if d is None or price is None: return self.get_error_message(stock_name) message = stock_name + '(' + str(stock_id) + ')の株価は' + str(price) + 'だしー\n'\ '前日終値は' + str(d.close) + 'で今日の始値は' + str(d.open) +\ ',高値は' + str(d.high) + ',安値は' + str(d.low) + 'だしー\n' return message + 'そんなことより早くあたしを撫でればいいし' + self.get_datetime() def get_stock_id_name(self, target): stock_id = self.get_stock_id(target) stock_name = self.get_stock_name(target) if stock_id is None and stock_name is None: return None, None elif stock_id is None: stock_id = self.get_stock_id(stock_name) elif stock_name is None: stock_id = int(stock_id) stock_name = self.get_stock_name(stock_id) return stock_id, stock_name def get_stock_price(self, stock_id): try: url = stock_url + 'stocks/detail/?code=' + str(stock_id) soup = BeautifulSoup(urlopen(url)) res = soup.find_all('td', class_='stoksPrice') regex= r'<.+>(.+)<.+>' price = re.search(regex, str(res[1])).group(1) return int(price.replace(',', '')) except: return None def get_stock_id(self, stock_name): try: url = info_url + 'search/?query=' + stock_name soup = BeautifulSoup(urlopen(url)) res = soup.find('span', {'class': 'code highlight'}) regex = r'\[([0-9]+)\]' matches = re.search(regex, str(res)) if matches is not None: return int(matches.group(1)) else: return None except: return None def get_stock_name(self, stock_id): try: url = info_url + 'search/?query=' + str(stock_id) soup = BeautifulSoup(urlopen(url)) title = str(soup.find('title')) regex = r'<title>(.+)【[0-9]+】.+</title>' return re.search(regex, title).group(1) except: return None def get_error_message(self, target): return target + 'とか,そんな銘柄知らないしー' + self.get_datetime() def stream_user_timeline(self): self.__account.userstream(NoconocoStockStreamListener(bot=self)) def get_datetime(self): d = datetime.datetime.today() return ' (' + d.strftime("%H:%M:%S") + ')'