From 37a6571eee4311df91f61909955a04b87d652ef6 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Mon, 20 Jun 2016 11:42:10 +0200 Subject: [PATCH] First commit --- .gitignore | 3 ++ README.md | 23 ++++++++++++++ ampache2irc.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++ config.py.example | 11 +++++++ 4 files changed, 118 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 ampache2irc.py create mode 100644 config.py.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fe82f0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pyc +__pycache__ +config.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..a89327a --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +Ampache2IRC +=========== + +An IRC bot to post currently listened music on an Ampache server. + +## Installation + +* `git clone "https://github.com/Phyks/Ampache2IRC"` + +Then, in the cloned repository, + +* `pip install -r requirements.txt`. +* `cp config.py.example config.py` and edit it according to your needs. + + +## Usage + +As simple as `./ampache2irc.py`. + + +## License + +Code released under MIT license. diff --git a/ampache2irc.py b/ampache2irc.py new file mode 100755 index 0000000..2a0ab9c --- /dev/null +++ b/ampache2irc.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +""" +IRC bot to track currently played songs on Ampache. + +Code under MIT license. +""" +import ssl + +import feedparser +import irc.bot +import irc.connection + +import config + + +class Ampache2IRC(irc.bot.SingleServerIRCBot): + """ + Main bot class + """ + def __init__(self): + self.last_seen = None + if not config.use_ssl: + irc.bot.SingleServerIRCBot.__init__(self, [(config.server, + config.port)], + config.nick, + config.desc) + else: + self.ssl_factory = irc.connection.Factory(wrapper=ssl.wrap_socket) + irc.bot.SingleServerIRCBot.__init__( + self, [(config.server, config.port)], + config.nick, + config.desc, + connect_factory=self.ssl_factory) + + def refresh_feed(self, serv): + """Refresh Ampache played RSS feed""" + d = feedparser.parse("%s/rss.php?type=recently_played" % + (config.ampache_URL,)) + for entry in d.entries: + if(self.last_seen is None or + entry.published_parsed > self.last_seen): + serv.privmsg(config.channel, + "%s (%s)" % (entry.title, + (entry.comments + .replace(config.ampache_URL, "") + .strip("/")))) + self.last_seen = entry.published_parsed + + def on_welcome(self, serv, ev): + """Upon server connection, handles nickserv""" + if config.password != "": + serv.privmsg("nickserv", "identify %s" % (config.password,)) + serv.join(config.channel) + # Refresh feed every 10 seconds + self.connection.execute_every(10, self.refresh_feed, (serv,)) + + def on_privmsg(self, serv, ev): + """Handles queries""" + pass + + def on_pubmsg(self, serv, ev): + """Handles the messages on the chan""" + pass + + def close(self): + """Exits nicely""" + pass + + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() + + +if __name__ == '__main__': + try: + with Ampache2IRC() as bot: + bot.start() + except KeyboardInterrupt: + pass diff --git a/config.py.example b/config.py.example new file mode 100644 index 0000000..399dcf5 --- /dev/null +++ b/config.py.example @@ -0,0 +1,11 @@ +server = "" # IRC server to connect to +port = 6667 # Port to use +use_ssl = False # Use SSL for IRC connection? +channel = "" # Channel to join + +password = "" # Used for NickServ + +nick = "ampache_playing" # IRC Nickname +desc = "" # IRC Realname + +ampache_URL = "" # Base URL of Ampache without trailing slash