Example #1
0
def http(req):
    if req.user:
        res = Deferred()

        def userCb(payload):
            user = simplejson.loads(payload)

            private = user['private']

            def tokCb(payload):
                result = parse_qs(payload)
                auth_token = result["oauth_token"][0]
                auth_secret = result["oauth_token_secret"][0]

                private['twitter'] = {'oauth_token_secret': auth_secret}

                def saveCb(payload):
                    req.result = "ok"

                    redirect = Redirect("%s?oauth_token=%s" %
                                        (TWITTER_AUTHORIZE_URL, auth_token))
                    res.callback(redirect)

                d2 = data.set('AUTH', req.user['id'],
                              simplejson.dumps(private))
                d2.addCallback(saveCb)

            params = {
                "oauth_consumer_key": encode(TWITTER_KEY),
                "oauth_signature_method": "HMAC-SHA1",
                "oauth_timestamp": str(int(time())),
                "oauth_nonce": encode(str(getrandbits(64))),
                "oauth_version": "1.0"
            }

            params_str = "&".join([
                "%s=%s" % (encode(k), encode(params[k]))
                for k in sorted(params)
            ])
            message = "&".join(
                ["GET", encode(TWITTER_REQUEST_URL),
                 encode(params_str)])

            key = "%s&%s" % (encode(TWITTER_SECRET), "")
            signature = hmac(key, message, sha1)
            digest_base64 = signature.digest().encode("base64").strip()
            params["oauth_signature"] = digest_base64

            tok = client.getPage(url="%s?%s" %
                                 (TWITTER_REQUEST_URL, urlencode(params)))
            tok.addCallback(tokCb)

        d1 = data.get('AUTH', req.user['id'])
        d1.addCallback(userCb)

        return res

    else:
        req.result = None
        return None
Example #2
0
File: login.py Project: lzimm/360io
def http_POST(req):
    email = req.args.get('email', [None])[0]
    password = req.args.get('password', [None])[0]

    if email:
        id = hashlib.md5(str(email).lower()).hexdigest()
        
        res = Deferred()

        def loginCb(payload):
            req.result = False

            if payload:
                user = simplejson.loads(payload)
                if user['private']['password'] == password:
                    req.result = user['public']
                    req.result['twitter'] = user['private'].get('twitter', {}).get('screen_name', [None])[0]
                    updateUserCookie(req, user['public'], twitter=user['private'].get('twitter', {}).get('screen_name', [None])[0])

            res.callback(None)

        d1 = data.get('AUTH', id)
        d1.addCallback(loginCb)

        return res

    else:
        req.result = None
        return None
Example #3
0
File: login.py Project: lzimm/360io
def http_POST(req):
    email = req.args.get('email', [None])[0]
    password = req.args.get('password', [None])[0]

    if email:
        id = hashlib.md5(str(email).lower()).hexdigest()

        res = Deferred()

        def loginCb(payload):
            req.result = False

            if payload:
                user = simplejson.loads(payload)
                if user['private']['password'] == password:
                    req.result = user['public']
                    req.result['twitter'] = user['private'].get(
                        'twitter', {}).get('screen_name', [None])[0]
                    updateUserCookie(req,
                                     user['public'],
                                     twitter=user['private'].get(
                                         'twitter',
                                         {}).get('screen_name', [None])[0])

            res.callback(None)

        d1 = data.get('AUTH', id)
        d1.addCallback(loginCb)

        return res

    else:
        req.result = None
        return None
