Ejemplo n.º 1
0
 def _set_data(self, data):
     if self.type == 4:              # json
         data = json.loads(data)
     elif self.type == 5:            # event
         data = json.loads(data)
         if 'name' not in data:
             raise ValueError("An event message must have a 'name' key.")
         if 'args' not in data:
             raise ValueError("An event message must have an 'args' key.")
         if data['name'] in RESERVED_EVENTS:
             msg = "That event name is reserved: %s." % data['name']
             raise ValueError(msg)
     self.__data = data
Ejemplo n.º 2
0
 def _set_data(self, data):
     if self.type == 4:  # json
         data = json.loads(data)
     elif self.type == 5:  # event
         data = json.loads(data)
         if 'name' not in data:
             raise ValueError("An event message must have a 'name' key.")
         if 'args' not in data:
             raise ValueError("An event message must have an 'args' key.")
         if data['name'] in RESERVED_EVENTS:
             msg = "That event name is reserved: %s." % data['name']
             raise ValueError(msg)
     self.__data = data
Ejemplo n.º 3
0
    def test_post_can_remove_member_from_team(self):
        response = self.client.POST('/team/members/alice.json', {'take': '0.01'}, auth_as='team')

        data = json.loads(response.body)
        assert len(data) == 2

        for rec in data:
            assert rec['username'] in ('team', 'alice'), rec['username']

        response = self.client.POST('/team/members/alice.json', {'take': '0.00'}, auth_as='team')
        data = json.loads(response.body)
        assert len(data) == 1
        assert data[0]['username'] == 'team'
Ejemplo n.º 4
0
def test_a_custom_renderer(harness):
    class TestRenderer(Renderer):

        def compile(self, *a):
            return self.raw.upper()

        def render_content(self, context):
            d = dict((k, v) for k, v in self.__dict__.items() if k[0] != '_')
            return json.dumps(d)

    class TestFactory(Factory):
        Renderer = TestRenderer

        def compile_meta(self, configuration):
            return 'foobar'

    website = harness.client.website
    website.renderer_factories['lorem'] = TestFactory(website)

    r = harness.simple("[---]\n[---] text/html via lorem\nLorem ipsum")
    d = json.loads(r.body)
    assert d['meta'] == 'foobar'
    assert d['raw'] == 'Lorem ipsum'
    assert d['media_type'] == 'text/html'
    assert d['offset'] == 2
    assert d['compiled'] == 'LOREM IPSUM'
Ejemplo n.º 5
0
def oauth_dance(website, qs):
    """Given a querystring, return a dict of user_info.

    The querystring should be the querystring that we get from GitHub when
    we send the user to the return value of oauth_url above.

    See also:

        http://developer.github.com/v3/oauth/

    """

    log("Doing an OAuth dance with Github.")

    data = {
        "code": qs["code"].encode("US-ASCII"),
        "client_id": website.github_client_id,
        "client_secret": website.github_client_secret,
    }
    r = requests.post("https://github.com/login/oauth/access_token", data=data)
    assert r.status_code == 200, (r.status_code, r.text)

    back = dict([pair.split("=") for pair in r.text.split("&")])  # XXX
    if "error" in back:
        raise Response(400, back["error"].encode("utf-8"))
    assert back.get("token_type", "") == "bearer", back
    access_token = back["access_token"]

    r = requests.get("https://api.github.com/user", headers={"Authorization": "token %s" % access_token})
    assert r.status_code == 200, (r.status_code, r.text)
    user_info = json.loads(r.text)
    log("Done with OAuth dance with Github for %s (%s)." % (user_info["login"], user_info["id"]))

    return user_info
Ejemplo n.º 6
0
 def test_participant_can_get_their_privacy_settings(self):
     response = self.hit_privacy('GET')
     actual = json.loads(response.body)
     assert actual == {
         'is_searchable': True,
         'anonymous_giving': False,
     }
