-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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='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' | ||
], | ||
) |