File size: 2,190 Bytes
dd83214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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))