Ejemplo n.º 7
0
def get_user_info(screen_name):
    """Given a unicode, return a dict.
    """
    typecheck(screen_name, unicode)
    try:
        rec = gittip.db.one( "SELECT user_info FROM elsewhere "
                             "WHERE platform='twitter' "
                             "AND user_info->'screen_name' = %s"
                           , (screen_name,)
                            )
    except TooFew:
        rec = None

    if rec is not None:
        user_info = rec['user_info']
    else:
        # Updated using Twython as a point of reference:
        # https://github.com/ryanmcgrath/twython/blob/master/twython/twython.py#L76
        oauth = OAuth1(
            # we do not have access to the website obj,
            # so let's grab the details from the env
            environ['TWITTER_CONSUMER_KEY'],
            environ['TWITTER_CONSUMER_SECRET'],
            environ['TWITTER_ACCESS_TOKEN'],
            environ['TWITTER_ACCESS_TOKEN_SECRET'],
        )

        url = "https://api.twitter.com/1.1/users/show.json?screen_name=%s"
        user_info = requests.get(url % screen_name, auth=oauth)

        # Keep an eye on our Twitter usage.
        # =================================

        rate_limit = user_info.headers['X-Rate-Limit-Limit']
        rate_limit_remaining = user_info.headers['X-Rate-Limit-Remaining']
        rate_limit_reset = user_info.headers['X-Rate-Limit-Reset']

        try:
            rate_limit = int(rate_limit)
            rate_limit_remaining = int(rate_limit_remaining)
            rate_limit_reset = int(rate_limit_reset)
        except (TypeError, ValueError):
            log( "Got weird rate headers from Twitter: %s %s %s"
               % (rate_limit, rate_limit_remaining, rate_limit_reset)
                )
        else:
            reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
            reset = to_age(reset)
            log( "Twitter API calls used: %d / %d. Resets %s."
               % (rate_limit - rate_limit_remaining, rate_limit, reset)
                )


        if user_info.status_code == 200:
            user_info = json.loads(user_info.text)
        else:
            log("Twitter lookup failed with %d." % user_info.status_code)
            raise Response(404)

    return user_info
Ejemplo n.º 8
0
def get_user_info(login):
    """Get the given user's information from the DB or failing that, github.

    :param login:
        A unicode string representing a username in github.

    :returns:
        A dictionary containing github specific information for the user.
    """
    typecheck(login, unicode)
    rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
                              "WHERE platform='github' "
                              "AND user_info->'login' = %s"
                            , (login,)
                             )
    if rec is not None:
        user_info = rec['user_info']
    else:
        url = "https://api.github.com/users/%s"
        user_info = requests.get(url % login)
        status = user_info.status_code
        content = user_info.text
        if status == 200:
            user_info = json.loads(content)
        elif status == 404:
            raise Response(404,
                           "GitHub identity '{0}' not found.".format(login))
        else:
            log("Github api responded with {0}: {1}".format(status, content),
                level=logging.WARNING)
            raise Response(502, "GitHub lookup failed with %d." % status)

    return user_info
Ejemplo n.º 9
0
    def test_paydays_json_gives_paydays(self):
        Payday.start()
        self.make_participant("alice")

        response = self.client.GET("/about/paydays.json")
        paydays = json.loads(response.body)
        assert paydays[0]['ntippers'] == 0
Ejemplo n.º 10
0
def get_user_info(username):
    """Get the given user's information from the DB or failing that, bitbucket.

    :param username:
        A unicode string representing a username in bitbucket.

    :returns:
        A dictionary containing bitbucket specific information for the user.
    """
    typecheck(username, unicode)
    rec = gittip.db.fetchone(
        "SELECT user_info FROM elsewhere "
        "WHERE platform='bitbucket' "
        "AND user_info->'username' = %s", (username, ))
    if rec is not None:
        user_info = rec['user_info']
    else:
        url = "%s/users/%s?pagelen=100"
        user_info = requests.get(url % (BASE_API_URL, username))
        status = user_info.status_code
        content = user_info.content
        if status == 200:
            user_info = json.loads(content)['user']
        elif status == 404:
            raise Response(
                404, "Bitbucket identity '{0}' not found.".format(username))
        else:
            log("Bitbucket api responded with {0}: {1}".format(
                status, content),
                level=logging.WARNING)
            raise Response(502, "Bitbucket lookup failed with %d." % status)

    return user_info
Ejemplo n.º 11
0
def get_user_info(login):
    """Get the given user's information from the DB or failing that, github.

    :param login:
        A unicode string representing a username in github.

    :returns:
        A dictionary containing github specific information for the user.
    """
    typecheck(login, unicode)
    rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
                              "WHERE platform='github' "
                              "AND user_info->'login' = %s"
                            , (login,)
                             )
    if rec is not None:
        user_info = rec['user_info']
    else:
        url = "https://api.github.com/users/%s"
        user_info = requests.get(url % login, params={
            'client_id': os.environ.get('GITHUB_CLIENT_ID'),
            'client_secret': os.environ.get('GITHUB_CLIENT_SECRET')
        })
        status = user_info.status_code
        content = user_info.text

        # Calculate how much of our ratelimit we have consumed
        remaining = int(user_info.headers['x-ratelimit-remaining'])
        limit = int(user_info.headers['x-ratelimit-limit'])
        # thanks to from __future__ import division this is a float
        percent_remaining = remaining/limit

        log_msg = ''
        log_lvl = None
        # We want anything 50% or over
        if 0.5 <= percent_remaining:
            log_msg = ("{0}% of GitHub's ratelimit has been consumed. {1}"
                       " requests remaining.").format(percent_remaining * 100,
                                                      remaining)
        if 0.5 <= percent_remaining < 0.8:
            log_lvl = logging.WARNING
        elif 0.8 <= percent_remaining < 0.95:
            log_lvl = logging.ERROR
        elif 0.95 <= percent_remaining:
            log_lvl = logging.CRITICAL

        if log_msg and log_lvl:
            log(log_msg, log_lvl)

        if status == 200:
            user_info = json.loads(content)
        elif status == 404:
            raise Response(404,
                           "GitHub identity '{0}' not found.".format(login))
        else:
            log("Github api responded with {0}: {1}".format(status, content),
                level=logging.WARNING)
            raise Response(502, "GitHub lookup failed with %d." % status)

    return user_info
