1seok2

feat: update database

...@@ -4,17 +4,19 @@ from selenium.webdriver.common.keys import Keys ...@@ -4,17 +4,19 @@ from selenium.webdriver.common.keys import Keys
4 from selenium.webdriver.chrome.options import Options 4 from selenium.webdriver.chrome.options import Options
5 from config.admin import ID, PW, LOCAL_PROJECT_PATH 5 from config.admin import ID, PW, LOCAL_PROJECT_PATH
6 from config.URLs import INSTAGRAM_URL 6 from config.URLs import INSTAGRAM_URL
7 -from config.firebase import update_data
8 7
9 8
10 -def check_people(driver, type): 9 +def get_description(driver):
10 + element = driver.find_element_by_css_selector('.Igw0E .IwRSH .eGOV_ ._4EzTm')
11 + data = element.get_attribute('innerHTML')
12 + return str(data)
13 +
14 +
15 +def check_people(driver):
11 result = [] 16 result = []
12 navigations = driver.find_elements_by_class_name('-nal3') 17 navigations = driver.find_elements_by_class_name('-nal3')
13 18
14 - if type == "followers": 19 + navigations[1].click()
15 - navigations[1].click()
16 - elif type == "following":
17 - navigations[2].click()
18 time.sleep(2) 20 time.sleep(2)
19 21
20 elem = driver.find_elements_by_css_selector('.Jv7Aj ._0imsa') 22 elem = driver.find_elements_by_css_selector('.Jv7Aj ._0imsa')
...@@ -36,8 +38,10 @@ def login(driver): ...@@ -36,8 +38,10 @@ def login(driver):
36 38
37 39
38 def get_list(insta_id, driver): 40 def get_list(insta_id, driver):
41 + desc = get_description(driver)
42 +
39 # check followers 43 # check followers
40 - followers_list = check_people(driver, "followers") 44 + followers_list = check_people(driver)
41 45
42 # close followers 46 # close followers
43 driver.find_element_by_css_selector('.WaOAr .wpO6b').click() 47 driver.find_element_by_css_selector('.WaOAr .wpO6b').click()
...@@ -47,8 +51,9 @@ def get_list(insta_id, driver): ...@@ -47,8 +51,9 @@ def get_list(insta_id, driver):
47 data = { 51 data = {
48 "followers": followers_list, 52 "followers": followers_list,
49 "insta_id": insta_id, 53 "insta_id": insta_id,
54 + "description": desc,
55 + "follower_num": len(followers_list)
50 } 56 }
51 - update_data(insta_id, data)
52 57
53 return data 58 return data
54 59
......
1 +def update_users_data(data):
...\ No newline at end of file ...\ No newline at end of file
...@@ -25,4 +25,4 @@ def search(insta_id): ...@@ -25,4 +25,4 @@ def search(insta_id):
25 if __name__ == '__main__': 25 if __name__ == '__main__':
26 os.putenv('NLS_LANG', '.UTF8') 26 os.putenv('NLS_LANG', '.UTF8')
27 cx_Oracle.init_oracle_client(lib_dir="/Users/choewonseog/Downloads/instantclient_19_8") 27 cx_Oracle.init_oracle_client(lib_dir="/Users/choewonseog/Downloads/instantclient_19_8")
28 - print(search('__tester2__').data)
...\ No newline at end of file ...\ No newline at end of file
28 + print(search('__tester2__').data)
......
1 from crawler.crawler_instagram import crawler_instagram 1 from crawler.crawler_instagram import crawler_instagram
2 +import json
3 +import cx_Oracle
4 +import os
5 +from config.key import DATABASE_ID, DATABASE_PW
6 +from config.URLs import DATABASE_URL
2 from flask import jsonify 7 from flask import jsonify
3 8
4 9
10 +# data = {
11 +# "followers": followers_list,
12 +# "insta_id": insta_id,
13 +# "description": desc,
14 +# "follower_num": len(followers_list)
15 +# }
16 +
5 def update(insta_id): 17 def update(insta_id):
6 data = {} 18 data = {}
19 +
7 try: 20 try:
8 data = crawler_instagram(insta_id) 21 data = crawler_instagram(insta_id)
9 except Exception as e: 22 except Exception as e:
10 print(e) 23 print(e)
11 24
25 + connection = cx_Oracle.connect(DATABASE_ID, DATABASE_PW, DATABASE_URL)
26 + cursor = connection.cursor()
27 +
28 + user_query = """
29 + select * from users where user_id=\'%s\'
30 + """ % insta_id
31 +
32 + cursor.execute(user_query) # user가 존재하는지 확인
33 + exist_user = cursor.fetchall()
34 +
35 + query = ""
36 +
37 + if len(exist_user) == 0:
38 + query = f"""
39 + insert into
40 + users(user_id, description, follower_num)
41 + values(
42 + \'{data.user_id}\',
43 + \'{data.description}\',
44 + \'{data.follower_num}\'
45 + )
46 + """
47 + else:
48 + query = f"""
49 + update users set
50 + user_id = \'{data.user_id}\',
51 + description = \'{data.description}\',
52 + follower_num = \'{data.follower_num}\'
53 + """
54 +
55 + cursor.execute(query)
56 +
57 + cursor.close()
58 +
12 return jsonify(data) 59 return jsonify(data)
...\ No newline at end of file ...\ No newline at end of file
......
1 import cx_Oracle 1 import cx_Oracle
2 import os 2 import os
3 from flask import Flask, render_template, request, send_from_directory 3 from flask import Flask, render_template, request, send_from_directory
4 -from database.connect import connectDB
5 4
6 from router.update import update 5 from router.update import update
7 from router.search import search 6 from router.search import search
......