Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mxrch
GitHub Repository: mxrch/GHunt
Path: blob/master/ghunt/helpers/drive.py
252 views
1
from typing import *
2
3
from ghunt.parsers.drive import DriveComment, DriveCommentList, DriveCommentReply, DriveFile
4
from ghunt.objects.base import DriveExtractedUser
5
from ghunt.helpers.utils import oprint # TEMP
6
7
8
def get_users_from_file(file: DriveFile) -> List[DriveExtractedUser]:
9
"""
10
Extracts the users from the permissions of a Drive file,
11
and the last modifying user.
12
"""
13
14
users: Dict[str, DriveExtractedUser] = {}
15
for perms in [file.permissions, file.permissions_summary.select_permissions]:
16
for perm in perms:
17
if not perm.email_address:
18
continue
19
#oprint(perm)
20
user = DriveExtractedUser()
21
user.email_address = perm.email_address
22
user.gaia_id = perm.user_id
23
user.name = perm.name
24
user.role = perm.role
25
users[perm.email_address] = user
26
27
# Last modifying user
28
target_user = file.last_modifying_user
29
if target_user.id:
30
email = target_user.email_address
31
if not email:
32
email = target_user.email_address_from_account
33
if not email:
34
return users
35
36
if email in users:
37
users[email].is_last_modifying_user = True
38
39
return list(users.values())
40
41
def get_comments_from_file(comments: DriveCommentList) -> List[Tuple[str, Dict[str, any]]]:
42
"""
43
Extracts the comments and replies of a Drive file.
44
"""
45
46
def update_stats(authors: List[Dict[str, Dict[str, any]]], comment: DriveComment|DriveCommentReply):
47
name = comment.author.display_name
48
pic_url = comment.author.picture.url
49
key = f"{name}${pic_url}" # Two users can have the same name, not the same picture URL (I hope so)
50
# So we do this to make users "unique"
51
if key not in authors:
52
authors[key] = {
53
"name": name,
54
"pic_url": pic_url,
55
"count": 0
56
}
57
authors[key]["count"] += 1
58
59
authors: Dict[str, Dict[str, any]] = {}
60
for comment in comments.items:
61
update_stats(authors, comment)
62
for reply in comment.replies:
63
update_stats(authors, reply)
64
65
return sorted(authors.items(), key=lambda k_v: k_v[1]['count'], reverse=True)
66