Ejemplo n.º 12
0
 def test_participant_can_toggle_anonymous_receiving_back(self):
     response = self.hit_privacy('POST',
                                 data={'toggle': 'anonymous_receiving'})
     response = self.hit_privacy('POST',
                                 data={'toggle': 'anonymous_receiving'})
     actual = json.loads(response.body)['anonymous_receiving']
     assert actual is False
Ejemplo n.º 13
0
def get_user_info(db, username):
    """Get the given user's information from the DB or failing that, bitbucket.

    :param username:
        A unicode string representing a username in bitbucket.

    :returns:
        A dictionary containing bitbucket specific information for the user.
    """
    typecheck(username, (unicode, PathPart))
    rec = db.one(
        """
        SELECT user_info FROM elsewhere
        WHERE platform='bitbucket'
        AND user_info->'username' = %s
    """,
        (username,),
    )
    if rec is not None:
        user_info = rec
    else:
        url = "%s/users/%s?pagelen=100"
        user_info = requests.get(url % (BASE_API_URL, username))
        status = user_info.status_code
        content = user_info.content
        if status == 200:
            user_info = json.loads(content)["user"]
        elif status == 404:
            raise Response(404, "Bitbucket identity '{0}' not found.".format(username))
        else:
            log("Bitbucket api responded with {0}: {1}".format(status, content), level=logging.WARNING)
            raise Response(502, "Bitbucket lookup failed with %d." % status)

    return user_info
Ejemplo n.º 14
0
def travis_ci(first, second, color):
    first = "build"

    url = 'https://api.travis-ci.org/repos?slug=%s' % quote(second)
    fp = urlopen(url)
    repos = json.loads(fp.read())
    if repos:
        status = repos[0].get('last_build_status', 'n/a')
    else:
        status = 'n/a'

    second = {
        0: 'passing',
        1: 'failing',
        None: 'pending',
        'n/a': 'n/a'
    }.get(status, 'n/a')

    color = {
        'failing': RED,
        'passing': GREEN,
        'pending': YELLOW
    }.get(second, LIGHTGRAY)

    return first, second, color
Ejemplo n.º 15
0
    def test_paydays_json_gives_paydays(self):
        Payday.start()
        self.make_participant("alice")

        response = self.client.GET("/about/paydays.json")
        paydays = json.loads(response.body)
        assert paydays[0]['ntippers'] == 0
Ejemplo n.º 16
0
    def test_post_can_add_member_to_team(self):
        response = self.client.POST('/team/members/alice.json', {'take': '0.01'}, auth_as=self.team)
        data = json.loads(response.body)['members']
        assert len(data) == 2

        for rec in data:
            assert rec['username'] in ('team', 'alice'), rec['username']
Ejemplo n.º 17
0
 def test_participant_can_get_their_privacy_settings(self):
     response = self.hit_privacy('GET')
     actual = json.loads(response.body)
     assert actual == {
         'is_searchable': True,
         'anonymous_giving': False,
         'anonymous_receiving': False
     }
Ejemplo n.º 18
0
    def test_post_can_add_member_to_team(self):
        response = self.client.POST('/~team/members/alice.json',
                                    {'take': '0.01'},
                                    auth_as='team')
        data = json.loads(response.body)['members']
        assert len(data) == 2

        for rec in data:
            assert rec['username'] in ('team', 'alice'), rec['username']
Ejemplo n.º 19
0
    def test_get_team_members_returns_take_when_member(self):
        response = self.client.POST('/team/members/alice.json', {'take': '0.01'}, auth_as='team')
        assert response.code == 200

        response = self.client.GET('/team/members/alice.json', auth_as='team')
        data = json.loads(response.body)
        assert response.code == 200
        assert data['username'] == 'alice'
        assert data['take'] == '0.01'
