This repository has been archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgabgroups.py
executable file
·57 lines (36 loc) · 1.58 KB
/
gabgroups.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
#!/usr/bin/python3
import json
import sys
import argparse
from pymongo import *
#
# this tool collects group data from scraped posts from gab.ai.
# it is part of the Gabber toolset.
# see https://github.com/utrecht-data-school/gabber
#
def main(argv):
# getting command line parameters
parser = argparse.ArgumentParser()
parser.add_argument('-r','--reposts', dest='reposts', help='Include reposts as edges in the graph', action='store_true', required=False, default=False)
args = parser.parse_args()
# setting up the database
db = MongoClient().gab
# create a fresh new collection for the groups and make sure they are unique
db.groups.drop()
db.groups.create_index('id', unique=True)
# depending on whether we include reposts, either find everything with group metadata, or just the original posts
if args.reposts:
groups = db.posts.find({'post.group':{"$exists":1}},{'post.group':1}, no_cursor_timeout=True)
else:
groups = db.posts.find({'type':'post','post.group':{"$exists":1}},{'post.group':1}, no_cursor_timeout=True)
# loop over all the posts with group metadata we found
for group in groups:
try:
# try to insert the group, this will fail if the group is already in the collection
group['post']['group']['postcount'] = 1
db.groups.insert_one(group['post']['group'])
except:
# if the insert failed, the group was already in the collection and we just need to increment the postcount
db.groups.update({'id':group['post']['group']['id']},{"$inc":{'postcount':1}})
if __name__ == "__main__":
main(sys.argv[1:])