Exemple #1
0
def getNodesEdges(self, uid, session):
    result = fbutils.fql("SELECT uid2 FROM friend WHERE uid1 = " + uid,
                         session['access_token'])
    nodes = []
    for node in result:
        nodes.append(node['uid2'])

    result = fbutils.fql(
        "SELECT uid1, uid2 FROM friend WHERE " +
        "uid1 IN (SELECT uid2 FROM friend WHERE uid1 = " + uid + ") AND " +
        "uid2 IN (SELECT uid1 FROM friend WHERE uid2 = " + uid + ")",
        session['access_token'])

    edges = []
    for edge in result:
        edges.append([edge['uid1'], edge['uid2']])

    libSNA = libsna.SocialNetwork()
    libSNA.loadGraph(nodes=nodes, edges=edges)

    h = hashlib.sha1()
    h.update("%s - %s" % (libSNA.graph.nodes(), libSNA.graph.edges()))
    networkhash = h.hexdigest()

    return nodes, edges, networkhash
def getNodesEdges(self, uid, session):
    result = fbutils.fql(
            "SELECT uid2 FROM friend WHERE uid1 = " + uid,
            session['access_token'])
    nodes = []
    for node in result:
        nodes.append(node['uid2'])
        
    result = fbutils.fql(
        "SELECT uid1, uid2 FROM friend WHERE " +
        "uid1 IN (SELECT uid2 FROM friend WHERE uid1 = " + uid + ") AND " +
        "uid2 IN (SELECT uid1 FROM friend WHERE uid2 = " + uid + ")",
        session['access_token']);
    
    edges = []
    for edge in result:
        edges.append([edge['uid1'], edge['uid2']])
        
    libSNA = libsna.SocialNetwork() 
    libSNA.loadGraph(nodes=nodes, edges=edges)

    h = hashlib.sha1()
    h.update("%s - %s" % (libSNA.graph.nodes(), libSNA.graph.edges()))
    networkhash = h.hexdigest()
    
    return nodes, edges, networkhash
Exemple #3
0
def computeLeague(libSNA, uid, session):
    d = nx.degree(libSNA.graph)
    c = nx.closeness_centrality(libSNA.graph)
    b = nx.betweenness_centrality(libSNA.graph)

    ds = sorted_map(d)
    cs = sorted_map(c)
    bs = sorted_map(b)

    names1 = [x[0] for x in ds[:10]]
    names2 = [x[0] for x in cs[:10]]
    names3 = [x[0] for x in bs[:10]]

    names = list(set(names1) | set(names2) | set(names3))
    names = sorted(names,
                   key=lambda name: d[name] / ds[0][1] * 300 + c[name] / cs[0][
                       1] * 200 + b[name] / bs[0][1] * 100,
                   reverse=True)

    result = fbutils.fql(
        "SELECT uid, name FROM user WHERE uid IN ( " \
        "SELECT uid2 FROM friend WHERE uid1 = " + uid + ")",
        session['access_token'])

    nodes = {}
    for node in result:
        nodes[str(node['uid'])] = node['name']

    return [[name, nodes[name],
             str(d[name]),
             str(c[name]),
             str(b[name])] for name in names]