Ejemplo n.º 20
0
def get_user_info(screen_name):
    """Given a unicode, return a dict.
    """
    typecheck(screen_name, (unicode, UnicodeWithParams))
    rec = gittip.db.one( "SELECT user_info FROM elsewhere "
                         "WHERE platform='twitter' "
                         "AND user_info->'screen_name' = %s"
                       , (screen_name,)
                        )

    if rec is not None:
        user_info = rec
    else:
        # Updated using Twython as a point of reference:
        # https://github.com/ryanmcgrath/twython/blob/master/twython/twython.py#L76
        oauth = OAuth1(
            # we do not have access to the website obj,
            # so let's grab the details from the env
            environ['TWITTER_CONSUMER_KEY'],
            environ['TWITTER_CONSUMER_SECRET'],
            environ['TWITTER_ACCESS_TOKEN'],
            environ['TWITTER_ACCESS_TOKEN_SECRET'],
        )

        url = "https://api.twitter.com/1.1/users/show.json?screen_name=%s"
        user_info = requests.get(url % screen_name, auth=oauth)

        # Keep an eye on our Twitter usage.
        # =================================

        rate_limit = user_info.headers['X-Rate-Limit-Limit']
        rate_limit_remaining = user_info.headers['X-Rate-Limit-Remaining']
        rate_limit_reset = user_info.headers['X-Rate-Limit-Reset']

        try:
            rate_limit = int(rate_limit)
            rate_limit_remaining = int(rate_limit_remaining)
            rate_limit_reset = int(rate_limit_reset)
        except (TypeError, ValueError):
            log( "Got weird rate headers from Twitter: %s %s %s"
               % (rate_limit, rate_limit_remaining, rate_limit_reset)
                )
        else:
            reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
            reset = to_age(reset)
            log( "Twitter API calls used: %d / %d. Resets %s."
               % (rate_limit - rate_limit_remaining, rate_limit, reset)
                )


        if user_info.status_code == 200:
            user_info = json.loads(user_info.text)
        else:
            log("Twitter lookup failed with %d." % user_info.status_code)
            raise Response(404)

    return user_info
Ejemplo n.º 21
0
def get_user_info(screen_name):
    """Given a unicode, return a dict.
    """
    typecheck(screen_name, unicode)
    rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
                              "WHERE platform='twitter' "
                              "AND user_info->'screen_name' = %s"
                            , (screen_name,)
                             )
    if rec is not None:
        user_info = rec['user_info']
    else:
        oauth = OAuthHook(
            # we haven't got access to the website obj,
            # so let's grab the details from the env
            access_token=environ['TWITTER_ACCESS_TOKEN'],
            access_token_secret=environ['TWITTER_ACCESS_TOKEN_SECRET'],
            consumer_key=environ['TWITTER_CONSUMER_KEY'],
            consumer_secret=environ['TWITTER_CONSUMER_SECRET'],
            header_auth=True
        )

        url = "https://api.twitter.com/1.1/users/show.json?screen_name=%s"
        user_info = requests.get(url % screen_name, hooks={'pre_request': oauth})

        # Keep an eye on our Twitter usage.
        # =================================

        rate_limit = user_info.headers['X-RateLimit-Limit']
        rate_limit_remaining = user_info.headers['X-RateLimit-Remaining']
        rate_limit_reset = user_info.headers['X-RateLimit-Reset']

        try:
            rate_limit = int(rate_limit)
            rate_limit_remaining = int(rate_limit_remaining)
            rate_limit_reset = int(rate_limit_reset)
        except (TypeError, ValueError):
            log( "Got weird rate headers from Twitter: %s %s %s"
               % (rate_limit, rate_limit_remaining, rate_limit_reset)
                )
        else:
            reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
            reset = to_age(reset)
            log( "Twitter API calls used: %d / %d. Resets %s."
               % (rate_limit - rate_limit_remaining, rate_limit, reset)
                )


        if user_info.status_code == 200:
            user_info = json.loads(user_info.text)
        else:
            log("Twitter lookup failed with %d." % user_info.status_code)
            raise Response(404)

    return user_info
Ejemplo n.º 22
0
def get_user_info(screen_name):
    """Given a unicode, return a dict.
    """
    typecheck(screen_name, unicode)
    rec = gittip.db.fetchone(
        "SELECT user_info FROM elsewhere "
        "WHERE platform='twitter' "
        "AND user_info->'screen_name' = %s", (screen_name, ))
    if rec is not None:
        user_info = rec['user_info']
    else:
        oauth = OAuthHook(
            # we haven't got access to the website obj,
            # so let's grab the details from the env
            access_token=environ['TWITTER_ACCESS_TOKEN'],
            access_token_secret=environ['TWITTER_ACCESS_TOKEN_SECRET'],
            consumer_key=environ['TWITTER_CONSUMER_KEY'],
            consumer_secret=environ['TWITTER_CONSUMER_SECRET'],
            header_auth=True)

        url = "https://api.twitter.com/1.1/users/show.json?screen_name=%s"
        user_info = requests.get(url % screen_name,
                                 hooks={'pre_request': oauth})

        # Keep an eye on our Twitter usage.
        # =================================

        rate_limit = user_info.headers['X-RateLimit-Limit']
        rate_limit_remaining = user_info.headers['X-RateLimit-Remaining']
        rate_limit_reset = user_info.headers['X-RateLimit-Reset']

        try:
            rate_limit = int(rate_limit)
            rate_limit_remaining = int(rate_limit_remaining)
            rate_limit_reset = int(rate_limit_reset)
        except (TypeError, ValueError):
            log("Got weird rate headers from Twitter: %s %s %s" %
                (rate_limit, rate_limit_remaining, rate_limit_reset))
        else:
            reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
            reset = to_age(reset)
            log("Twitter API calls used: %d / %d. Resets %s." %
                (rate_limit - rate_limit_remaining, rate_limit, reset))

        if user_info.status_code == 200:
            user_info = json.loads(user_info.text)
        else:
            log("Twitter lookup failed with %d." % user_info.status_code)
            raise Response(404)

    return user_info
