-
Notifications
You must be signed in to change notification settings - Fork 0
/
analytics_published_datetime.py
88 lines (73 loc) · 2.23 KB
/
analytics_published_datetime.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'''
Check This Link: https://plotly.com/python/
'''
import sqlite3
import json
import numpy as np
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from datetime import datetime
db_file_path = './volume/archive.db'
plt_config = {
'displayModeBar': True,
'displaylogo': False,
'autosizable': True,
'responsive': True,
'modeBarButtonsToAdd': ['zoom', 'pan'],
'toImageButtonOptions': {
'format': 'jpeg',
}
}
# Import Categories
with open('./statics/fars_news_categories.json', encoding="utf-8") as file:
categories = json.load(file)
def count_articles_per_month():
db_connection = sqlite3.connect(db_file_path)
db_cursor = db_connection.cursor()
query = "SELECT COUNT(*), published_datetime FROM news GROUP BY STRFTIME('%m-%Y', published_datetime) ORDER BY published_datetime ASC;"
db_cursor.execute(query)
result = db_cursor.fetchall()
db_connection.commit()
db_connection.close()
return result
def main():
chart_data_values = []
chart_data_labels = []
result = count_articles_per_month()
for month in result:
chart_data_values.append(month[0])
date_time = datetime.strptime(month[1], '%Y-%m-%d %H:%M:%S')
chart_data_labels.append(date_time)
fig = go.Figure(data=[
go.Bar(x=chart_data_labels, y=chart_data_values, text=chart_data_values, textposition='outside')
])
fig.update_layout(
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1,
label="1m",
step="month",
stepmode="backward"),
dict(count=6,
label="6m",
step="month",
stepmode="backward"),
dict(step="all")
])
),
rangeslider=dict(
visible=False
),
type="date"
),
title_font_family="IRANSansWeb",
font_family="IRANSansWeb",
uniformtext_minsize=12,
hoverlabel=dict(
font_family="IRANSansWeb"
)
)
fig.show(config=plt_config)
if __name__ == '__main__':
main()