-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
166 lines (160 loc) · 4.34 KB
/
app.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { main } from "./instagram.js";
// import { data } from "./data.js";
const getHahtag = (item) => item.caption.match(/#[\p{L}]+/giu);
async function myAction(input) {
if (input?.value) {
let data = await main(input.value);
document.getElementById("follower-count").innerText =
data?.profile?.follower_count || "Not Available";
document.getElementById("following-count").innerText =
data?.profile?.following_count || "Not Available";
document.getElementById("media-count").innerText =
data?.profile?.media_count || "Not Available";
document.getElementById("er").innerText =
data?.engagement_rate_avg || "Not Available";
document.getElementById("placeholder_name").innerText =
data?.profile?.full_name.charAt(0);
document.getElementById("full_name").innerText = data?.profile?.full_name;
document.getElementById("biography").innerText = data?.profile?.biography;
// wordcloud
let hashtags = [].concat
.apply([], [...data.posts.map(getHahtag)])
.filter((e) => e != null);
let cloud = hashtags.reduce((arr, word) => {
let obj = Highcharts.find(arr, (obj) => obj.name === word);
if (obj) {
obj.weight += 1;
} else {
obj = {
name: word,
weight: 1,
};
arr.push(obj);
}
return arr;
}, []);
Highcharts.seriesTypes.wordcloud.prototype.deriveFontSize = function (
relativeWeight
) {
var maxFontSize = 60;
// Will return a fontSize between 0px and 25px.
return Math.floor(maxFontSize * relativeWeight);
};
Highcharts.chart("hashtags", {
accessibility: {
screenReaderSection: {
beforeChartFormat:
"<h5>{chartTitle}</h5>" +
"<div>{chartSubtitle}</div>" +
"<div>{chartLongdesc}</div>" +
"<div>{viewTableButton}</div>",
},
},
series: [
{
type: "wordcloud",
data: cloud,
name: "Occurrences",
rotation: {
from: 0,
to: 0,
},
spiral: "rectangular",
placementStrategy: "center",
},
],
title: {
text: "Frequently Used Hashtags",
},
tooltip: {
headerFormat:
'<span style="font-size: 16px"><b>{point.key}</b></span><br>',
},
});
// post engagement timeline
let posts = data?.posts || [];
let er = [];
let comments = [];
let likers = [];
for (const post of posts) {
let postDate = new Date(post.taken_at * 1000);
er.push([
Date.UTC(
postDate.getUTCFullYear(),
postDate.getUTCMonth(),
postDate.getDay()
),
post.engagement_rate,
]);
// comments.push([
// Date.UTC(
// postDate.getUTCFullYear(),
// postDate.getUTCMonth(),
// postDate.getDay()
// ),
// post.comments,
// ]);
// likers.push([
// Date.UTC(
// postDate.getUTCFullYear(),
// postDate.getUTCMonth(),
// postDate.getDay()
// ),
// post.like,
// ]);
}
er.sort((a, b) => a[0] - b[0]);
// comments.sort((a, b) => a[0] - b[0]);
// likers.sort((a, b) => a[0] - b[0]);
Highcharts.chart("container", {
chart: {
type: "line",
},
title: {
text: "Post Engagement Timeline",
},
yAxis: {
title: {
text: "Enagement Score",
},
},
xAxis: {
type: "datetime",
dateTimeLabelFormats: {
// don't display the dummy year
month: "%e. %b",
year: "%b",
},
title: {
text: "Post Publish Date",
},
},
colors: ["#2A0944", "#3FA796", "#FEC260", "#A10035", "#000"],
plotOptions: {
series: {
marker: {
enabled: true,
radius: 2.5,
},
},
},
series: [
{
name: "Engagement Rate",
data: er,
},
// {
// name: "Likers",
// data: likers,
// },
// {
// name: "Commenters",
// data: comments,
// },
],
});
}
}
document.getElementById("ok_btn").addEventListener("click", function () {
myAction(document.getElementById("username")).then();
});