Ejemplo n.º 23
0
def gittip(first, second, color):
    first = "tips"
    fp = urlopen("https://www.gittip.com/%s/public.json" % second)
    receiving = float(json.loads(fp.read())['receiving'])
    second = "$%d / week" % receiving
    if receiving == 0:
        color = RED
    elif receiving < 10:
        color = YELLOW
    elif receiving < 100:
        color = YELLOWGREEN
    else:
        color = GREEN

    return first, second, color
Ejemplo n.º 24
0
def gittip(first, second, color):
    first = "tips"
    fp = urlopen("https://www.gittip.com/%s/public.json" % second)
    receiving = float(json.loads(fp.read())['receiving'])
    second = "$%d / week" % receiving
    if receiving == 0:
        color = RED
    elif receiving < 10:
        color = YELLOW
    elif receiving < 100:
        color = YELLOWGREEN
    else:
        color = GREEN

    return first, second, color
Ejemplo n.º 25
0
def travis_ci(first, second, color):
    first = "build"

    url = 'https://api.travis-ci.org/repos/%s/branches/master' % quote(second)
    fp = urlopen(url)
    repo = json.loads(fp.read())
    try:
        second = repo['branch']['state']
    except KeyError:
        second = 'n/a'

    color = { 'failed': RED
            , 'passed': GREEN
            , 'started': YELLOW
             }.get(second, LIGHTGRAY)

    return first, second, color
Ejemplo n.º 26
0
def alias_and_track(cookie, gittip_user_id):
    """Given a cookie and a unicode, hit Mixpanel in a thread.
    """
    typecheck(cookie, SimpleCookie, gittip_user_id, unicode)

    # Pull distinct_id out of Mixpanel cookie. Yay undocumented internals!
    # This is bound to fail some day. Since this is in a thread, it shouldn't
    # affect the user experience, and we'll still get a record of the failure
    # in Sentry.

    mpcookie = [v for k,v in cookie.items() if k.endswith('_mixpanel')]
    if mpcookie:
        distinct_id = json.loads(unquote(mpcookie[0].value))['distinct_id']
        distinct_id = distinct_id.decode("utf8")
        alias(distinct_id, gittip_user_id)

    track(gittip_user_id, u"Opt In")
Ejemplo n.º 27
0
def get_user_info(screen_name):
    """Given a unicode, return a dict.
    """
    typecheck(screen_name, unicode)
    rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
                              "WHERE platform='twitter' "
                              "AND user_info->'screen_name' = %s"
                            , (screen_name,)
                             )
    if rec is not None:
        user_info = rec['user_info']
    else:
        url = "https://api.twitter.com/1/users/show.json?screen_name=%s"
        user_info = requests.get(url % screen_name)


        # Keep an eye on our Twitter usage.
        # =================================

        rate_limit = user_info.headers['X-RateLimit-Limit']
        rate_limit_remaining = user_info.headers['X-RateLimit-Remaining']
        rate_limit_reset = user_info.headers['X-RateLimit-Reset']

        try:
            rate_limit = int(rate_limit)
            rate_limit_remaining = int(rate_limit_remaining)
            rate_limit_reset = int(rate_limit_reset)
        except (TypeError, ValueError):
            log( "Got weird rate headers from Twitter: %s %s %s"
               % (rate_limit, rate_limit_remaining, rate_limit_reset)
                )
        else:
            reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
            reset = to_age(reset)
            log( "Twitter API calls used: %d / %d. Resets %s."
               % (rate_limit - rate_limit_remaining, rate_limit, reset)
                )


        if user_info.status_code == 200:
            user_info = json.loads(user_info.text)
        else:
            log("Twitter lookup failed with %d." % user_info.status_code)
            raise Response(404)

    return user_info
Ejemplo n.º 28
0
def travis_ci(first, second, color):
    first = "build"

    url = 'https://api.travis-ci.org/repos/%s/branches/master' % quote(second)
    fp = urlopen(url)
    repo = json.loads(fp.read())
    try:
        second = repo['branch']['state']
    except KeyError:
        second = 'n/a'

    color = {
        'failed': RED,
        'passed': GREEN,
        'started': YELLOW
    }.get(second, LIGHTGRAY)

    return first, second, color
