1seok2

feat: search & update DB

...@@ -7,8 +7,8 @@ from config.URLs import INSTAGRAM_URL ...@@ -7,8 +7,8 @@ from config.URLs import INSTAGRAM_URL
7 7
8 8
9 def get_description(driver): 9 def get_description(driver):
10 - element = driver.find_element_by_css_selector('.Igw0E .IwRSH .eGOV_ ._4EzTm') 10 + element = driver.find_element_by_css_selector('.-vDIg')
11 - data = element.get_attribute('innerHTML') 11 + data = element.text
12 return str(data) 12 return str(data)
13 13
14 14
...@@ -50,7 +50,7 @@ def get_list(insta_id, driver): ...@@ -50,7 +50,7 @@ def get_list(insta_id, driver):
50 # update at firebase 50 # update at firebase
51 data = { 51 data = {
52 "followers": followers_list, 52 "followers": followers_list,
53 - "insta_id": insta_id, 53 + "user_id": insta_id,
54 "description": desc, 54 "description": desc,
55 "follower_num": len(followers_list) 55 "follower_num": len(followers_list)
56 } 56 }
...@@ -66,9 +66,7 @@ def crawler_instagram(insta_id): ...@@ -66,9 +66,7 @@ def crawler_instagram(insta_id):
66 driver.get(url=INSTAGRAM_URL) 66 driver.get(url=INSTAGRAM_URL)
67 time.sleep(2) 67 time.sleep(2)
68 68
69 - print('hi')
70 login(driver) 69 login(driver)
71 - print(insta_id)
72 time.sleep(2) 70 time.sleep(2)
73 71
74 url = "%s/%s" % (INSTAGRAM_URL, insta_id) 72 url = "%s/%s" % (INSTAGRAM_URL, insta_id)
......
...@@ -10,19 +10,31 @@ def search(insta_id): ...@@ -10,19 +10,31 @@ def search(insta_id):
10 connection = cx_Oracle.connect(DATABASE_ID, DATABASE_PW, DATABASE_URL) 10 connection = cx_Oracle.connect(DATABASE_ID, DATABASE_PW, DATABASE_URL)
11 cursor = connection.cursor() 11 cursor = connection.cursor()
12 12
13 - query = """ 13 + cursor.execute(f"""
14 - select * from users where user_id=\'%s\' 14 + select * from users where user_id=\'{insta_id}\'
15 - """ % insta_id 15 + """)
16 - 16 + exist_user = cursor.fetchall()
17 - cursor.execute(query) 17 + print(exist_user)
18 - exist = cursor.fetchall() 18 + cursor.execute(f"""
19 + select * from followers where user_id=\'{insta_id}\'
20 + """)
21 + exist_followers = cursor.fetchall()
22 + print(exist_followers)
23 + exist_info = ()
24 + if len(exist_user) > 0:
25 + exist_info = {
26 + "user": exist_user[0],
27 + "follower": exist_followers
28 + }
29 +
30 + print(exist_info)
19 31
20 cursor.close() 32 cursor.close()
21 33
22 - return Response(json.dumps(exist), mimetype='application/json') 34 + return Response(json.dumps(exist_info), mimetype='application/json')
23 35
24 36
25 if __name__ == '__main__': 37 if __name__ == '__main__':
26 os.putenv('NLS_LANG', '.UTF8') 38 os.putenv('NLS_LANG', '.UTF8')
27 cx_Oracle.init_oracle_client(lib_dir="/Users/choewonseog/Downloads/instantclient_19_8") 39 cx_Oracle.init_oracle_client(lib_dir="/Users/choewonseog/Downloads/instantclient_19_8")
28 - print(search('__tester2__').data) 40 + print(search('__re.mind.er__').data)
......
...@@ -4,14 +4,14 @@ import cx_Oracle ...@@ -4,14 +4,14 @@ import cx_Oracle
4 import os 4 import os
5 from config.key import DATABASE_ID, DATABASE_PW 5 from config.key import DATABASE_ID, DATABASE_PW
6 from config.URLs import DATABASE_URL 6 from config.URLs import DATABASE_URL
7 -from flask import jsonify 7 +from flask import Response
8 8
9 9
10 # data = { 10 # data = {
11 -# "followers": followers_list, 11 +# "followers": [followers_list],
12 # "insta_id": insta_id, 12 # "insta_id": insta_id,
13 # "description": desc, 13 # "description": desc,
14 -# "follower_num": len(followers_list) 14 +# "follower_num": len([followers_list])
15 # } 15 # }
16 16
17 def update(insta_id): 17 def update(insta_id):
...@@ -29,31 +29,54 @@ def update(insta_id): ...@@ -29,31 +29,54 @@ def update(insta_id):
29 select * from users where user_id=\'%s\' 29 select * from users where user_id=\'%s\'
30 """ % insta_id 30 """ % insta_id
31 31
32 - cursor.execute(user_query) # user가 존재하는지 확인 32 + cursor.execute(user_query) # user가 존재하는지 확인
33 exist_user = cursor.fetchall() 33 exist_user = cursor.fetchall()
34 + print(exist_user)
34 35
36 + ### update users table ###
35 query = "" 37 query = ""
36 -
37 if len(exist_user) == 0: 38 if len(exist_user) == 0:
38 - query = f""" 39 + print('insert new')
40 + cursor.execute(f"""
39 insert into 41 insert into
40 - users(user_id, description, follower_num) 42 + users (user_id, description, follower_num)
41 - values( 43 + values (
42 - \'{data.user_id}\', 44 + \'{data['user_id']}\',
43 - \'{data.description}\', 45 + \'{data['description'][0:254]}\',
44 - \'{data.follower_num}\' 46 + \'{data['follower_num']}\'
45 ) 47 )
46 - """ 48 + """)
47 else: 49 else:
48 - query = f""" 50 + print('update user')
51 + cursor.execute(f"""
49 update users set 52 update users set
50 - user_id = \'{data.user_id}\', 53 + description = \'{data['description'][0:254]}\',
51 - description = \'{data.description}\', 54 + follower_num = \'{data['follower_num']}\'
52 - follower_num = \'{data.follower_num}\' 55 + where user_id = \'{data['user_id']}\'
53 - """ 56 + """)
57 +
58 + ### update followers table ###
59 + for follower in data['followers']:
60 + print(follower)
61 + cursor.execute("""
62 + select * from followers where follower_id=\'%s\'
63 + """ % follower)
54 64
55 - cursor.execute(query) 65 + exist_follower = cursor.fetchall()
56 66
67 + if len(exist_follower) == 0:
68 + print('insert follower')
69 + cursor.execute(f"""
70 + insert into
71 + followers (follower_id, user_id, description)
72 + values (\'{follower}\', \'{data['user_id']}\', \'\')
73 + """)
57 cursor.close() 74 cursor.close()
58 75
59 - return jsonify(data)
...\ No newline at end of file ...\ No newline at end of file
76 + return Response(json.dumps(data), mimetype='application/json')
77 +
78 +
79 +if __name__ == '__main__':
80 + os.putenv('NLS_LANG', '.UTF8')
81 + cx_Oracle.init_oracle_client(lib_dir="/Users/choewonseog/Downloads/instantclient_19_8")
82 + update('__re.mind.er__')
......