def loadFollowingCountFromDBofUser(user_id):
    con = db.con()
    with con:
        cur = con.cursor()
        cur.execute('''SELECT count(friend) FROM b_following
                       WHERE user_id = %s''', (user_id))
        count = cur.fetchone()[0]

    return count
def countFollowing(user_id):
    con = db.con()
    with con:
        cur = con.cursor()
        cur.execute('''SELECT count(friend)
                       FROM b_following
                       WHERE user_id = %s''', (user_id))
        following_count = cur.fetchone()[0]

    return following_count
def countStatus(user_id):
    con = db.con()
    with con:
        cur = con.cursor()
        cur.execute('''SELECT length
                       FROM b_text
                       WHERE user_id = %s''', (user_id))
        numrows = int(cur.rowcount)

        length = cur.fetchone()[0] if numrows else 0
        return length
def loadFollowingFromDBofUser(user_id):
    friends = []

    con = db.con()
    with con:
        cur = con.cursor()
        cur.execute('''SELECT friend FROM b_following
                       WHERE user_id = %s''', (user_id))
        numrows = int(cur.rowcount)
        for i in range(numrows):
            friend = cur.fetchone()[0]
            friends.append(friend)

    return friends
def loadNameFromDBofUser(user_id):
    con = db.con()
    with con:
        cur = con.cursor()
        cur.execute('''SELECT name, screen_name FROM m_names
                       WHERE user_id = %s''', (user_id))
        numrows = int(cur.rowcount)

        if numrows:
            row = cur.fetchone()
            name = row[0]
            screen_name = row[1]
        else:
            name = "None"
            screen_name = "None"

    return (name, screen_name)
def loadNeighborTextFromDBofUser(user_id):
    texts = []

    con = db.con()
    with con:
        cur = con.cursor()
        cur.execute('''SELECT t.text
                       FROM m_text t
                       INNER JOIN m_cluster c
                       ON t.user_id = c.friend
                       WHERE c.root = %s''', (user_id))
        numrows = int(cur.rowcount)

        for i in range(numrows):
            text = cur.fetchone()[0]
            texts.append(text)

    texts = '\n'.join(texts)

    return (texts, 200 * numrows)
def saveNameIntoDBofUser(user_id, data):
    '''
    data = {'user_id': user_id,
            'name': name,
            'lang': lang,
            'screen_name': screen_name
           }
    '''
    user_id = user_id
    name = data['name']
    lang = data['lang']
    screen_name = data['screen_name']

    con = db.con()
    with con:
        cur = con.cursor()

        cur.execute('''REPLACE INTO m_names
                       VALUES (%s, %s, %s, %s)''',
                       (user_id, name, lang, screen_name))
def loadTextFromDBofUser(user_id, option = 'normal'):
    con = db.con()
    with con:
        cur = con.cursor()

        if option == 'normal':
            cur.execute('''SELECT text, length FROM b_text
                           WHERE user_id = %s''', (user_id))
        elif option == 'short':
            cur.execute('''SELECT text, length FROM b_shorttext
                           WHERE user_id = %s''', (user_id))

        numrows = int(cur.rowcount)

        if numrows:
            row = cur.fetchone()
            text = row[0]
            length = row[1]
        else:
            text = ''
            length = None

        return (text, length)