Ejemplo n.º 29
0
def get_user_info(screen_name):
    """Given a unicode, return a dict.
    """
    typecheck(screen_name, unicode)
    rec = gittip.db.fetchone(
        "SELECT user_info FROM elsewhere "
        "WHERE platform='twitter' "
        "AND user_info->'screen_name' = %s", (screen_name, ))
    if rec is not None:
        user_info = rec['user_info']
    else:
        url = "https://api.twitter.com/1/users/show.json?screen_name=%s"
        user_info = requests.get(url % screen_name)

        # Keep an eye on our Twitter usage.
        # =================================

        rate_limit = user_info.headers['X-RateLimit-Limit']
        rate_limit_remaining = user_info.headers['X-RateLimit-Remaining']
        rate_limit_reset = user_info.headers['X-RateLimit-Reset']

        try:
            rate_limit = int(rate_limit)
            rate_limit_remaining = int(rate_limit_remaining)
            rate_limit_reset = int(rate_limit_reset)
        except (TypeError, ValueError):
            log("Got weird rate headers from Twitter: %s %s %s" %
                (rate_limit, rate_limit_remaining, rate_limit_reset))
        else:
            reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
            reset = to_age(reset)
            log("Twitter API calls used: %d / %d. Resets %s." %
                (rate_limit - rate_limit_remaining, rate_limit, reset))

        if user_info.status_code == 200:
            user_info = json.loads(user_info.text)
        else:
            log("Twitter lookup failed with %d." % user_info.status_code)
            raise Response(404)

    return user_info
Ejemplo n.º 30
0
    def __init__(self, first, second, bg):
        self.first = "build"

        url = 'https://api.travis-ci.org/repos?slug=%s' % quote(second)
        fp = urlopen(url)
        repos = json.loads(fp.read())
        if repos:
            status = repos[0].get('last_build_status', 'n/a')
        else:
            status = 'n/a'

        self.second = { 0: 'passing'
                      , 1: 'failing'
                      , None: 'pending'
                      , 'n/a': 'n/a'
                       }.get(status, 'n/a')

        self.bg = { 'failing': RED
                  , 'passing': GREEN
                  , 'pending': YELLOW
                   }.get(self.second, LIGHT_GREY)
Ejemplo n.º 31
0
    def oauth_dance(website, qs):
        """Given a querystring, return a dict of user_info.

        The querystring should be the querystring that we get from GitHub when
        we send the user to the return value of oauth_url above.

        See also:

            http://developer.github.com/v3/oauth/

        """

        log("Doing an OAuth dance with Github.")

        if 'error' in qs:
            raise Response(500, str(qs['error']))

        data = {
            'code': qs['code'].encode('US-ASCII'),
            'client_id': website.github_client_id,
            'client_secret': website.github_client_secret
        }
        r = requests.post("https://github.com/login/oauth/access_token",
                          data=data)
        assert r.status_code == 200, (r.status_code, r.text)

        back = dict([pair.split('=') for pair in r.text.split('&')])  # XXX
        if 'error' in back:
            raise Response(400, back['error'].encode('utf-8'))
        assert back.get('token_type', '') == 'bearer', back
        access_token = back['access_token']

        r = requests.get("https://api.github.com/user",
                         headers={'Authorization': 'token %s' % access_token})
        assert r.status_code == 200, (r.status_code, r.text)
        user_info = json.loads(r.text)
        log("Done with OAuth dance with Github for %s (%s)." %
            (user_info['login'], user_info['id']))

        return user_info
Ejemplo n.º 32
0
def get_user_info(login):
    """Given a unicode, return a dict.
    """
    typecheck(login, unicode)
    rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
                              "WHERE platform='github' "
                              "AND user_info->'login' = %s"
                            , (login,)
                             )
    if rec is not None:
        user_info = rec['user_info']
    else:
        url = "https://api.github.com/users/%s"
        user_info = requests.get(url % login)

        if user_info.status_code == 200:
            user_info = json.loads(user_info.text)
        else:
            code = user_info.status_code
            raise Response(500, "GitHub lookup failed with %d." % code)

    return user_info
