class GitHubFacet(ThingFacet): name = "github" commands = command.thing.add_child(command.FacetCommandSet(name)) def __init__(self, thing_): ThingFacet.__init__(self, thing_) self.get_info = Cache(self._get_info, expire_seconds=10 * 60) @classmethod def does_attach(cls, thing): return False @commands.add(u"forget that {thing} is on github", help=u"unset {thing}'s github username", exclusive=True) def unset_github_username(self, thing, context): del self.data self.thing.detach_persistent(self) @commands.add(u"{thing} has github username {username}", help=u"set {thing}'s github username to {username}") def set_github_username(self, thing, username, context): self.username = username @property def username(self): if self.has_data and "username" in self.data: return self.data["username"] else: return self.thing.name @username.setter def set_username(self, value): if "username" not in self.data or value != self.data["username"]: self.data["username"] = value self.get_info.reset() def _get_info(self): about_url = "http://github.com/{0}.json" about = urllib.urlopen(about_url.format(self.username)) return json.load(about) @commands.add(u"{thing} commits", help=u"show the last 3 commits by {thing}") def get_github_commits(self, thing, context): info = self.get_info() pushes = filter(lambda x: x["type"] == "PushEvent", info) lines = [] for push in pushes[:3]: lines.append( u'"{last_commit_msg}" -- {url}'.format( url=push["repository"]["url"], last_commit_msg=push["payload"]["shas"][0][2] ) ) context.reply("\n".join(lines))
class TwitterFacet(thing.ThingFacet): name = "twitter" commands = command.thing.add_child(command.FacetCommandSet(name)) def __init__(self, thing_): thing.ThingFacet.__init__(self, thing_) self.get_info = Cache(self._get_info, expire_seconds=10*60) @classmethod def does_attach(cls, thing): return False @commands.add(u"forget that {thing} is on twitter", help=u"unset {thing}'s twitter username", exclusive=True) def unset_twitterer(self, thing, context): del self.data self.thing.detach_persistent(self) @commands.add(u"{thing} has twitter username {username}", help=u"set {thing}'s twitter username to {username}") def set_twitter_username(self, thing, username, context): self.username = username @property def username(self): if self.has_data and "username" in self.data: return self.data["username"] else: return self.thing.name @username.setter def username(self, value): if "username" not in self.data or value != self.data["username"]: self.data["username"] = value self.get_info.reset() def _get_info(self): about_url = "http://api.twitter.com/1/statuses/user_timeline/{0}.json" about = urllib.urlopen(about_url.format(self.username)) return json.load(about) def get_last_tweet(self): return unescape_html(self.get_info()[0]["text"])
class RedditorFacet(thing.ThingFacet): name = "redditor" commands = command.thing.add_child(command.FacetCommandSet(name)) def __init__(self, thing_): thing.ThingFacet.__init__(self, thing_) self.get_info = Cache(self._get_info, expire_seconds=10 * 60) @classmethod def does_attach(cls, thing): return False @commands.add(u"forget that {thing} is a redditor", help=u"unset {thing}'s reddit username", exclusive=True) def unset_redditor(self, thing, context): del self.data self.thing.detach_persistent(self) @commands.add(u"{thing} has reddit username {username}", help=u"set {thing}'s reddit username to {username}") def set_redditor_username(self, thing, username, context): self.username = username @property def username(self): if self.has_data and "username" in self.data: return self.data["username"] else: return self.thing.name @username.setter def username(self, value): if "username" not in self.data or value != self.data["username"]: self.data["username"] = value self.get_info.reset() def _get_info(self): about_url = "http://www.reddit.com/user/{0}/about.json" about = urllib.urlopen(about_url.format(self.username)) return json.load(about)["data"]
class TwitterFacet(Facet): name = "twitter" commands = command.thing.add_child(command.FacetCommandSet(name)) def __init__(self, thing_): super(self, Facet).__init__(self, thing_) self.get_info = Cache(self._get_info, expire_seconds=10*60) @classmethod def does_attach(cls, thing): return False @commands.add(u"forget that {thing} is on twitter", help=u"unset {thing}'s twitter username", exclusive=True) def unset_twitterer(self, thing, context): del self.data self.thing.remove_facet(self) @commands.add(u"{thing} has twitter username {username}", help=u"set {thing}'s twitter username to {username}") def set_twitter_username(self, thing, username, context): self.username = username @property def username(self): return self.data.get("username", self.thing.name) @username.setter def username(self, value): if "username" not in self.data or value != self.data["username"]: self.data["username"] = value self.get_info.reset() def _get_info(self): about_url = "http://api.twitter.com/1/statuses/user_timeline/{0}.json" about = urllib.urlopen(about_url.format(self.username)) return json.load(about) def get_last_tweet(self): return unescape_html(self.get_info()[0]["text"])
class RedditorFacet(Facet): name = "redditor" commands = command.thing.add_child(command.FacetCommandSet(name)) def __init__(self, thing_): super(self, Facet).__init__(self, thing_) self.get_info = Cache(self._get_info, expire_seconds=10 * 60) @classmethod def does_attach(cls, thing): return False @commands.add(u"forget that {thing} is a redditor", help=u"unset {thing}'s reddit username", exclusive=True) def unset_redditor(self, thing, context): del self.data self.thing.remove_facet(self) @commands.add(u"{thing} has reddit username {username}", help=u"set {thing}'s reddit username to {username}") def set_redditor_username(self, thing, username, context): self.username = username @property def username(self): return self.data.get("username", self.thing.name) @username.setter def username(self, value): if "username" not in self.data or value != self.data["username"]: self.data["username"] = value self.get_info.reset() def _get_info(self): about_url = "http://www.reddit.com/user/{0}/about.json" about = urllib.urlopen(about_url.format(self.username)) return json.load(about)["data"]
def __init__(self, thing_): super(self, Facet).__init__(self, thing_) self.get_info = Cache(self._get_info, expire_seconds=10 * 60)
def __init__(self, thing_): thing.ThingFacet.__init__(self, thing_) self.get_info = Cache(self._get_info, expire_seconds=10 * 60)