def postTwitter(tweet):
    try:
        twitter = Twython(CUSTOMER_KEY, CUSTOMER_SECRET, ACCESS_TOKEN,
                          ACCESS_TOKEN_SECRET)
        twitter.update_status(status=tweet)
        return 'Tweet posted!'
    except Exception as e:
        print e
        return 'Check your internet connection.'
Пример #2
0
class TwitterSingleton(metaclass=Singleton):
    twitter = None

    def __init__(self):
        if settings.DEBUG:
            self.twitter = TwitterDebugLogger()
        else:
            self.twitter = Twython(settings.CONSUMER_KEY,
                                   settings.CONSUMER_SECRET,
                                   settings.OAUTH_TOKEN,
                                   settings.OAUTH_TOKEN_SECRET)

    def status_update_hooks(self):
        pass

    def tweet(self, status, *args, **kwargs):
        try:
            self.twitter.update_status(status=status, *args, **kwargs)
            self.status_update_hooks()
        except TwythonError as e:
            log("Twython error, tweeting: {0}".format(e))

    def reply_to(self, tweet_data, status, *args, **kwargs):
        status = '@{0} {1}'.format(tweet_data['user']['screen_name'], status)
        self.tweet(status=status,
                   in_reply_to_status_id=tweet_data['id_str'],
                   *args,
                   **kwargs)

    def update_status_with_media(self, status, media, in_reply_to_status_id):
        '''

        :param status:
        :param media: is simply the un-uploaded media
        :param in_reply_to:
        :return:
        '''

        try:
            log('blah blaaeouaoeuh')
            response = self.twitter.upload_media(media=media)
            log('blah blah')
            self.twitter.update_status(
                status=status,
                media_ids=[response['media_id']],
                in_reply_to_status_id=in_reply_to_status_id)
            self.status_update_hooks()
        except TwythonError as e:
            log("Twython error, updating status with media: {0}".format(e))

    def send_direct_message(self, text, user_id=None, screen_name=None):
        if user_id:
            self.twitter.send_direct_message(text=text, user_id=user_id)
        elif screen_name:
            self.twitter.send_direct_message(text=text,
                                             screen_name=screen_name)
Пример #3
0
class TwitterSingleton(metaclass=Singleton):
    twitter = None

    def __init__(self):
        if settings.DEBUG:
            self.twitter = TwitterDebugLogger()
        else:
            self.twitter = Twython(
                settings.CONSUMER_KEY,
                settings.CONSUMER_SECRET,
                settings.OAUTH_TOKEN,
                settings.OAUTH_TOKEN_SECRET
            )

    def status_update_hooks(self):
        pass

    def tweet(self, status, *args, **kwargs):
        try:
            self.twitter.update_status(status=status, *args, **kwargs)
            self.status_update_hooks()
        except TwythonError as e:
            log("Twython error, tweeting: {0}".format(e))

    def reply_to(self, tweet_data, status, *args, **kwargs):
        status = '@{0} {1}'.format(tweet_data['user']['screen_name'], status)
        self.tweet(status=status, in_reply_to_status_id=tweet_data['id_str'], *args, **kwargs)

    def update_status_with_media(self, status, media, in_reply_to_status_id):
        '''

        :param status:
        :param media: is simply the un-uploaded media
        :param in_reply_to:
        :return:
        '''

        try:
            log('blah blaaeouaoeuh')
            response = self.twitter.upload_media(media=media)
            log('blah blah')
            self.twitter.update_status(status=status, media_ids=[response['media_id']],
                                       in_reply_to_status_id=in_reply_to_status_id)
            self.status_update_hooks()
        except TwythonError as e:
            log("Twython error, updating status with media: {0}".format(e))

    def send_direct_message(self, text, user_id=None, screen_name=None):
        if user_id:
            self.twitter.send_direct_message(text=text, user_id=user_id)
        elif screen_name:
            self.twitter.send_direct_message(text=text, screen_name=screen_name)