Ejemplo n.º 33
0
def oauth_dance(website, qs):
    """Given a querystring, return a dict of user_info.

    The querystring should be the querystring that we get from GitHub when
    we send the user to the return value of oauth_url above.

    See also:

        http://developer.github.com/v3/oauth/

    """

    log("Doing an OAuth dance with Github.")

    if 'error' in qs:
        raise Response(500, str(qs['error']))

    data = { 'code': qs['code'].encode('US-ASCII')
           , 'client_id': website.github_client_id
           , 'client_secret': website.github_client_secret
            }
    r = requests.post("https://github.com/login/oauth/access_token", data=data)
    assert r.status_code == 200, (r.status_code, r.text)

    back = dict([pair.split('=') for pair in r.text.split('&')]) # XXX
    if 'error' in back:
        raise Response(400, back['error'].encode('utf-8'))
    assert back.get('token_type', '') == 'bearer', back
    access_token = back['access_token']

    r = requests.get( "https://api.github.com/user"
                    , headers={'Authorization': 'token %s' % access_token}
                     )
    assert r.status_code == 200, (r.status_code, r.text)
    user_info = json.loads(r.text)
    log("Done with OAuth dance with Github for %s (%s)."
        % (user_info['login'], user_info['id']))

    return user_info
Ejemplo n.º 34
0
def get_user_info(login):
    """Get the given user's information from the DB or failing that, github.

    :param login:
        A unicode string representing a username in github.

    :returns:
        A dictionary containing github specific information for the user.
    """
    typecheck(login, unicode)
    rec = gittip.db.fetchone(
        "SELECT user_info FROM elsewhere "
        "WHERE platform='github' "
        "AND user_info->'login' = %s", (login, ))
    if rec is not None:
        user_info = rec['user_info']
    else:
        url = "https://api.github.com/users/%s"
        user_info = requests.get(url % login,
                                 params={
                                     'client_id':
                                     os.environ.get('GITHUB_CLIENT_ID'),
                                     'client_secret':
                                     os.environ.get('GITHUB_CLIENT_SECRET')
                                 })
        status = user_info.status_code
        content = user_info.text
        if status == 200:
            user_info = json.loads(content)
        elif status == 404:
            raise Response(404,
                           "GitHub identity '{0}' not found.".format(login))
        else:
            log("Github api responded with {0}: {1}".format(status, content),
                level=logging.WARNING)
            raise Response(502, "GitHub lookup failed with %d." % status)

    return user_info
Ejemplo n.º 35
0
def travis_ci(first, second, color):
    first = "build"

    url = 'https://api.travis-ci.org/repos?slug=%s' % quote(second)
    fp = urlopen(url)
    repos = json.loads(fp.read())
    if repos:
        status = repos[0].get('last_build_status', 'n/a')
    else:
        status = 'n/a'

    second = { 0: 'passing'
             , 1: 'failing'
             , None: 'pending'
             , 'n/a': 'n/a'
              }.get(status, 'n/a')

    color = { 'failing': RED
            , 'passing': GREEN
            , 'pending': YELLOW
             }.get(second, LIGHTGRAY)

    return first, second, color
Ejemplo n.º 36
0
    def __init__(self, first, second, bg):
        self.first = "build"

        url = 'https://api.travis-ci.org/repos?slug=%s' % quote(second)
        fp = urlopen(url)
        repos = json.loads(fp.read())
        if repos:
            status = repos[0].get('last_build_status', 'n/a')
        else:
            status = 'n/a'

        self.second = {
            0: 'passing',
            1: 'failing',
            None: 'pending',
            'n/a': 'n/a'
        }.get(status, 'n/a')

        self.bg = {
            'failing': RED,
            'passing': GREEN,
            'pending': YELLOW
        }.get(self.second, LIGHT_GREY)
Ejemplo n.º 37
0
 def test_participant_can_toggle_anonymous_giving(self):
     response = self.hit_privacy('POST',
                                 data={'toggle': 'anonymous_giving'})
     actual = json.loads(response.body)
     assert actual['anonymous_giving'] is True
Ejemplo n.º 38
0
 def test_participant_can_toggle_anonymous_receiving_back(self):
     response = self.hit_anonymous('POST', data={'toggle': 'receiving'})
     response = self.hit_anonymous('POST', data={'toggle': 'receiving'})
     actual = json.loads(response.body)['receiving']
     assert actual is False
Ejemplo n.º 39
0
 def test_get_team_when_team_equals_member(self):
     response = self.client.GET('/team/members/team.json', auth_as='team')
     data = json.loads(response.body)
     assert response.code == 200
     assert data['username'] == 'team'
     assert data['take'] == '0.00'
Ejemplo n.º 40
0
 def test_participant_can_get_their_anonymity_setting(self):
     response = self.hit_anonymous('GET')
     actual = json.loads(response.body)['anonymous']
     assert actual is False, actual
Ejemplo n.º 41
0
def test_aspen_json_loads_loads():
    actual = json.loads('{"cheese": "puffs"}')
    assert actual == {'cheese': 'puffs'}
Ejemplo n.º 42
0
def test_aspen_json_loads_loads():
    actual = json.loads('{"cheese": "puffs"}')
    assert actual == {'cheese': 'puffs'}, actual
