-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
43 lines (35 loc) · 1.6 KB
/
script.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
import os
from pymongo import MongoClient
import pandas as pd
from bson.objectid import ObjectId
# converts mongodb collection to csv
def convert_collection_to_df(mongo_cli, collection, field1, match1,
field2, match2):
dbcli = mongo_cli
scrader_db = dbcli['scrader']
cursor = scrader_db[collection].find({'$and': [{field1: {'$in': match1}},
{field2: {'$in': match2}}]},
{'_id': True})
cursor_list = list(cursor)
print(len(cursor_list))
for article in cursor_list:
scrader_db[collection].\
update({"_id": ObjectId(article['_id'])},
{'$set': {'appended': True}})
# scrader_db[collection].update({'$and': [{field1: {'$in': match1}},
# {field2: {'$in': match2}}]},
# {'$set': {'appended': True}},
# True, True)
return pd.DataFrame(cursor_list)
def main():
dataframe = convert_collection_to_df(MongoClient(), 'dev_articles',
'checked', [True],
'appended', [False])
# if file does not exist write header
if not os.path.isfile('scraderdata.csv'):
dataframe.to_csv('scraderdata.csv', encoding='utf-8')
else: # else it exists so append without writing the header
dataframe.to_csv('scraderdata.csv', mode='a', header=False,
encoding='utf-8')
if __name__ == '__main__':
main()