def computeLeague(libSNA, uid, session):
    d = nx.degree(libSNA.graph)
    c = nx.closeness_centrality(libSNA.graph)
    b = nx.betweenness_centrality(libSNA.graph)
    
    ds = sorted_map(d)
    cs = sorted_map(c)
    bs = sorted_map(b)
    
    names1 = [x[0] for x in ds[:10]]
    names2 = [x[0] for x in cs[:10]]
    names3 = [x[0] for x in bs[:10]]
    
    names = list(set(names1) | set(names2) | set(names3))
    names = sorted(names, key = lambda name: d[name]/ds[0][1]*300 + c[name]/cs[0][1]*200 + b[name]/bs[0][1]*100, reverse = True)
    
    result = fbutils.fql(
        "SELECT uid, name FROM user WHERE uid IN ( " \
        "SELECT uid2 FROM friend WHERE uid1 = " + uid + ")",
        session['access_token'])
    
    nodes = {}
    for node in result:
        nodes[str(node['uid'])] = node['name'] 
    
    return [[name, nodes[name], str(d[name]), str(c[name]), str(b[name])] for name in names]
    def renderPage(self, uid, indexname):
        code = self.request.get('code', None)
        index = None
        
        value = None
        nodevalues = None
        edgevalues = None
        nodegaussian = None
        edgegaussian = None
        
        session = sessionmanager.getsession(self)
        try:
            if session['me']['id'] != uid: code = None
        except: code = None
                        
        q = db.GqlQuery("SELECT * FROM Index " +
                        "WHERE uid = :1 AND name = :2 " +
                        "ORDER BY updated_time DESC",
                        uid,
                        indexname)
        indexes = q.fetch(1)

        if not len(indexes) == 0:
            index = indexes[0]
            
            value = index.value
            nodevalues = self.setcentiles(index.get_nodevalues())
            edgevalues = self.setcentiles(index.get_edgevalues())
            nodegaussian = self.getgaussian(nodevalues)
            edgegaussian = self.getgaussian(edgevalues)
            
        if session == None:
            app_friends = None
        else:
            app_friends = fbutils.fql("SELECT uid, name, is_app_user " +
                                      "FROM user " +
                                      "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1 " +
                                      "ORDER BY name", session['access_token'])
        
        template_values = {
            'conf': conf,
            'indexname': indexname,
            'description': conf.INDEXES[indexname],
            'uid': uid,
            'index': index,
            'nodevalues': nodevalues,
            'edgevalues': edgevalues,
            'nodegaussian': nodegaussian,
            'edgegaussian': edgegaussian,
            'value': value,
            'friends': app_friends,
            'isdesktop': session and session['isdesktop'] or False,
            'header': 'sociologicalindex',
            'code': code }
        
        root = os.path.normpath(os.path.join(os.path.dirname(__file__), os.path.pardir))
        self.response.out.write(template.render(os.path.join(root, 'templates/_header.html'), template_values))
        self.response.out.write(template.render(os.path.join(root, 'pages/templates/sociologicalindex.html'), template_values))
        self.response.out.write(template.render(os.path.join(root, 'templates/_footer.html'), template_values))
    def renderPage(self, uid):
        code = self.request.get('code', None)
        
        session = sessionmanager.getsession(self)
        try:
            if session['me']['id'] != uid: code = None
        except: code = None
        
        nodes = None
        edges = None
        league = None
        hiddenleague = False
        
        q = db.GqlQuery("SELECT * FROM Network WHERE uid = :1", uid)
        networks = q.fetch(1)

        if not len(networks) == 0:
            network = networks[0]
            
            nodes = network.getnodes()
            edges = network.getedges()
            league = network.getleague()
            
            try:
                if not fbutils.is_friend(session['access_token'], session['me']['id'], uid): 
                    for row in league:
                        row[1] = re.sub("\w", "x", row[1]).title()
                    hiddenleague = True
            except:
                for row in league:
                    row[1] = re.sub("\w", "x", row[1]).title()
                hiddenleague = True
        
        if session == None:
            app_friends = None
        else:
            app_friends = fbutils.fql("SELECT uid, name, is_app_user " +
                                      "FROM user " +
                                      "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1 " +
                                      "ORDER BY name", session['access_token'])
        
        template_values = {
            'conf': conf,
            'uid': uid,
            'me': session['me'],
            'nodes': nodes,
            'edges': edges,
            'league': league,
            'hiddenleague': hiddenleague,
            'friends': app_friends,
            'isdesktop': session and session['isdesktop'] or False,
            'header': 'network',
            'code': code }

        root = os.path.normpath(os.path.join(os.path.dirname(__file__), os.path.pardir))
        self.response.out.write(template.render(os.path.join(root, 'templates/_header.html'), template_values))
        self.response.out.write(template.render(os.path.join(root, 'pages/templates/network.html'), template_values))
        self.response.out.write(template.render(os.path.join(root, 'templates/_footer.html'), template_values))
Exemple #7
0
    def renderPage(self, uid, indexname):
        code = self.request.get('code', None)
        index = None

        value = None
        nodevalues = None
        edgevalues = None
        nodegaussian = None
        edgegaussian = None

        session = sessionmanager.getsession(self)
        try:
            if session['me']['id'] != uid: code = None
        except:
            code = None

        q = db.GqlQuery(
            "SELECT * FROM Index " + "WHERE uid = :1 AND name = :2 " +
            "ORDER BY updated_time DESC", uid, indexname)
        indexes = q.fetch(1)

        if not len(indexes) == 0:
            index = indexes[0]

            value = index.value
            nodevalues = self.setcentiles(index.get_nodevalues())
            edgevalues = self.setcentiles(index.get_edgevalues())
            nodegaussian = self.getgaussian(nodevalues)
            edgegaussian = self.getgaussian(edgevalues)

        if session == None:
            app_friends = None
        else:
            app_friends = fbutils.fql(
                "SELECT uid, name, is_app_user " + "FROM user " +
                "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1 "
                + "ORDER BY name", session['access_token'])

        template_values = {
            'conf': conf,
            'indexname': indexname,
            'description': conf.INDEXES[indexname],
            'uid': uid,
            'index': index,
            'nodevalues': nodevalues,
            'edgevalues': edgevalues,
            'nodegaussian': nodegaussian,
            'edgegaussian': edgegaussian,
            'value': value,
            'friends': app_friends,
            'isdesktop': session and session['isdesktop'] or False,
            'header': 'sociologicalindex',
            'code': code
        }

        root = os.path.normpath(
            os.path.join(os.path.dirname(__file__), os.path.pardir))
        self.response.out.write(
            template.render(os.path.join(root, 'templates/_header.html'),
                            template_values))
        self.response.out.write(
            template.render(
                os.path.join(root, 'pages/templates/sociologicalindex.html'),
                template_values))
        self.response.out.write(
            template.render(os.path.join(root, 'templates/_footer.html'),
                            template_values))
    def renderPage(self, uid):
        code = self.request.get('code', None)

        session = sessionmanager.getsession(self)
        try:
            if session['me']['id'] != uid: code = None
        except:
            code = None

        nodes = None
        edges = None
        league = None
        hiddenleague = False

        q = db.GqlQuery("SELECT * FROM Network WHERE uid = :1", uid)
        networks = q.fetch(1)

        if not len(networks) == 0:
            network = networks[0]

            nodes = network.getnodes()
            edges = network.getedges()
            league = network.getleague()

            try:
                if not fbutils.is_friend(session['access_token'],
                                         session['me']['id'], uid):
                    for row in league:
                        row[1] = re.sub("\w", "x", row[1]).title()
                    hiddenleague = True
            except:
                for row in league:
                    row[1] = re.sub("\w", "x", row[1]).title()
                hiddenleague = True

        if session == None:
            app_friends = None
        else:
            app_friends = fbutils.fql(
                "SELECT uid, name, is_app_user " + "FROM user " +
                "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1 "
                + "ORDER BY name", session['access_token'])

        template_values = {
            'conf': conf,
            'uid': uid,
            'me': session['me'],
            'nodes': nodes,
            'edges': edges,
            'league': league,
            'hiddenleague': hiddenleague,
            'friends': app_friends,
            'isdesktop': session and session['isdesktop'] or False,
            'header': 'network',
            'code': code
        }

        root = os.path.normpath(
            os.path.join(os.path.dirname(__file__), os.path.pardir))
        self.response.out.write(
            template.render(os.path.join(root, 'templates/_header.html'),
                            template_values))
        self.response.out.write(
            template.render(os.path.join(root, 'pages/templates/network.html'),
                            template_values))
        self.response.out.write(
            template.render(os.path.join(root, 'templates/_footer.html'),
                            template_values))
