1seok2

feat: search & update DB

......@@ -7,8 +7,8 @@ from config.URLs import INSTAGRAM_URL
def get_description(driver):
element = driver.find_element_by_css_selector('.Igw0E .IwRSH .eGOV_ ._4EzTm')
data = element.get_attribute('innerHTML')
element = driver.find_element_by_css_selector('.-vDIg')
data = element.text
return str(data)
......@@ -50,7 +50,7 @@ def get_list(insta_id, driver):
# update at firebase
data = {
"followers": followers_list,
"insta_id": insta_id,
"user_id": insta_id,
"description": desc,
"follower_num": len(followers_list)
}
......@@ -66,9 +66,7 @@ def crawler_instagram(insta_id):
driver.get(url=INSTAGRAM_URL)
time.sleep(2)
print('hi')
login(driver)
print(insta_id)
time.sleep(2)
url = "%s/%s" % (INSTAGRAM_URL, insta_id)
......
......@@ -10,19 +10,31 @@ def search(insta_id):
connection = cx_Oracle.connect(DATABASE_ID, DATABASE_PW, DATABASE_URL)
cursor = connection.cursor()
query = """
select * from users where user_id=\'%s\'
""" % insta_id
cursor.execute(query)
exist = cursor.fetchall()
cursor.execute(f"""
select * from users where user_id=\'{insta_id}\'
""")
exist_user = cursor.fetchall()
print(exist_user)
cursor.execute(f"""
select * from followers where user_id=\'{insta_id}\'
""")
exist_followers = cursor.fetchall()
print(exist_followers)
exist_info = ()
if len(exist_user) > 0:
exist_info = {
"user": exist_user[0],
"follower": exist_followers
}
print(exist_info)
cursor.close()
return Response(json.dumps(exist), mimetype='application/json')
return Response(json.dumps(exist_info), mimetype='application/json')
if __name__ == '__main__':
os.putenv('NLS_LANG', '.UTF8')
cx_Oracle.init_oracle_client(lib_dir="/Users/choewonseog/Downloads/instantclient_19_8")
print(search('__tester2__').data)
print(search('__re.mind.er__').data)
......
......@@ -4,14 +4,14 @@ import cx_Oracle
import os
from config.key import DATABASE_ID, DATABASE_PW
from config.URLs import DATABASE_URL
from flask import jsonify
from flask import Response
# data = {
# "followers": followers_list,
# "followers": [followers_list],
# "insta_id": insta_id,
# "description": desc,
# "follower_num": len(followers_list)
# "follower_num": len([followers_list])
# }
def update(insta_id):
......@@ -29,31 +29,54 @@ def update(insta_id):
select * from users where user_id=\'%s\'
""" % insta_id
cursor.execute(user_query) # user가 존재하는지 확인
cursor.execute(user_query) # user가 존재하는지 확인
exist_user = cursor.fetchall()
print(exist_user)
### update users table ###
query = ""
if len(exist_user) == 0:
query = f"""
print('insert new')
cursor.execute(f"""
insert into
users(user_id, description, follower_num)
values(
\'{data.user_id}\',
\'{data.description}\',
\'{data.follower_num}\'
users (user_id, description, follower_num)
values (
\'{data['user_id']}\',
\'{data['description'][0:254]}\',
\'{data['follower_num']}\'
)
"""
""")
else:
query = f"""
print('update user')
cursor.execute(f"""
update users set
user_id = \'{data.user_id}\',
description = \'{data.description}\',
follower_num = \'{data.follower_num}\'
"""
description = \'{data['description'][0:254]}\',
follower_num = \'{data['follower_num']}\'
where user_id = \'{data['user_id']}\'
""")
### update followers table ###
for follower in data['followers']:
print(follower)
cursor.execute("""
select * from followers where follower_id=\'%s\'
""" % follower)
cursor.execute(query)
exist_follower = cursor.fetchall()
if len(exist_follower) == 0:
print('insert follower')
cursor.execute(f"""
insert into
followers (follower_id, user_id, description)
values (\'{follower}\', \'{data['user_id']}\', \'\')
""")
cursor.close()
return jsonify(data)
\ No newline at end of file
return Response(json.dumps(data), mimetype='application/json')
if __name__ == '__main__':
os.putenv('NLS_LANG', '.UTF8')
cx_Oracle.init_oracle_client(lib_dir="/Users/choewonseog/Downloads/instantclient_19_8")
update('__re.mind.er__')
......