Example #4
0
def http(req):    
    oauth_token = req.args.get('oauth_token', [None])[0]
    oauth_verifier = req.args.get('oauth_verifier', [None])[0]
    
    if req.user:  
        res = Deferred()
              
        def userCb(payload):
            user = simplejson.loads(payload)
            
            private = user['private']
            oauth_secret = private['twitter']['oauth_token_secret']
            
            def tokCb(payload):
                result = parse_qs(payload)
                private['twitter'] = result

                def saveCb(payload):
                    if (result.get('screen_name', None)):
                        req.result = result['screen_name'][0]
                    else:
                        req.result = ''

                    res.callback(None)
                
                d2 = data.set('AUTH', req.user['id'], simplejson.dumps(private))
                d2.addCallback(saveCb)

            params = {
                "oauth_consumer_key": encode(TWITTER_KEY),
                "oauth_signature_method": "HMAC-SHA1",
                "oauth_timestamp": str(int(time())),
                "oauth_nonce": encode(str(getrandbits(64))),
                "oauth_version": "1.0",
                "oauth_token": encode(oauth_token)
            }

            params_str = "&".join(["%s=%s" % (encode(k), encode(params[k])) for k in sorted(params)])
            message = "&".join(["GET", encode(TWITTER_ACCESS_URL), encode(params_str)])

            key = "%s&%s" % (encode(TWITTER_SECRET), encode(oauth_secret))
            signature = hmac(key, message, sha1)
            digest_base64 = signature.digest().encode("base64").strip()
            params["oauth_signature"] = digest_base64

            tok = client.getPage(url="%s?%s" % (TWITTER_ACCESS_URL, urlencode(params)))

            tok.addCallback(tokCb)
    
        d1 = data.get('AUTH', req.user['id'])
        d1.addCallback(userCb)
    
        return res
    
    else:
        req.result = None
        return None
Example #5
0
def getStreamTail(req, res, lid):
    def cb(payload):
        req.result.update({'tail': payload})
        res.callback(payload)

    d1 = data.get("TAIL", lid)
    d1.addCallback(cb)

    return d1
Example #6
0
def getStreamTail(req, res, lid):
    def cb(payload):
        req.result.update({'tail': payload})
        res.callback(payload)
    
    d1 = data.get("TAIL", lid)
    d1.addCallback(cb)

    return d1
Example #7
0
File: style.py Project: lzimm/360io
def http(req):
    if req.user:
        res = Deferred()
              
        def userCb(payload):
            user = simplejson.loads(payload)
            public = user['public']
            
            req.result = {'style' : public.get('style', '')}
            res.callback(None)            
    
        d1 = data.get('AUTH', req.user['id'])
        d1.addCallback(userCb)
    
        return res
    
    else:
        req.result = None
        return None
Example #8
0
File: style.py Project: lzimm/360io
def http(req):
    if req.user:
        res = Deferred()

        def userCb(payload):
            user = simplejson.loads(payload)
            public = user['public']

            req.result = {'style': public.get('style', '')}
            res.callback(None)

        d1 = data.get('AUTH', req.user['id'])
        d1.addCallback(userCb)

        return res

    else:
        req.result = None
        return None
Example #9
0
File: style.py Project: lzimm/360io
def http_POST(req):
    style = req.args.get('style', [None])[0]

    if style and req.user:
        style = style[1:]

        res = Deferred()

        def userCb(payload):
            user = simplejson.loads(payload)

            encoded_style = ""
            for tag, val in STYLE_MATCH.findall(style):
                if not tag.strip():
                    continue
                encoded_style = encoded_style + (
                    "div._comment div.style_%s %s{%s}" %
                    (req.user['id'], tag, val))

            public = user['public']
            public['style'] = encoded_style

            def saveCb(payload):
                req.result = "ok"
                res.callback(None)

            d2 = data.set('USER', req.user['id'], simplejson.dumps(public))
            d2.addCallback(saveCb)

        d1 = data.get('AUTH', req.user['id'])
        d1.addCallback(userCb)

        return res

    elif req.user:
        return http(req)

    else:
        req.result = None
        return None
Example #10
0
File: style.py Project: lzimm/360io
def http_POST(req):
    style = req.args.get('style', [None])[0]
    
    if style and req.user:
        style = style[1:]
        
        res = Deferred()
        
        def userCb(payload):
            user = simplejson.loads(payload)
            
            encoded_style = ""
            for tag, val in STYLE_MATCH.findall(style):
                if not tag.strip():
                    continue
                encoded_style = encoded_style + ("div._comment div.style_%s %s{%s}" % (req.user['id'], tag, val))
            
            public = user['public']
            public['style'] = encoded_style

            def saveCb(payload):
                req.result = "ok"
                res.callback(None)
            
            d2 = data.set('USER', req.user['id'], simplejson.dumps(public))
            d2.addCallback(saveCb)               
    
        d1 = data.get('AUTH', req.user['id'])
        d1.addCallback(userCb)
    
        return res
        
    elif req.user:
        return http(req)
    
    else:
        req.result = None
        return None
