-
Notifications
You must be signed in to change notification settings - Fork 0
/
LM_submit_social.py
257 lines (217 loc) · 7.36 KB
/
LM_submit_social.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 4 11:12:43 2021
@author: cgwork
"""
# Dataframes, DBs
import os
import urllib
#import webbrowser
# Dashboards modules
import dash_bootstrap_components as dbc
# for post
import requests
from dash import html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from dotenv import load_dotenv
from app import app
load_dotenv()
# Tokens
MASTODON_TOKEN = os.environ.get("MASTODON_TOKEN")
# app.logger.info(country_options)
dropdown_style = {
"marginLeft": "20px",
"marginRight": "50px",
"color": "#ffffff",
"backgroundColor": "#000000",
}
# layout
layout = html.Form(
html.Div(
style={
"fontFamily": "Sawasdee",
"fontSize": 22,
"color": "#ffffff",
"backgroundColor": "#111111",
},
children=[
html.Br(),
html.H1(style={"textAlign": "left"}, children=""),
# header
html.Br(),
html.Div(
style={
"textAlign": "left",
"fontSize": 32,
"margin": "auto",
"width": "50%",
"padding": "20px",
},
children=[
html.P("Share Your Statistics:", style={"paddingBottom": "2em"}),
html.Div(
[
html.A(
id="tw-link",
children=[
html.Img(
src=app.get_asset_url("twitter.png"),
style={
"height": "140px",
"width": "140px",
"padding": "10px",
"cursor": "pointer",
},
),
],
),
html.Img(
src=app.get_asset_url("instagram.jpg"),
style={
"height": "140px",
"width": "140px",
"padding": "10px",
},
),
html.Img(
src=app.get_asset_url("facebook.png"),
style={
"height": "140px",
"width": "140px",
"padding": "10px",
},
),
html.A(
id="a-link",
children=[
html.Img(
src=app.get_asset_url("mastodon.png"),
style={
"height": "140px",
"width": "140px",
"padding": "10px",
"cursor": "pointer",
},
)
],
),
],
style={"textAlign": "center"},
),
],
),
html.Br(),
html.Div(
dbc.Button(
style={
"fontSize": 22,
"marginLeft": "20px",
"marginRight": "80px",
"backgroundColor": "#111",
"color": "#ffffff",
},
id="submit-button-state",
n_clicks=0,
children="Restart",
color="Primary",
className="me-1",
href="/page0",
),
className="d-grip gap-2 d-md-flex justify-content-md-end",
),
html.Div(id="output-submit-social", style={"textAlign": "center"}),
html.Div(id="output-submit-social-tw", style={"textAlign": "center"}),
],
)
)
def post_to_mastodon(toot, tags):
token = MASTODON_TOKEN
headers = {}
data = {}
hashtags = tags
headers["Authorization"] = "Bearer " + token
url = "https://botsin.space/api/v1/statuses"
# toots are 500 chars max
data["status"] = toot + hashtags
data["visibility"] = "public"
retdata = requests.post(url=url, json=data, headers=headers)
return retdata
def no_data():
return 'There is no user data to post. Please click on "Restart".'
@app.callback(
Output(component_id="output-submit-social", component_property="children"),
[
Input(component_id="a-link", component_property="n_clicks"),
Input("dccstore_summary", "data"),
],
)
def mastodon(n_clicks, summary):
if n_clicks == 0 or n_clicks is None:
raise PreventUpdate
if summary is None:
return no_data()
# app.loggin.info("========")
# app.loggin.info(type(summary))
# app.loggin.info("========")
toot = (
summary["time_left"]
+ "\n\n"
+ summary["life_spent"]
+ "\n\n"
+ summary["life_compare"]
+ "\n\n"
+ summary["school"]
+ "\n\n"
+ summary["co2_stats"]
+ "\n\n"
)
poverty = str(summary["poverty"])
if len(toot + poverty) < 500:
toot += poverty
if len(toot + poverty) < 477: # all URLS are 23 chars long, all
toot += "https://datavizmultlab.herokuapp.com"
app.logger.info(toot)
# app.logger.info(toot)
tags = "\n\n#multimedia\n#databiz"
if n_clicks is None or n_clicks == 0:
raise PreventUpdate
status_code = post_to_mastodon(toot, tags)
app.logger.info("Tried posting, code = {}".format(status_code))
# webbrowser.open(url="https://botsin.space/@deadlineproject",new=2, autoraise=True)
if status_code.status_code == 200:
return "Successfully uploaded to mastodon!"
else:
return "Failed to upload to mastodon!"
@app.callback(
Output(component_id="output-submit-social-tw", component_property="children"),
# Output("twitterLink","href"),
[
Input(component_id="tw-link", component_property="n_clicks"),
Input("dccstore_summary", "data"),
],
)
def tweet(n_clicks, summary):
if n_clicks == 0 or n_clicks is None:
raise PreventUpdate
if summary is None:
return no_data()
url = "https://twitter.com/intent/tweet?text=" + urllib.parse.quote(
(
summary["time_left"]
+ "\n\n"
+ summary["life_spent"]
+ "\n\n"
+ summary["life_compare"]
+ "\n\n"
+ summary["school"]
+ "\n\n"
+ summary["co2_stats"]
)[:276]
+ " ...",
safe="/",
)
if n_clicks is None or n_clicks == 0:
raise PreventUpdate
return "" #dcc.Location(pathname=url, id="my-output")