forked from harmony-community-node/harmony_one_tip_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore.py
100 lines (88 loc) · 3.92 KB
/
datastore.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import pymongo
from pymongo import MongoClient
class DataStore:
client = None
db = None
usersData = u'usersData'
tweetData = u'tweetData'
twitter_events = u'twitter_events'
def __init__(self):
self.client = MongoClient()
self.db = self.client.one_tip_bot_data
def checkIfUserRecordExists(self, chat_id, telegram_user_id):
userDataCollection = self.db.usersData
doc_ref = userDataCollection.find({"$and":[{'chat_id' : chat_id}, {'telegram_user_id': telegram_user_id}]})
if doc_ref.count() > 0:
return True
else:
return False
def checkIfUserRecordExistsWithTwitter(self, twitter_handle):
userDataCollection = self.db.usersData
doc_ref = userDataCollection.find({'twitter_handle': twitter_handle})
if doc_ref.count() > 0:
return True
else:
return False
def saveUserDetails(self, userDetails):
if 'chat_id' in userDetails and 'telegram_user_id' in userDetails:
userDataCollection = self.db.usersData
doc_ref = userDataCollection.find({"$and":[{'chat_id' : userDetails['chat_id']}, {'telegram_user_id': userDetails['telegram_user_id']}]})
if doc_ref.count() == 0:
#print(f" add {addressData}")
userDataCollection.insert_one(userDetails)
else:
#print(f"update {addressData}")
userDataCollection.update({ "$and" : [{'chat_id' : userDetails['chat_id']}, {'telegram_user_id': userDetails['telegram_user_id']}]}, userDetails)
def getUserDetails(self, chat_id, telegram_user_id):
userDataCollection = self.db.usersData
doc_ref = userDataCollection.find({"$and":[{'chat_id' : chat_id}, {'telegram_user_id': telegram_user_id}]})
if doc_ref.count() == 0:
return None
else:
return doc_ref[0]
def getUserDetailsByTwitterHandle(self, twitter_handle):
userDataCollection = self.db.usersData
doc_ref = userDataCollection.find({'twitter_handle': twitter_handle})
if doc_ref.count() == 0:
return None
else:
return doc_ref[0]
def checkIftweetDataExists(self, tweet_id):
tweetDataCollection = self.db.tweetData
doc_ref = tweetDataCollection.find({'tweet_id': tweet_id})
if doc_ref.count() > 0:
return True
else:
return False
def getTweetDetails(self, tweet_id):
tweetDataCollection = self.db.tweetData
doc_ref = tweetDataCollection.find({'tweet_id': tweet_id})
if doc_ref.count() == 0:
return None
else:
return doc_ref[0]
def saveTweetDetails(self, tweetDetails):
if 'tweet_id' in tweetDetails:
tweet_id = tweetDetails['tweet_id']
tweetDataCollection = self.db.tweetData
doc_ref = tweetDataCollection.find({'tweet_id': tweet_id})
if doc_ref.count() == 0:
print(f" tweet add {tweetDetails}")
tweetDataCollection.insert_one(tweetDetails)
else:
print(f" tweet update {tweetDetails}")
tweetDataCollection.update({'tweet_id': tweet_id}, tweetDetails)
def getNotAddressedTwitterEvents(self):
twitterEventDataCollection = self.db.twitter_events
doc_ref = twitterEventDataCollection.find({'addressed': False})
if doc_ref.count() == 0:
return None
else:
return doc_ref[0]
def saveTwitterEventDetails(self, event_id, addressed):
twitterEventDataCollection = self.db.twitter_events
doc_ref = twitterEventDataCollection.find({'event_id': event_id})
if doc_ref.count() == 0:
print(f" twitter event not found add")
else:
twitterEventDataCollection.update({'event_id': event_id}, {'addressed' : addressed})