Ejemplo n.º 43
0
 def test_participant_can_toggle_their_anonymity_setting_back(self):
     response = self.hit_anonymous('POST')
     response = self.hit_anonymous('POST')
     actual = json.loads(response.body)['anonymous']
     assert actual is False, actual
Ejemplo n.º 44
0
 def test_participant_can_get_their_anonymity_setting(self):
     response = self.hit_anonymous('GET')
     actual = json.loads(response.body)['anonymous']
     assert actual is False, actual
Ejemplo n.º 45
0
 def test_participant_can_toggle_their_anonymity_setting_back(self):
     response = self.hit_anonymous('POST')
     response = self.hit_anonymous('POST')
     actual = json.loads(response.body)['anonymous']
     assert actual is False, actual
Ejemplo n.º 46
0
 def test_participant_can_get_their_anonymity_settings(self):
     response = self.hit_anonymous('GET')
     actual = json.loads(response.body)
     assert actual == {'giving': False, 'receiving': False}
Ejemplo n.º 47
0
 def __init__(self, first, second, bg):
     self.first = "tips"
     fp = urlopen("https://www.gittip.com/%s/public.json" % second)
     receiving = json.loads(fp.read())['receiving']
     self.second = "$%d / week" % float(receiving)
     self.bg = (42, 143, 121)  # Gittip green! :)
Ejemplo n.º 48
0
 def test_participant_can_toggle_is_searchable_back(self):
     response = self.hit_privacy('POST', data={'toggle': 'is_searchable'})
     response = self.hit_privacy('POST', data={'toggle': 'is_searchable'})
     actual = json.loads(response.body)
     assert actual['is_searchable'] is True
Ejemplo n.º 49
0
 def test_participant_can_get_their_anonymity_settings(self):
     response = self.hit_anonymous('GET')
     actual = json.loads(response.body)
     assert actual == {'giving': False, 'receiving': False}
Ejemplo n.º 50
0
 def test_participant_can_toggle_anonymous_giving(self):
     response = self.hit_anonymous('POST', data={'toggle': 'giving'})
     actual = json.loads(response.body)
     assert actual['giving'] is True
Ejemplo n.º 51
0
 def test_participant_can_toggle_is_searchable_back(self):
     response = self.hit_privacy('POST', data={'toggle': 'is_searchable'})
     response = self.hit_privacy('POST', data={'toggle': 'is_searchable'})
     actual = json.loads(response.body)
     assert actual['is_searchable'] is True
Ejemplo n.º 52
0
def get_user_info(login):
    """Get the given user's information from the DB or failing that, github.

    :param login:
        A unicode string representing a username in github.

    :returns:
        A dictionary containing github specific information for the user.
    """
    typecheck(login, (unicode, UnicodeWithParams))
    rec = gittip.db.one( "SELECT user_info FROM elsewhere "
                         "WHERE platform='github' "
                         "AND user_info->'login' = %s"
                       , (login,)
                        )

    if rec is not None:
        user_info = rec
    else:
        url = "https://api.github.com/users/%s"
        user_info = requests.get(url % login, params={
            'client_id': os.environ.get('GITHUB_CLIENT_ID'),
            'client_secret': os.environ.get('GITHUB_CLIENT_SECRET')
        })
        status = user_info.status_code
        content = user_info.text

        # Calculate how much of our ratelimit we have consumed
        remaining = int(user_info.headers['x-ratelimit-remaining'])
        limit = int(user_info.headers['x-ratelimit-limit'])
        # thanks to from __future__ import division this is a float
        percent_remaining = remaining/limit

        log_msg = ''
        log_lvl = None
        # We want anything 50% or over
        if 0.5 <= percent_remaining:
            log_msg = ("{0}% of GitHub's ratelimit has been consumed. {1}"
                       " requests remaining.").format(percent_remaining * 100,
                                                      remaining)
        if 0.5 <= percent_remaining < 0.8:
            log_lvl = logging.WARNING
        elif 0.8 <= percent_remaining < 0.95:
            log_lvl = logging.ERROR
        elif 0.95 <= percent_remaining:
            log_lvl = logging.CRITICAL

        if log_msg and log_lvl:
            log(log_msg, log_lvl)

        if status == 200:
            user_info = json.loads(content)
        elif status == 404:
            raise Response(404,
                           "GitHub identity '{0}' not found.".format(login))
        else:
            log("Github api responded with {0}: {1}".format(status, content),
                level=logging.WARNING)
            raise Response(502, "GitHub lookup failed with %d." % status)

    return user_info
Ejemplo n.º 53
0
 def __init__(self, first, second, bg):
     self.first = "tips"
     fp = urlopen("https://www.gittip.com/%s/public.json" % second)
     receiving = json.loads(fp.read())['receiving']
     self.second = "$%d / week" % float(receiving)
     self.bg = (42, 143, 121)  # Gittip green! :)