| import psycopg2 | |
| import os | |
| from loguru import logger | |
| from dotenv import load_dotenv | |
| from loguru import logger | |
| import psycopg2.extras | |
| load_dotenv() | |
| class PsqlDataBaseConnection: | |
| def __init__(self): | |
| self.connection = None | |
| def __enter__(self): | |
| self.connection = psycopg2.connect( | |
| host=os.environ['DATABASE_URL'], | |
| database=os.environ['DATABASE_NAME'], | |
| user=os.environ['DATABASE_USERNAME'], | |
| password=os.environ['DATABASE_PASSWORD'], | |
| sslmode='require') | |
| self.connection.autocommit = True | |
| return self.connection | |
| def __exit__(self, exc_type, exc_val, exc_tb): | |
| if self.connection: | |
| self.connection.close() | |
| else: | |
| logger.error(exc_type, exc_val, exc_tb, sep="\n") | |
| return True | |
| with PsqlDataBaseConnection() as connection : | |
| stm = "select keyword from perigee_consolidated_events " \ | |
| "where id in (select fk_consolidated from gdacs where fk_consolidated='JMBA3aZhF8wqfykfMomSj9') " \ | |
| " or id in (select fk_consolidated from copernicus_ems where fk_consolidated='JMBA3aZhF8wqfykfMomSj9' ) " \ | |
| "or id in (select fk_consolidated from rw_disaster where fk_consolidated='JMBA3aZhF8wqfykfMomSj9') " \ | |
| "or id in (select fk_consolidated from glide_numbers where fk_consolidated='JMBA3aZhF8wqfykfMomSj9')" | |
| cursor = connection.cursor() | |
| cursor.execute(stm) | |
| res = cursor.fetchall()[0][0] | |
| print(res) | |
| stm2 = f"select user_id from direct_engagement " \ | |
| f"where user_id in (select id from twitter_user where verified=true) " \ | |
| f"and keyword='{res}'" | |
| cursor.execute(stm2) | |
| res2 = cursor.fetchall() | |
| x = [item[0] for item in res2] | |
| f = tuple(x) | |
| stm3 = "select user_id from baseline_tweets INNER JOIN twitter_user on baseline_tweets.user_id=twitter_user.id " \ | |
| "where baseline_tweets.mentioned_country='COD' " \ | |
| "and twitter_user.verified=true " | |
| cursor.execute(stm3) | |
| res3 = cursor.fetchall() | |
| z = [item[0] for item in res3] | |
| print(f"me : {z}") | |
| print(list(set(z).symmetric_difference(set(x)))) | |
| print(len(z)) | |