diff --git a/pastepwn/actions/__init__.py b/pastepwn/actions/__init__.py index da99b54..cad74ae 100644 --- a/pastepwn/actions/__init__.py +++ b/pastepwn/actions/__init__.py @@ -7,5 +7,15 @@ from .genericaction import GenericAction from .databaseaction import DatabaseAction from .savejsonaction import SaveJSONAction +from .twitteraction import TwitterAction -__all__ = ('BasicAction', 'SaveFileAction', 'TelegramAction', 'LogAction', 'GenericAction', 'DatabaseAction', 'SaveJSONAction') +__all__ = ( + "BasicAction", + "SaveFileAction", + "TelegramAction", + "LogAction", + "GenericAction", + "DatabaseAction", + "SaveJSONAction", + "TwitterAction", +) diff --git a/pastepwn/actions/twitteraction.py b/pastepwn/actions/twitteraction.py new file mode 100644 index 0000000..027ae3a --- /dev/null +++ b/pastepwn/actions/twitteraction.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +import logging +from string import Template + +import twitter + +from pastepwn.util import DictWrapper +from .basicaction import BasicAction + + +class TwitterAction(BasicAction): + """Action to tweet a message to a given account""" + + name = "TwitterAction" + + def __init__( + self, + consumer_key=None, + consumer_secret=None, + access_token_key=None, + access_token_secret=None, + template=None, + ): + super().__init__() + + self.logger = logging.getLogger(__name__) + + self.twitter_api = twitter.Api( + consumer_key=consumer_key, + consumer_secret=consumer_secret, + access_token_key=access_token_key, + access_token_secret=access_token_secret, + ) + + if template is not None: + self.template = Template(template) + else: + self.template = None + + def perform(self, paste, analyzer_name=None): + """Tweet a message""" + + if self.template is None: + text = "New paste matched by analyzer '{0}' - Link: {1}".format( + analyzer_name, paste.full_url + ) + else: + paste_dict = paste.to_dict() + paste_dict["analyzer_name"] = analyzer_name + text = self.template.safe_substitute(DictWrapper(paste_dict)) + + self.twitter_api.PostUpdate(text) diff --git a/requirements.txt b/requirements.txt index 15cbeaf..dab93a2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pymongo mysql-connector-python requests +python-twitter \ No newline at end of file