-
Notifications
You must be signed in to change notification settings - Fork 0
/
lybot.py
54 lines (40 loc) · 1.36 KB
/
lybot.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import psycopg2
from datetime import datetime
import random as rd
import tweepy
from tweepy import OAuthHandler
# Fetch config vars
CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
ACCESS_KEY = os.environ['ACCESS_KEY']
ACCESS_SECRET = os.environ['ACCESS_SECRET']
DATABASE_URL = os.environ['DATABASE_URL']
# Setup db connection
connection = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = connection.cursor()
# Twitter auth
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
try:
postgreSQL_select_Query = "select * from lyrics ORDER BY last_time NULLS FIRST fetch first 100 rows only"
cursor.execute(postgreSQL_select_Query)
query = cursor.fetchall()
aux = rd.randint(0, 100)
index, lyric, album, date = query[aux]
date = datetime.now()
sql_update_query = """Update lyrics set last_time = %s where id = %s"""
cursor.execute(sql_update_query, (date, index))
api.update_status(lyric)
connection.commit()
print ("Tweet sent!");
except (Exception, psycopg2.Error) as error:
if(connection):
print("Error: ", error)
finally:
# Closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")