def timer(bot): from modules.__common__ import shorten_url # Execute feeds script bot.logger.info('Executing %s', __file__) command = ['python3', __file__, '--config-dir={}'.format(bot.config_dir)] process = tornado.process.Subprocess( command, stdout=tornado.process.Subprocess.STREAM) results = yield tornado.gen.Task(process.stdout.read_until_close) # Read and process results cache_path = os.path.join(bot.config_dir, 'feeds.cache') with dbm.open(cache_path, 'c') as feeds_cache: for feed, entries in json.loads(results).items(): for entry in entries: title = entry['title'].replace('\r', ' ').replace('\n', ' ') key = entry['link'].encode('ascii', 'ignore') link = yield shorten_url(entry['link']) author = entry['author'] channels = entry['channels'] message = TEMPLATE.format(feed=feed, title=title, link=link, author=author) # Send each entry to the appropriate channel for channel in channels: bot.send_message(message, channel=channel) # Mark entry as delivered bot.logger.info('Delivered %s from %s to %s', title, feed, ', '.join(channels)) feeds_cache[key] = str(time.time())
def command(bot, nick, message, channel, subreddit, query=''): url = REDDIT_TEMPLATE.format(subreddit=subreddit) client = tornado.httpclient.AsyncHTTPClient() result = yield tornado.gen.Task(client.fetch, url) query = query.lower() try: for result in json.loads(result.body.decode())['data']['children']: data = result['data'] title = data['title'] url = data['url'] nsfw = '[NSFW] ' if data['over_18'] else '' if query not in title.lower() and query not in url.lower(): continue if data['stickied']: continue response = 'From {} - {}{} @ {}'.format(subreddit, nsfw, title, url) shorturl = yield shorten_url(url) if url != shorturl: response += ' || ' + shorturl break except (IndexError, KeyError, ValueError) as e: bot.logger.warn(e) response = 'No results' bot.send_response(response, nick, channel)
def command(bot, nick, message, channel, query=None): params = {'q': query} url = GOOGLE_URL + '?' + urlencode(params) client = tornado.httpclient.AsyncHTTPClient() result = yield tornado.gen.Task(client.fetch, url) try: urls = re.findall(b'/url\?q=([^&]*)', result.body) response = yield shorten_url(unquote(urls[0].decode())) except (IndexError, ValueError) as e: bot.logger.warn(e) response = 'No results' bot.send_response(response, nick, channel)
def command(bot, nick, message, channel, query=None): params = {'q': query, 's': 0} url = DDG_URL + '?' + urlencode(params) client = tornado.httpclient.AsyncHTTPClient() result = yield tornado.gen.Task(client.fetch, url) try: urls = [ unquote(url) for url in re.findall(DDG_RX, result.body.decode()) if 'y.js' not in url ] response = yield shorten_url(urls[0]) except (IndexError, ValueError): response = 'No results' bot.send_response(response, nick, channel)
def timer(bot): # Execute feeds script bot.logger.info('Executing %s', __file__) command = ['python3', __file__, '--config-dir={}'.format(bot.config_dir)] environ = dict( os.environ, **{ 'PYTHONPATH': os.path.join(__file__, '..', '..') + ':' + os.environ.get('PYTHONPATH', '') }) process = tornado.process.Subprocess( command, stdout=tornado.process.Subprocess.STREAM, env=environ) results = yield tornado.gen.Task(process.stdout.read_until_close) # Read configuration config_path = os.path.join(bot.config_dir, 'feeds.yaml') feeds_config = yaml.safe_load(open(config_path)) templates = feeds_config.get('templates', {}) default_template = templates.get('default', TEMPLATE) # Read and process results cache_path = os.path.join(bot.config_dir, 'feeds.cache') with dbm.open(cache_path, 'c') as feeds_cache: for feed, entries in json.loads(results).items(): for entry in entries: title = entry['title'].replace('\r', ' ').replace('\n', ' ') key = entry['link'].encode('ascii', 'ignore') link = yield shorten_url(entry['link']) author = entry['author'] channels = entry['channels'] # Send each entry to the appropriate channel for channel in channels: template = templates.get(channel, default_template) message = bot.format_text(template, feed=feed, title=title, link=link, author=author) bot.send_message(message, channel=channel) # Mark entry as delivered bot.logger.info('Delivered %s from %s to %s', title, feed, ', '.join(channels)) feeds_cache[key] = str(time.time())
def timer(bot): # Execute tweets script bot.logger.info('Executing %s', __file__) command = ['python3', __file__, '--config-dir={}'.format(bot.config_dir)] environ = dict( os.environ, **{ 'PYTHONPATH': os.path.join(__file__, '..', '..') + ':' + os.environ.get('PYTHONPATH', '') }) process = tornado.process.Subprocess( command, stdout=tornado.process.Subprocess.STREAM, env=environ) results = yield tornado.gen.Task(process.stdout.read_until_close) # Read configuration config_path = os.path.join(bot.config_dir, 'tweets.yaml') tweets_config = yaml.safe_load(open(config_path)) templates = tweets_config.get('templates', {}) default_template = templates.get('default', TEMPLATE) # Read and process results cache_path = os.path.join(bot.config_dir, 'tweets.cache') with dbm.open(cache_path, 'c') as tweet_cache: for user, entries in json.loads(results).items(): for entry in entries: status = entry['status'] channels = entry['channels'] status_key = entry['status_key'] status_id = entry['status_id'] link = yield shorten_url(entry['link']) # Send each entry to the appropriate channel for channel in channels: template = templates.get(channel, default_template) message = bot.format_text(template, user=user, status=status, link=link) bot.send_message(message, channel=channel) # Mark entry as delivered bot.logger.info('Delivered %s from %s to %s', status, user, ', '.join(channels)) tweet_cache['since_id'] = str( max(int(tweet_cache['since_id']), status_id)) tweet_cache[status_key] = str(time.time())
def command(bot, nick, message, channel, query=None): params = {'q': query} url = GOOGLE_URL + '?' + urlencode(params) client = tornado.httpclient.AsyncHTTPClient() result = yield tornado.gen.Task(client.fetch, url) try: matches = re.findall( b'/url\?q=([^&]*)[^>]*><div class="[^"]+">([^<]+)</div', result.body) url = yield shorten_url(unquote(matches[0][0].decode())) title = unquote(matches[0][1].decode()) response = bot.format_text( '{bold}{title}{bold} @ {color}{blue}{url}{color}', title=title, url=url) except (IndexError, ValueError) as e: bot.logger.warn(e) response = 'No results' bot.send_response(response, nick, channel)
def command(bot, nick, message, channel, url): response = yield shorten_url(url) if response != url: bot.send_response(response, nick, channel)