-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmongo.py
67 lines (54 loc) · 1.73 KB
/
mongo.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
# %%
from pymongo import MongoClient, ReturnDocument
from dotenv import load_dotenv
import os
load_dotenv()
ATLAS = os.getenv('ATLAS')
MONGO_DB_NAME = os.getenv('MONGO_DB_NAME')
def connect():
print("connecting")
client = MongoClient(f"mongodb+srv://rihp:{ATLAS}@messenger-api.lq6n5.mongodb.net/{MONGO_DB_NAME}?retryWrites=true&w=majority")
db = client.test
print("Connected")
return db
db = connect()
#%%
def newTweetObject(tweetText, messageObject, poll):
# Get Author
# Get Tweet Text
# Get Score
tweetObject = {
'_id': int(messageObject.id),
'channel': messageObject.channel.id,
'poll': int(poll.id),
'author': f'{messageObject.author}',
'content': f'{tweetText}',
'reactions': f'{messageObject.reactions}',
'created_at': f'{messageObject.created_at}',
'status': 'pending',
}
return tweetObject
def submit_tweet(tweetObject):
post_id = db.tweets.insert_one(tweetObject).inserted_id
print(post_id)
return post_id
def confirm_tweet(_id, response):
"""
Update a object in the database after tweeting it
"""
new_object = db.tweets.find_one_and_update({'_id': _id},
{ '$set': { "status" : "tweeted",
"response": response} },
#TODO: This database should keep the URL of the tweet sent
)
return new_object
# %%
def count_submissions():
return db.tweets.count()
# %%
def nukeDB():
cursor = db.tweets.find({})
for element in cursor:
print(element)
db.tweets.delete_many( {'_id': element['_id']} )
print(' ~ Deleted all tweets on database')