def __init__(self): self.config = Config("servrhe.json", { # Bot config "password": "", "channels": ["#commie-subs","#commie-staff"], "notifies": {}, "premux_dir": "", "mad": False, "markov_banned": ["c","k","kb","sync","op","deop","protect","deprotect","ban","unban"], # Release config "rip_host": "", "cr_user": "", "cr_pass": "", "funi_user": "", "funi_pass": "", "ftp_host": "", "ftp_port": 21, "ftp_user": "", "ftp_pass": "", "ftp_encode_dir": "", "xdcc_host": "", "xdcc_port": 21, "xdcc_user": "", "xdcc_pass": "", "xdcc_folder": "", "seed_host": "", "seed_port": 21, "seed_user": "", "seed_pass": "", "seed_file_folder": "", "seed_torrent_folder": "", "nyaa_user": "", "nyaa_pass": "", "tt_user": "", "tt_pass": "", "blog_user": "", "blog_pass": "", "mal_user": "", "mal_pass": "", # Showtime config "key": "", "base": "http://commie.milkteafuzz.com/st", "positions": ["translator","editor","typesetter","timer","encoding"], # Topic config "topic": ["☭ Commie Subs ☭",20,20.56], # Database "db_host": "", "db_port": "", "db_database": "" }) self.pluginmanager = PluginManager("commands") self.db = getattr(txmongo.lazyMongoConnectionPool(self.config.db_host, self.config.db_port, pool_size=3), self.config.db_database) self.alias = Aliases("alias.json") self.markov = Markov(self.db, self.alias) self.crunchy = Crunchyroll("crunchyroll.json", self.config, self.broadcast) self.funi = Funimation("funimation.json", self.config, self.broadcast) reactor.addSystemEventTrigger("before", "shutdown", self.shutdown) t = task.LoopingCall(self.refresh_shows) t.start(5*60) # 5 minutes n = task.LoopingCall(self.check_notifies) n.start(30) # Every 30 seconds
class ServrheFactory(protocol.ReconnectingClientFactory): # Protocol config protocol = Servrhe maxDelay = 5 * 60 # 5 minutes protocols = [] shows = {} def __init__(self): self.config = Config("servrhe.json", { # Bot config "password": "", "channels": ["#commie-subs","#commie-staff"], "notifies": {}, "premux_dir": "", "mad": False, "markov_banned": ["c","k","kb","sync","op","deop","protect","deprotect","ban","unban"], # Release config "rip_host": "", "cr_user": "", "cr_pass": "", "funi_user": "", "funi_pass": "", "ftp_host": "", "ftp_port": 21, "ftp_user": "", "ftp_pass": "", "ftp_encode_dir": "", "xdcc_host": "", "xdcc_port": 21, "xdcc_user": "", "xdcc_pass": "", "xdcc_folder": "", "seed_host": "", "seed_port": 21, "seed_user": "", "seed_pass": "", "seed_file_folder": "", "seed_torrent_folder": "", "nyaa_user": "", "nyaa_pass": "", "tt_user": "", "tt_pass": "", "blog_user": "", "blog_pass": "", "mal_user": "", "mal_pass": "", # Showtime config "key": "", "base": "http://commie.milkteafuzz.com/st", "positions": ["translator","editor","typesetter","timer","encoding"], # Topic config "topic": ["☭ Commie Subs ☭",20,20.56], # Database "db_host": "", "db_port": "", "db_database": "" }) self.pluginmanager = PluginManager("commands") self.db = getattr(txmongo.lazyMongoConnectionPool(self.config.db_host, self.config.db_port, pool_size=3), self.config.db_database) self.alias = Aliases("alias.json") self.markov = Markov(self.db, self.alias) self.crunchy = Crunchyroll("crunchyroll.json", self.config, self.broadcast) self.funi = Funimation("funimation.json", self.config, self.broadcast) reactor.addSystemEventTrigger("before", "shutdown", self.shutdown) t = task.LoopingCall(self.refresh_shows) t.start(5*60) # 5 minutes n = task.LoopingCall(self.check_notifies) n.start(30) # Every 30 seconds def broadcast(self, message): if self.protocols: self.protocols[0].msg("#commie-staff", message) def load(self, *params, **kwargs): url = "/".join([self.config.base]+[str(x) for x in params]) url = urllib.quote(url.encode("utf-8","ignore"),"/:") headers = {} data = "" if "data" in kwargs: d = kwargs["data"] d["key"] = self.config.key data = json.dumps(d) headers["Content-Type"] = ["application/json"] d = fetchPage(url, data, headers) d.addCallback(json.loads) return d @defer.inlineCallbacks def refresh_shows(self): data = yield self.load("shows") if "status" in data and not data["status"]: self.broadcast(data["message"]) return data = data["results"] for show in data: self.shows[show["id"]] = show def resolve(self, show, channel): matches = [] if not show: self.protocols[0].msg(channel, "Show name not specified.") return None for s in self.shows.itervalues(): if (s["series"].lower() == show.lower() or s["abbr"].lower() == show.lower()): return s if s["series"].lower().count(show.lower()): matches.append(s) if len(matches) > 1: r = [s["series"] for s in matches] if len(r) > 5: extra = "and {:d} more.".format(len(r) - 5) r = r[:5] + [extra] self.protocols[0].msg(channel, "Show name not specific, found: %s" % ", ".join(r)) return None elif not matches: self.protocols[0].msg(channel, "Show name not found.") return None return matches[0] @defer.inlineCallbacks def update_topic(self): shows = yield self.load("shows","current_episodes") shows = [(s["abbr"], s["current_ep"], s["last_release"]) for s in shows["results"]] shows.sort(key=lambda x: x[2], reverse=True) shows = ", ".join(["{} {:d}".format(s[0],s[1]) for s in shows[:self.config.topic[1]]]) topic = " || ".join([self.config.topic[0], shows, "Mahoyo progress: {:0.2f}%".format(self.config.topic[2])] + self.config.topic[3:]) self.protocols[0].topic("#commie-subs", normalize(topic)) def check_notifies(self): dt = datetime.datetime now = dt.utcnow() shows = [] for show in self.shows.itervalues(): if show["current_ep"] == show["total_eps"]: continue diff = dt.utcfromtimestamp(show["airtime"]) - now if 0 <= diff.total_seconds() < 30: shows.append(show["id"]) for id in shows: ping = [] if id in self.config.notifies: ping.extend(self.config.notifies[id].keys()) for k, v in self.config.notifies[id].items(): if v > 0: if v == 1: del self.config.notifies[id][k] else: self.config.notifies[id][k] = v - 1 if "*" in self.config.notifies: ping.extend(self.config.notifies["*"].keys()) for k, v in self.config.notifies["*"].items(): if v > 0: if v == 1: del self.config.notifies["*"][k] else: self.config.notifies["*"][k] = v - 1 if ping: show = self.shows[id] self.broadcast("{}: Episode {:d} of {} has aired on {}".format(", ".join(ping), show["current_ep"]+1, show["series"], show["channel"])) def shutdown(self): self.config.save() self.alias.save() self.funi.save() self.crunchy.save()