Skip to content

Commit

Permalink
couchbot release
Browse files Browse the repository at this point in the history
  • Loading branch information
flaeppe committed Aug 15, 2015
1 parent 7d6191e commit ac635d5
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# couchbot
A Slack bot for Couch Potato notifications
# CouchBot
A Slack bot for queuing movies to Couch Potato
91 changes: 91 additions & 0 deletions couchbot/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
requests==2.7.0
six==1.9.0
-e [email protected]:slackhq/python-slackclient.git@ba71b24603f63e54e704d0481812efcd9f7b8c14#egg=slackclient-master
websocket-client==0.32.0
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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='[email protected]',
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'
],
)

0 comments on commit ac635d5

Please sign in to comment.