From ac635d5a394faa072d483b75be58ea2bb7dd486d Mon Sep 17 00:00:00 2001 From: Petter Date: Sat, 15 Aug 2015 22:10:30 +0200 Subject: [PATCH] couchbot release --- README.md | 4 +- couchbot/__init__.py | 91 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++ setup.py | 25 ++++++++++++ 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 couchbot/__init__.py create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/README.md b/README.md index cccc939..0919f00 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# couchbot -A Slack bot for Couch Potato notifications +# CouchBot +A Slack bot for queuing movies to Couch Potato diff --git a/couchbot/__init__.py b/couchbot/__init__.py new file mode 100644 index 0000000..0cba5b5 --- /dev/null +++ b/couchbot/__init__.py @@ -0,0 +1,91 @@ +import argparse +import re +import requests +from slackclient import SlackClient + + +def parse_args(): + global bot_token, couch_key, couch_url + parser = argparse.ArgumentParser() + parser.add_argument("bot_token", help="Slack bot API token", + type=str) + parser.add_argument("couch_key", help="Couch Potato API key", + type=str) + parser.add_argument("couch_url", help="Couch Potato server URL", + type=str) + + args = parser.parse_args() + bot_token = args.bot_token + couch_key = args.couch_key + couch_url = args.couch_url + + +def send_message_to_channel(msg, channel, sc): + sc.rtm_send_message(channel, msg) + + +def prepare_couch_data(messages, bot_mention): + messenger_data = {'imdb_ids': []} + for msg in messages: + if 'subtype' in msg: + continue + + if msg.get('type') == "message": + raw = msg['text'] + messenger_data['user'] = '<@{}>'.format(msg['user']) + messenger_data['channel'] = msg['channel'] + + if bot_mention in raw: + ids = set(re.findall(r'imdb.com/title/(.+?)/', raw)) + messenger_data['imdb_ids'] = ids + + return messenger_data + + +def tell_couch_potato(slack_data, sc): + for imdb_id in slack_data['imdb_ids']: + post_data = { + 'identifier': imdb_id + } + url = '{}api/{}/'.format(couch_url, couch_key) + url = '{}{}'.format(url, 'movie.add/') + response = requests.post(url, post_data).json() + if response['success']: + msg = '{} added movie: {}'.format( + slack_data['user'], + response['movie']['info']['original_title'] + ) + send_message_to_channel(msg, slack_data['channel'], sc) + else: + link = 'http://www.imdb.com/title/{}'.format(imdb_id) + send_message_to_channel( + 'Failed to add movie {}'.format(link), + slack_data['channel'], + sc + ) + + +def start(): + global bot_token + sc = SlackClient(bot_token) + + if sc.rtm_connect(): + bot_id = sc.server.users[0].id + bot_mention = '<@{}>'.format(bot_id) + while True: + rtm = sc.rtm_read() + if len(rtm) > 0: + slack_data = prepare_couch_data(rtm, bot_mention) + if len(slack_data['imdb_ids']) > 0: + tell_couch_potato(slack_data, sc) + else: + print("Connection Failed, invalid token") + + +def main(): + parse_args() + start() + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..217e4e4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +requests==2.7.0 +six==1.9.0 +-e git+git@github.com:slackhq/python-slackclient.git@ba71b24603f63e54e704d0481812efcd9f7b8c14#egg=slackclient-master +websocket-client==0.32.0 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..ad6b068 --- /dev/null +++ b/setup.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +from setuptools import setup, find_packages + + +setup( + name='couchbot', + version='0.1.0', + author='Petter Friberg', + author_email='petter_friberg@hotmail.com', + description='A Slack bot for queuing movies to Couch Potato', + packages=find_packages(), + zip_safe=False, + install_requires=[], + license='MIT', + include_package_data=True, + url='https://github.com/flaeppe/couchbot', + entry_points={ + 'console_scripts': [ + 'couchbot = couchbot:main', + ], + }, + classifiers=[ + 'Topic :: Utilities' + ], +) \ No newline at end of file