Esempio n. 1
0
 def testProcess(self):
     self.start_test_server()
     message_id = SentryClient().process(message='hello')
     self.assertTrue(message_id)
     instance = Message.objects.all().order_by('-id')[0]
     self.assertEquals(instance.message, 'hello')
     self.stop_test_server()
Esempio n. 2
0
    def fetch_new_content(self, **options):
        update_horizon = datetime.utcnow() - timedelta(minutes=15)
        last_viewed_horizon = datetime.utcnow() - timedelta(days=5)

        users = User.objects.all()
        for user in users:
            try:
                person = user.person
            except Person.DoesNotExist:
                continue

            # Don't update accounts if someone hasn't viewed the site in some days.
            if person.last_viewed_home < last_viewed_horizon:
                logging.getLogger(__name__).debug(
                    "User %s hasn't viewed the site in a while; skipping",
                    user.username)
                continue

            for account in person.accounts.all():
                log = logging.getLogger('%s.%s' % (__name__, account.service))

                try:
                    poller = pollers[account.service]
                except KeyError:
                    log.debug("Account service %s has no poller, skipping",
                              account.service)
                    continue

                if options['service'] and account.service != options['service']:
                    log.debug(
                        "Account is for service %s but we're only polling %s, skipping",
                        account.service, options['service'])
                    continue

                if not options[
                        'force'] and account.last_updated > update_horizon:
                    log.debug(
                        "Account %s %s was updated fewer than 15 minutes ago, skipping",
                        account.service, account.display_name)
                    continue

                # Mark the account as updated even if the update fails later.
                log.debug("Polling account %s %s", account.service,
                          account.display_name)
                account.last_updated = datetime.utcnow()
                account.save()

                try:
                    poller(account)
                except Exception, exc:
                    log.exception(exc)
                    SentryClient().create_from_exception(
                        view='%s.%s' % (__name__, account.service))
                else:
                    account.last_success = datetime.utcnow()
                    account.save()
Esempio n. 3
0
    def testTimestamp(self):
        timestamp = datetime.datetime.now() - datetime.timedelta(hours=1)

        self.start_test_server()
        message_id = SentryClient().process(message='hello', timestamp=timestamp)
        self.stop_test_server()

        self.assertTrue(message_id)
        instance = Message.objects.all().order_by('-id')[0]
        self.assertEquals(instance.message, 'hello')
        self.assertEquals(instance.datetime, timestamp)
        group = instance.group
        self.assertEquals(group.first_seen, timestamp)
        self.assertEquals(group.last_seen, timestamp)
Esempio n. 4
0
def send(data):
    return SentryClient().send(**data)
Esempio n. 5
0
 def handle_noargs(self, **options):
     try:
         self.fetch_new_content(**options)
     except Exception, exc:
         logging.exception(exc)
         SentryClient().create_from_exception(view=__name__)
Esempio n. 6
0
            log.debug("Creating a UserStream row for object %r which is a %s by %r", obj, why_verb, actor)
            streamitem, created = UserStream.objects.get_or_create(user=user, obj=obj,
                defaults={'why_account': actor, 'why_verb': why_verb, 'time': orig_obj.time})

            # Now walk up again creating UserReplyStream rows as necessary
            reply_obj = orig_obj
            while reply_obj.in_reply_to is not None:
                UserReplyStream.objects.get_or_create(user=user, root=obj, reply=reply_obj,
                    defaults={'root_time': streamitem.time, 'reply_time': reply_obj.time})
                reply_obj = reply_obj.in_reply_to


        except Exception, exc:
            from sentry.client.base import SentryClient
            SentryClient().create_from_exception(view=__name__)


def account_for_facebook_user(fb_user, person=None):
    try:
        account = Account.objects.get(service='facebook.com', ident=fb_user["id"])
    except Account.DoesNotExist:
        if person is None:
            avatar = Media(
                width=50,
                height=50,
                image_url='http://graph.facebook.com/%s/picture' % fb_user["id"]
            )
            avatar.save()
            person = Person(
                display_name=fb_user["name"],
Esempio n. 7
0
def poll_twitter(account):
    user = account.person.user
    if user is None:
        return

    authtoken = account.authinfo
    if not authtoken:
        return

    # Get that twitter user's home timeline.
    csr = oauth.Consumer(*settings.TWITTER_CONSUMER)
    token = oauth.Token(*authtoken.split(':', 1))
    client = oauth.Client(csr, token)
    try:
        resp, content = client.request(
            'http://api.twitter.com/1/statuses/home_timeline.json?include_entities=true&count=50',
            'GET')
    except httplib.IncompleteRead:
        log.info(
            "Twitter returned an incomplete response asking for %s's feed",
            account.ident)
        return
    if resp.status in (500, 502, 503):
        # Can't get Twitter results right now. Let's try again later.
        log.info(
            "Twitter returned a server error status %d asking for %s's feed (Twitter's down?)",
            resp.status, account.ident)
        return
    if resp.status == 401:
        # The token may be invalid. Have we successfully scanned this account recently?
        if account.last_success > datetime.utcnow() - timedelta(days=2):
            raise ValueError(
                "Token for Twitter user %s came back as invalid (possibly temporary)"
                % account.ident)
        # The token is now invalid (maybe they revoked the app). Stop updating this account.
        account.authinfo = ''
        account.save()
        raise ValueError(
            "Token for Twitter user %s came back as invalid (probably permanent, so deleted authinfo)"
            % account.ident)
    if resp.status != 200:
        raise ValueError(
            "Unexpected %d %s response fetching %s's twitter timeline" %
            (resp.status, resp.reason, account.ident))

    tl = json.loads(content)

    for orig_tweetdata in reversed(tl):
        try:
            # TODO: filter based on source?

            tweetdata = orig_tweetdata
            why_verb = 'post'
            try:
                tweetdata = orig_tweetdata['retweeted_status']
            except KeyError:
                pass
            else:
                why_verb = 'share'

            really_a_share, tweet = raw_object_for_tweet(tweetdata, client)
            if tweet is None:
                continue

            if really_a_share:
                why_verb = 'share'

            if why_verb == 'share':
                why_account = account_for_twitter_user(orig_tweetdata['user'])
            else:
                why_account = tweet.author

            # CASES:
            # real reply to...
            # real retweet of...
            # tweet with just a link
            # tweet with a link and custom text
            # tweet with a link and the link's target page title (found how?)

            if why_verb == 'post' and tweet.in_reply_to is not None:
                why_verb = 'reply'

            root = tweet
            while root.in_reply_to is not None:
                log.debug('Walking up from %r to %r', root, root.in_reply_to)
                root = root.in_reply_to

            streamitem, created = UserStream.objects.get_or_create(
                user=user,
                obj=root,
                # TODO: is tweet.time the right time here or do we need the "why time" from orig_tweetdata?
                defaults={
                    'why_account': why_account,
                    'why_verb': why_verb,
                    'time': tweet.time
                })

            # Now add a reply for each tweet in the thread along the way.
            supertweet = tweet
            while supertweet.in_reply_to is not None:
                UserReplyStream.objects.get_or_create(user=user,
                                                      root=root,
                                                      reply=supertweet,
                                                      defaults={
                                                          'root_time':
                                                          streamitem.time,
                                                          'reply_time':
                                                          supertweet.time
                                                      })
                supertweet = supertweet.in_reply_to

        except Exception, exc:
            from sentry.client.base import SentryClient
            SentryClient().create_from_exception(view=__name__)