Esempio n. 1
0
    def facebook(self):
        """Send messge link to facebook"""
        logentries = Logs.objects.filter(news_id=self.id,source='F', error=False).all()
        if len(logentries) > 0:
            return True
        try:
            token = FacebookToken.objects.get(id=1)
        except FacebookToken.DoesNotExist:
            addLogEntry(self, _('Facebook token not found, cannot send message to Facebook!'),
                        error=True, source='F')
            return False

        # Kapsi sorturl service
        link = "http://kap.si/t/%s" % from4id(self.publishid)
        # Unexist picture denies facebook to fetching site favicon.
        message = render_to_string('news/social_media.txt', {
                'subject' : self.subject, 'link' : link})
        payload = {
            'access_token' : token.token,
            'message' : message,
             'link' : link,
            'picture' : 'http://www.example.com/kuvat/tatakuvaaeioleoleassa.jpg'
        }
        retval = requests.post("https://graph.facebook.com/%s/feed" % token.userid,
                                data=payload)
        logger.debug(retval.text)
        retval = loads(retval.text)
        if 'error' in retval:
            addLogEntry(self, _('Failed to send message to Facebook!'),
                        error=True, source='F')
            addLogEntry(self, retval['error']['message'], error=True, source='F')
        else:
            addLogEntry(self, _('Message sent successfully to Facebook!'),
                        error=False, source='F')
        return True
Esempio n. 2
0
 def tweet(self):
     # https://dev.twitter.com/docs/api/1/post/statuses/update
     # raise NotImplementedError
     if TWITTER_CONSUMER_SECRET == '':
         return False
     logentries = Logs.objects.filter(news_id=self.id,source='T', error=False).all()
     if len(logentries) > 0:
         return True
     try:
         token = TwitterToken.objects.order_by('-id')[0:1].get()
         access_token = token.token
         access_secret = token.secret
     except TwitterToken.DoesNotExist:
         addLogEntry(self, _('Twitter token not found, cannot send message to Twitter!'),
                     source='T', error=True)
         return False
     link = "http://kap.si/t/%s" % from4id(self.publishid)
     OAuthHook.consumer_key = TWITTER_CONSUMER_KEY
     OAuthHook.consumer_secret = TWITTER_CONSUMER_SECRET
     oauth_hook = OAuthHook(access_token=access_token,
                     access_token_secret=access_secret, header_auth=True)
     message = render_to_string('news/social_media.txt', {
         'subject' : self.subject, 'link' : link})
     payload = {'status' : message, 'include_entities' : 'true'}
     #retval = requests.post("https://api.twitter.com/1/statuses/update.json",
     #                     data=payload, hooks={'pre_request_oauth': oauth_hook})
     request = requests.Request('POST', 
                                 "https://api.twitter.com/1/statuses/update.json",
                                 data=payload)
     request = oauth_hook(request)
     prepared = request.prepare()
     session = requests.session()
     retval = session.send(prepared)
     logger.debug(retval.text)
     retval = loads(retval.text)
     if 'id' in retval:
         addLogEntry(self, _('Message sent successfully to Twitter!'),
                     error=False, source='T')
         return
     addLogEntry(self, _('Failed to send message to Twitter!'),
                 error=True, source='T')
     if 'error' in retval:
         addLogEntry(self, retval['error'], error=True, source='T')