forked from vmware-archive/gp-magic-query
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgettweet4lang.py
31 lines (23 loc) · 1003 Bytes
/
gettweet4lang.py
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
import psycopg2
import psycopg2.extras
import argparse
parser = argparse.ArgumentParser(prog='gettweet4lang')
parser.add_argument('--lang', help='2 letter lang code',required=True)
args = parser.parse_args()
print ("Using language: ", args.lang)
try:
# You can also explicitly pass parameters like username, password,etc
# this code assumes PGHOST, PGPASSWORD, PGUSER are set in environment
connection = psycopg2.connect(database = "twitter")
cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
# parameterized query in psycopg2 follows pep-0249 standard
cursor.execute("SELECT * from tweets where full_text is not null and lang = %(langvar)s limit 5",
{'langvar':args.lang})
for record in cursor:
print("TWEET: ", record['full_text'])
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
if(connection):
cursor.close()
connection.close()