Example #11
0
File: post.py Project: lzimm/360io
def http_POST(req):
    tweet = req.args.get("tweet", [None])[0]

    if req.user:
        res = Deferred()

        def userCb(payload):
            user = simplejson.loads(payload)
            twitter = user["private"].get("twitter", None)

            if twitter:
                oauth_token = twitter["oauth_token"][0]
                oauth_secret = twitter["oauth_token_secret"][0]

                def updateCb(payload):
                    req.result = simplejson.loads(payload)
                    res.callback(None)

                params = {
                    "oauth_consumer_key": encode(TWITTER_KEY),
                    "oauth_signature_method": "HMAC-SHA1",
                    "oauth_timestamp": str(int(time())),
                    "oauth_nonce": encode(str(getrandbits(64))),
                    "oauth_version": "1.0",
                    "oauth_token": encode(oauth_token),
                    "status": tweet,
                }

                params_str = "&".join(["%s=%s" % (encode(k), encode(params[k])) for k in sorted(params)])
                message = "&".join(["POST", encode(TWITTER_UPDATE_URL), encode(params_str)])

                key = "%s&%s" % (encode(TWITTER_SECRET), encode(oauth_secret))
                signature = hmac(key, message, sha1)
                digest_base64 = signature.digest().encode("base64").strip()
                params["oauth_signature"] = digest_base64

                headers = {
                    "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
                    "Authorization": ", ".join(
                        ['OAuth realm=""']
                        + ['%s="%s"' % (encode(k), encode(params[k])) for k in params if k[:6] == "oauth_"]
                    ),
                }

                update = client.getPage(
                    url=TWITTER_UPDATE_URL, method="POST", postdata=urlencode({"status": tweet}), headers=headers
                )
                update.addCallback(updateCb)

            else:
                req.result = "notether"
                res.callback(None)

        d1 = data.get("AUTH", req.user["id"])
        d1.addCallback(userCb)

        return res

    else:
        req.result = "noaccount"
        return None
Example #12
0
def http_POST(req):
    tweet = req.args.get('tweet', [None])[0]

    if req.user:
        res = Deferred()

        def userCb(payload):
            user = simplejson.loads(payload)
            twitter = user['private'].get('twitter', None)

            if twitter:
                oauth_token = twitter['oauth_token'][0]
                oauth_secret = twitter['oauth_token_secret'][0]

                def updateCb(payload):
                    req.result = simplejson.loads(payload)
                    res.callback(None)

                params = {
                    "oauth_consumer_key": encode(TWITTER_KEY),
                    "oauth_signature_method": "HMAC-SHA1",
                    "oauth_timestamp": str(int(time())),
                    "oauth_nonce": encode(str(getrandbits(64))),
                    "oauth_version": "1.0",
                    "oauth_token": encode(oauth_token),
                    "status": tweet
                }

                params_str = "&".join([
                    "%s=%s" % (encode(k), encode(params[k]))
                    for k in sorted(params)
                ])
                message = "&".join(
                    ["POST",
                     encode(TWITTER_UPDATE_URL),
                     encode(params_str)])

                key = "%s&%s" % (encode(TWITTER_SECRET), encode(oauth_secret))
                signature = hmac(key, message, sha1)
                digest_base64 = signature.digest().encode("base64").strip()
                params["oauth_signature"] = digest_base64

                headers = {
                    'Content-Type':
                    'application/x-www-form-urlencoded; charset=utf-8',
                    'Authorization':
                    ', '.join(['OAuth realm=""'] + [
                        '%s="%s"' % (encode(k), encode(params[k]))
                        for k in params if k[:6] == 'oauth_'
                    ])
                }

                update = client.getPage(url=TWITTER_UPDATE_URL,
                                        method='POST',
                                        postdata=urlencode({"status": tweet}),
                                        headers=headers)
                update.addCallback(updateCb)

            else:
                req.result = "notether"
                res.callback(None)

        d1 = data.get('AUTH', req.user['id'])
        d1.addCallback(userCb)

        return res

    else:
        req.result = "noaccount"
        return None