File size: 3,579 Bytes
ef03d77
1d33c47
f5a5ce7
ef03d77
 
1d33c47
e00f1c6
6638f01
e00f1c6
 
 
 
ef03d77
 
 
 
 
 
b39ee05
ef03d77
2920981
 
 
ef03d77
6638f01
ef03d77
5ddd2a0
ef03d77
 
 
 
 
 
5ddd2a0
ef03d77
 
 
 
 
 
f080be6
 
ef03d77
 
 
 
 
 
 
15c6271
ef03d77
 
 
0a2541d
870c3a8
ef03d77
ca9dcd2
ef03d77
 
 
 
 
f080be6
 
ef03d77
f080be6
 
ef03d77
f080be6
 
 
ef03d77
f080be6
587e167
6178ee5
 
 
 
 
f080be6
 
f5a5ce7
 
f080be6
3150cd7
f080be6
2f44b2d
e7e4566
863952d
 
bd5a8f4
863952d
 
e7e4566
3450f30
e7e4566
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import tweepy
import os
import json
import mysql.connector
import gradio as gr

bearer_token=os.environ["BEARER_KEY"]

consumer_key = os.environ["CONSUMER_KEY"]
consumer_secret = os.environ["CONSUMER_SECRET"]
access_token = os.environ["ACCESS_TOKEN"]
access_token_secret = os.environ["ACCESS_TOKEN_SECRET"]
db_name = os.environ["DB_NAME"]
db_host = os.environ["DB_HOST"]
db_port = os.environ["DB_PORT"]
db_user = os.environ["DB_USER"]
db_ps = os.environ["DB_PS"]


auth = tweepy.OAuth1UserHandler(
    consumer_key=consumer_key, consumer_secret=consumer_secret,
    access_token=access_token, access_token_secret=access_token_secret
)
api = tweepy.API(auth)

def mysql_exe_insertorupdate(sql):
    db = mysql.connector.connect(host = db_host,port = db_port ,user =db_user, password=db_ps, database=db_name, charset='utf8mb4')
    cursor  = db.cursor()
    cursor.execute(sql)
    db.commit()
    db.close()

def mysql_exe_select(sql):
    db = mysql.connector.connect(host = db_host,port = db_port ,user =db_user, password=db_ps, database=db_name, charset='utf8mb4')
    cursor  = db.cursor()
    cursor.execute(sql)
    results = cursor.fetchall()
    db.commit()
    db.close()
    return results

def get_twitter_user_followers(user_id):
    select_by_id = "SELECT user_id, user_name, JSON_EXTRACT(user_info, '$.followers_count') AS followers_count FROM tw_user_info WHERE user_id = '"+user_id+"' and create_time >= DATE_SUB(NOW(), INTERVAL 15 DAY)"
    select_data = mysql_exe_select(select_by_id)
    result = {}
    if(select_data):
        result['id'] = select_data[0][0]
        result['name'] = select_data[0][1]
        result['followers_count'] = select_data[0][2]
        return result
    else:
        response = api.get_user(user_id=user_id)
        data = response._json
        data_str = json.dumps(json.dumps(data))[1:-1]
        sql = "INSERT INTO tw_user_info(`user_id`, `user_name`,`followers_count`, `user_info`) VALUES (\""+data['id_str']+"\",\""+data['name']+"\",\""+str(data['followers_count'])+"\", \""+data_str+"\")"
        mysql_exe_insertorupdate(sql)
    
        if "followers_count" in data:
            result['id'] = data['id']
            result['name'] = data['name']
            result['followers_count'] = data['followers_count']
            return result
        else:
            raise Exception("Unable to retrieve user data")


def get_followers_counts(user_ids):
    followers_counts = []
    for user_id in user_ids:
        try:
            followers_count = get_twitter_user_followers(user_id)
            followers_counts.append(followers_count)
        except Exception as e:
            print(e)
            result={}
            result['id'] = user_id
            result['reason'] = str(e)
            followers_counts.append(result)
            
    return followers_counts

def predict(user_ids_json):
    user_ids = json.loads(user_ids_json)
    followers_counts = get_followers_counts(user_ids)
    return json.dumps(followers_counts)

with gr.Blocks(css="") as demo:
    with gr.Row():
        input_message = gr.Textbox(show_label=False, placeholder="Please enter a list of user ids", visible=True).style(container=False)
        out_message = gr.Textbox(show_label=False, visible=True).style(container=False)
    with gr.Row():
        btn_submit = gr.Button("Submit")
    btn_submit.click(predict,input_message, out_message, api_name='submit_message')
            
demo.queue(concurrency_count=100)
demo.launch()
# iface = gr.Interface(fn=predict, inputs="text", outputs="text",api_name='getUserInfo')
# iface.launch()