Exemple #9
0
    def renderPage(self):
        session = sessionmanager.getsession(self)
        
        if session:
            app_friends = fbutils.fql(
                "SELECT uid, name, is_app_user, pic_square "
                "FROM user "
                "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND "
                "  is_app_user = 1", session['access_token'])
            
            q = db.GqlQuery("SELECT * FROM User WHERE uid = :1", session['me']['id'])
            users = q.fetch(1)

            datefb = session['me']['updated_time'].replace("+0000", "").replace("T", " ")
            datefb = datetime.datetime.fromtimestamp(
                   time.mktime(time.strptime(datefb, "%Y-%m-%d %H:%M:%S")))
            
            if len(users) > 0:
                curuser = users[0]
                datedb = curuser.updated_time

                if datefb > datedb:
                    curuser.first_name = session['me']['first_name']
                    curuser.last_name = session['me']['last_name']
                    curuser.link = session['me']['link']
                    if 'username' in session['me']:
                        curuser.username = session['me']['username']
                    curuser.updated_time = datefb
                    curuser.put()
                  
                    logging.info('User updated: ' + session['me']['id'])
            else:
                curuser = obj_user.User(uid = session['me']['id'])
                curuser.first_name = session['me']['first_name']
                curuser.last_name = session['me']['last_name']
                curuser.link = session['me']['link']
                if 'username' in session['me']:
                    curuser.username = session['me']['username']
                curuser.updated_time = datefb
                curuser.put()
              
                logging.info('User added: ' + session['me']['id'])

            indexes = []
            q = db.GqlQuery("SELECT * FROM Index " +
                        "WHERE uid = :1 " +
                        "ORDER BY updated_time DESC",
                        session['me']['id'])
            
            for index in q:            
                if not index.networkhash == None and \
                   not index.value == None and \
                   not index.name in indexes:
                    indexes.append(index.name)
            
            tests = []
            q = db.GqlQuery("SELECT * FROM Test")
        
            for test in q:
                if test.active and datetime.date.today() >= test.startdate and datetime.date.today() <= test.enddate:
                    tests.append(test)

            template_values = {
                'appId': conf.FBAPI_APP_ID,
                'token': session['access_token'],
                'app_friends': app_friends,
                'app': session['appid'],
                'conf': conf,
                'me': session['me'],
                'roles': session['roles'],
                'computedindexes': indexes,
                'numindexes': len(conf.INDEXES),
                'tests': tests,
                'isdesktop': session['isdesktop'],
                'header': '',
                'code': self.request.get('code') }

            root = os.path.normpath(os.path.join(os.path.dirname(__file__), os.path.pardir))
            self.response.out.write(template.render(os.path.join(root, 'templates/_header.html'), template_values))
            self.response.out.write(template.render(os.path.join(root, 'pages/templates/home.html'), template_values))
            self.response.out.write(template.render(os.path.join(root, 'templates/_footer.html'), template_values))
        else:
            self.response.out.write('''
            <html><head>
            <script type="text/javascript">
            <!--
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-256445-3']);
            _gaq.push(['_trackPageview']);

            (function() {
              var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
              ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
              var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();
            // -->
            </script>''')
            
            self.response.out.write('<meta HTTP-EQUIV="REFRESH" content="0; url=' +
                fbutils.oauth_login_url(self=self, next_url=fbutils.base_url(self)) +
                '"></head><body></body></html>')