-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
74 lines (55 loc) · 2.13 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from os import environ
from sqlalchemy import create_engine, Column, String, Integer, BigInteger
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from Bot.bot import *
Base = declarative_base()
Consumer_key_bot = environ['Consumer_key_bot']
Consumer_key_Secret_bot = environ['Consumer_key_Secret_bot']
Api_key_bot = environ['Api_key_bot']
Api_key_Secret_bot = environ['Api_key_Secret_bot']
class User(Base):
__tablename__ = 'user'
id = Column('id', Integer, primary_key=True)
user = Column('user_id', String)
tweet = Column('tweet', String)
class Last(Base):
__tablename__ = 'last'
id = Column('id', Integer, primary_key=True)
lastid = Column('lastid', BigInteger)
engine = create_engine('postgresql+psycopg2://dncayxqe:[email protected]/dncayxqe', echo=True)
Base.metadata.create_all(bind=engine)
Session = sessionmaker(bind=engine)
session = Session()
def update_db(thread_text, user):
new_user = User()
new_user.user = user
new_user.tweet = thread_text
session.add(new_user)
session.commit()
def get_last_seen_id():
last_seen = session.query(Last).first()
return last_seen.lastid
def set_last_seen_id(last_seen_id):
last_seen = session.query(Last).first()
last_seen.lastid = last_seen_id
session.commit()
return
if __name__ == '__main__':
auth_api = authenticate(Consumer_key_bot,Consumer_key_Secret_bot,Api_key_bot,Api_key_Secret_bot)
while True:
last_seen_id = get_last_seen_id()
last_seen_id, status_id, author_id, user,is_it_tweet = get_mentioned_thread(auth_api, last_seen_id)
if last_seen_id == 0 and status_id == 0 and author_id == 0 and user == 0:
continue
set_last_seen_id(last_seen_id)
if is_it_tweet==1:
thread = get_tweet_text(auth_api,status_id)
else:
thread = get_thread_text(auth_api, author_id, status_id)
try:
auth_api.send_direct_message(user,thread)
except Exception as e:
print(e)
finally:
update_db(thread, user)