Пример #4
0
def sendDefinitiveMessage(request, apk):
    
    if 'OAUTH_TOKEN' not in request.session or 'OAUTH_TOKEN_SECRET' not in request.session:
        return twitter_connect(request, apk)

    twitter = Twython(settings.TWITTER_API_KEY, settings.TWITTER_API_SECRET,
                      request.session['OAUTH_TOKEN'], request.session['OAUTH_TOKEN_SECRET'])

    logger.error(twitter);

    activity = Activity.objects.get(pk=apk )        
    
    twitter.update_status( status=  'Activity  ' + activity.name + ' is now definitive '
             ', activity starts at ' + activity.startDate.strftime("%d.%m.%Y")  + ' in ' + activity.location )
    
    return None
Пример #5
0
class TwitterAdapter(MessageProvider, ImageProvider):
    id = 'twitter'
    name = 'TwitterAdapter'

    def __init__(self, user, *args, **kwargs):
        self.user = user
        self.social_token = SocialToken.objects.filter(app__provider='twitter',
                                                       account__provider='twitter',
                                                       account__user=user)
        self.social_app = SocialApp.objects.filter(id=self.social_token.get().app.id)
        self.twitter = Twython(self.social_app.get().client_id, self.social_app.get().secret,
                               self.social_token.get().token,
                               self.social_token.get().token_secret)
        self.twitter.verify_credentials()

    def publish_image(self, image, message='', **kwargs):
        img = open(image.path).read()
        try:
            logger.info('try to update twitter status with an image, for user: %s ' % self.user)
            result = self.twitter.update_status_with_media(status=message, media=StringIO(img))
            logger.debug(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_message(self, message, **kwargs):
        try:
            logger.info('try to update twitter status, for user: %s ' % self.user)
            result = self.twitter.update_status(status=message)
            logger.debug(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e
Пример #6
0
class TwitterSingleton(metaclass=Singleton):
    twitter = None

    def __init__(self):
        if settings.DEBUG:
            self.twitter = TwitterDebugLogger()
        else:
            self.twitter = Twython(
                settings.CONSUMER_KEY,
                settings.CONSUMER_SECRET,
                settings.OAUTH_TOKEN,
                settings.OAUTH_TOKEN_SECRET
            )

    def status_update_hooks(self):
        pass

    def tweet(self, status, *args, **kwargs):
        try:
            self.twitter.update_status(status=status, *args, **kwargs)
            self.status_update_hooks()
        except TwythonError as e:
            log("Twython error, tweeting: {0}".format(e))

    def reply_to(self, tweet_data, status, *args, **kwargs):
        status = '@{0} {1}'.format(tweet_data['user']['screen_name'], status)
        self.tweet(status=status, in_reply_to_status_id=tweet_data['id_str'], *args, **kwargs)

    def update_status_with_media(self, *args, **kwargs):
        try:
            self.twitter.update_status_with_media(*args, **kwargs)
            self.status_update_hooks()
        except TwythonError as e:
            log("Twython error, updating status with media: {0}".format(e))

    def send_direct_message(self, text, user_id=None, screen_name=None):
        if user_id:
            self.twitter.send_direct_message(text=text, user_id=user_id)
        elif screen_name:
            self.twitter.send_direct_message(text=text, screen_name=screen_name)
Пример #7
0
class TwitterAdapter(MessageProvider, ImageProvider, ActionMessageProvider):
    id = 'twitter'
    name = 'TwitterAdapter'

    def __init__(self, user, *args, **kwargs):
        self.user = user
        self.social_token = SocialToken.objects.filter(app__provider='twitter',
                                                       account__provider='twitter',
                                                       account__user=user)
        self.social_app = SocialApp.objects.filter(id=self.social_token.get().app.id)
        self.twitter = Twython(self.social_app.get().client_id, self.social_app.get().secret,
                               self.social_token.get().token,
                               self.social_token.get().token_secret)
        self.twitter.verify_credentials()

    def publish_image(self, image, message='', **kwargs):
        img = open(image.path).read()
        try:
            logger.info('try to update twitter status with an image, for user: %s ' % self.user)
            result = self.twitter.update_status_with_media(status=message, media=StringIO(img))
            logger.debug(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_message(self, message, **kwargs):
        """
            Attachment:
            {"name": "Link name"
             "link": "http://www.example.com/",
             "caption": "{*actor*} posted a new review",
             "description": "This is a longer description of the attachment",
             "picture": picture}
        """
        try:
            logger.info('try to update twitter status, for user: %s ' % self.user)
            attachment = kwargs.pop('attachment', {})
            message = message or attachment.get('caption', attachment.get('description', ''))
            full_message = "%s (%s)" % (message, attachment.get('link')) if 'link' in attachment else message
            if 'picture' in attachment:
                img = open(attachment.get('picture').path).read()
                result = self.twitter.update_status_with_media(status=full_message, media=StringIO(img))
            else:
                result = self.twitter.update_status(status=full_message)
            logger.debug(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_action_message(self, message, action_info, **kwargs):
        """
            action_info: dictionary with information about the corresponding app activity/action
            {
                 "link": the url for the app activity/action to point to,
                 "actor": the actor,
                 "action": the action performed by 'actor',
                 "verb": the verbal form to show for the action performed by 'actor',
                 "target": the target of the action,
                 "app": the application name,
                 "domain": the application domain
                 "picture": the picture to show
             }
        """
        try:
            logger.info('try to update twitter status, for user: %s ' % self.user)
            from django.utils.translation import ugettext as _

            message = message or "%s (%s) %s %s" % (
                action_info.get('action', ''),
                action_info.get('target', ''),
                _(u'using'),
                action_info.get('app', _(u'application'))
            ),
            full_message = "%s (%s)" % (message, action_info.get('link')) if 'link' in action_info else message
            if 'picture' in action_info:
                img = open(action_info.get('picture').path).read()
                result = self.twitter.update_status_with_media(status=full_message, media=StringIO(img))
            else:
                result = self.twitter.update_status(status=full_message)
            logger.debug(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e
Пример #8
0
class TwitterAdapter(MessageProvider, ImageProvider, ActionMessageProvider):
    id = 'twitter'
    name = 'TwitterAdapter'

    def __init__(self, user, *args, **kwargs):
        self.user = user
        self.social_token = SocialToken.objects.filter(
            app__provider='twitter',
            account__provider='twitter',
            account__user=user)
        self.social_app = SocialApp.objects.filter(
            id=self.social_token.get().app.id)
        self.twitter = Twython(self.social_app.get().client_id,
                               self.social_app.get().secret,
                               self.social_token.get().token,
                               self.social_token.get().token_secret)
        self.twitter.verify_credentials()

    def publish_image(self, image, message='', **kwargs):
        img = open(image.path).read()
        try:
            logger.info(
                'try to update twitter status with an image, for user: %s ' %
                self.user)
            result = self.twitter.update_status_with_media(status=message,
                                                           media=StringIO(img))
            logger.debug(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_message(self, message, **kwargs):
        """
            Attachment:
            {"name": "Link name"
             "link": "http://www.example.com/",
             "caption": "{*actor*} posted a new review",
             "description": "This is a longer description of the attachment",
             "picture": picture}
        """
        try:
            logger.info('try to update twitter status, for user: %s ' %
                        self.user)
            attachment = kwargs.pop('attachment', {})
            message = message or attachment.get(
                'caption', attachment.get('description', ''))
            full_message = "%s (%s)" % (message, attachment.get('link')
                                        ) if 'link' in attachment else message
            if 'picture' in attachment:
                img = open(attachment.get('picture').path).read()
                result = self.twitter.update_status_with_media(
                    status=full_message, media=StringIO(img))
            else:
                result = self.twitter.update_status(status=full_message)
            logger.debug(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_action_message(self, message, action_info, **kwargs):
        """
            action_info: dictionary with information about the corresponding app activity/action
            {
                 "link": the url for the app activity/action to point to,
                 "actor": the actor,
                 "action": the action performed by 'actor',
                 "verb": the verbal form to show for the action performed by 'actor',
                 "target": the target of the action,
                 "app": the application name,
                 "domain": the application domain
                 "picture": the picture to show
             }
        """
        try:
            logger.info('try to update twitter status, for user: %s ' %
                        self.user)
            from django.utils.translation import ugettext as _

            message = message or "%s (%s) %s %s" % (
                action_info.get('action', ''), action_info.get('target', ''),
                _(u'using'), action_info.get('app', _(u'application'))),
            full_message = "%s (%s)" % (message, action_info.get('link')
                                        ) if 'link' in action_info else message
            if 'picture' in action_info:
                img = open(action_info.get('picture').path).read()
                result = self.twitter.update_status_with_media(
                    status=full_message, media=StringIO(img))
            else:
                result = self.twitter.update_status(status=full_message)
            logger.debug(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e