forked from cam-garrison/doccloud-uploads-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
53 lines (40 loc) · 1.4 KB
/
main.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
"""
This Addon is used to generate a graph/chart of
upload frequency from a given user.
"""
from documentcloud.addon import AddOn
import pandas as pd
import matplotlib
class UploadGraph(AddOn):
def create_df(self, doc_dates):
df = pd.DataFrame(doc_dates, columns=["datetime"])
df["datetime"] = pd.to_datetime(df["datetime"]).dt.tz_localize(None)
df["date"] = df["datetime"].dt.date
df = df.sort_values(by="date")
df.insert(0, "count", range(1, 1 + len(df)))
return df
def main(self):
# fetch your add-on specific data
user_id = self.data.get("user_id")
user = self.client.users.get(user_id)
user_name = user.username
query = "+user:" + str(user_id)
documents = self.client.documents.search(query)
document_dates = []
for document in documents:
document_dates.append(str(document.created_at))
df = self.create_df(doc_dates=document_dates)
lines = df.plot(
y="count",
kind="line",
x="date",
title=user_name + ": Uploads Over Time",
figsize=(12, 8),
)
fig = lines.get_figure()
fig.savefig("useruploads.png")
with open("useruploads.png", "rb") as file_:
self.upload_file(file_)
self.set_message("Uploads Graph end!")
if __name__ == "__main__":
UploadGraph().main()