-
Notifications
You must be signed in to change notification settings - Fork 7
/
publicacionesAutomaticas.py
333 lines (301 loc) · 13 KB
/
publicacionesAutomaticas.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from PIL import Image
from woocommerce import API
import math, time, random, requests, os, customtkinter, json
wcapi = API(
url="", #URL de tu tienda
consumer_key="",
consumer_secret="",
version="wc/v3",
)
ig_user_id = "" #el user ID que te da la app de facebook developer
# Si este programa te ha sido de utilidad no olvides
# SUSCRIBIRTE a mi canal de YOUTUBE 👇👇👇
# https://www.youtube.com/@codeffee_ ☕
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
def api_key():
key = self.api_key_entry.get()
with open("key.json", "w") as outfile:json.dump(key, outfile)
def obtenerDatos():
residuo = 0
numDePaginas = 0
productos = []
whilekey = False
while whilekey == False:
try:
totalProductos = (
wcapi.get("reports/products/totals").json()[2]["total"]
+ wcapi.get("reports/products/totals").json()[3]["total"]
)
whilekey = True
except:
self.label4.configure(text="Reconectando con el servidor...")
time.sleep(5)
residuo = totalProductos % 100
numDePaginas = totalProductos / 100
if residuo == 0:
for i in range(1, numDePaginas + 1):
whilekey = False
while whilekey == False:
try:
productos.append(
wcapi.get(
"products",
params={
"per_page": 100,
"stock_status": "instock",
"page": (i),
},
).json()
)
whilekey = True
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
self.label4.configure(
text="Base de datos descargada con éxito! %s"
% (str(current_time))
)
except:
self.label4.configure(
text="Reconectando con el servidor..."
)
time.sleep(5)
else:
numDePaginas = math.ceil(numDePaginas)
for i in range(1, numDePaginas + 1):
whilekey = False
while whilekey == False:
try:
productos.append(
wcapi.get(
"products",
params={
"per_page": 100,
"stock_status": "instock",
"page": (i),
},
).json()
)
whilekey = True
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
self.label4.configure(
text="Base de datos descargada con éxito! %s"
% (str(current_time))
)
except:
self.label4.configure(
text="Reconectando con el servidor..."
)
time.sleep(5)
productosSeleccionados = []
for i in range(len(productos)):
for j in range(len(productos[i])):
productosSeleccionados.append(
[
productos[(i)][j]["name"],
productos[(i)][j]["images"][0]["src"],
productos[(i)][j]["images"][0]["alt"]
]
)
with open("data.json", "w") as outfile:json.dump(productosSeleccionados, outfile)
def publicar():
with open('data.json', 'r') as openfile:
# Reading from json file
data = json.load(openfile)
numero = random.randrange(0, len(data), 1)
with open('key.json', 'r') as openfile:
# Reading from json file
access_token = json.load(openfile)
image_url = data[numero][1]
frase = data[numero][2]
caption = (
frase + "\nHaz tus pedidos por DM 🛒🔥\nMás productos en nuestra tienda online\n.\n.\nContamos con envío a domicilio\n📩Pedidos por DM\n🛒 o en nuestra tienda online (link en BIO)\n#tiendaenlinea #woocommerce @codeffee_"
)
post_url = "https://graph.facebook.com/v15.0/%s/media" % (ig_user_id)
param = dict()
param["access_token"] = access_token
param["caption"] = caption
param["image_url"] = image_url
try:
response = requests.post(post_url, params=param)
response = response.json()
if "id" in response:
creation_id = response["id"]
second_url = "https://graph.facebook.com/v15.0/%s/media_publish" % (
ig_user_id
)
second_param = dict()
second_param = {
"access_token": access_token,
"creation_id": creation_id,
}
response = requests.post(second_url, params=second_param)
response = response.json()
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
self.label4.configure(text="Imagen publicada! %s" % (str(current_time)))
else:
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
self.label4.configure(
text="Error: No fue posible publicar %s" % (str(current_time))
)
except:
self.label4.configure(
text="Error: Error de conexión %s" % (str(current_time))
)
self.title("Publicaciones Automáticas")
self.geometry("700x450")
self.resizable(False, False)
# set grid layout 1x2
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
# load images with light and dark mode image
image_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "test_images"
)
self.logo_image = customtkinter.CTkImage(
Image.open(os.path.join(image_path, "CustomTkinter_logo_single.png")),
size=(26, 26),
)
self.large_test_image = customtkinter.CTkImage(
Image.open(os.path.join(image_path, "large_test_image.png")),
size=(500, 150),
)
self.image_icon_image = customtkinter.CTkImage(
Image.open(os.path.join(image_path, "image_icon_light.png")), size=(20, 20)
)
self.image_icon_image2 = customtkinter.CTkImage(
Image.open(os.path.join(image_path, "image_icon_light-des.png")),
size=(20, 20),
)
self.image_icon_image3 = customtkinter.CTkImage(
Image.open(os.path.join(image_path, "image_icon_light-ins.png")),
size=(20, 20),
)
self.home_image = customtkinter.CTkImage(
light_image=Image.open(os.path.join(image_path, "home_dark.png")),
dark_image=Image.open(os.path.join(image_path, "home_light.png")),
size=(20, 20),
)
self.chat_image = customtkinter.CTkImage(
light_image=Image.open(os.path.join(image_path, "chat_dark.png")),
dark_image=Image.open(os.path.join(image_path, "chat_light.png")),
size=(20, 20),
)
self.add_user_image = customtkinter.CTkImage(
light_image=Image.open(os.path.join(image_path, "add_user_dark.png")),
dark_image=Image.open(os.path.join(image_path, "add_user_light.png")),
size=(20, 20),
)
# create navigation frame
self.navigation_frame = customtkinter.CTkFrame(self, corner_radius=0)
self.navigation_frame.grid(row=0, column=0, sticky="nsew")
self.navigation_frame.grid_rowconfigure(4, weight=1)
self.navigation_frame_label = customtkinter.CTkLabel(
self.navigation_frame,
text=" Publicaciones Automáticas",
image=self.logo_image,
compound="left",
font=customtkinter.CTkFont(size=15, weight="bold"),
)
self.navigation_frame_label.grid(row=0, column=0, padx=20, pady=20)
self.home_button = customtkinter.CTkButton(
self.navigation_frame,
corner_radius=0,
height=40,
border_spacing=10,
text="Mi tienda",
fg_color="transparent",
text_color=("gray10", "gray90"),
hover_color=("gray70", "gray30"),
image=self.home_image,
anchor="w",
command=self.home_button_event,
)
self.home_button.grid(row=1, column=0, sticky="ew")
self.appearance_mode_menu = customtkinter.CTkOptionMenu(
self.navigation_frame,
values=["Light", "Dark", "System"],
command=self.change_appearance_mode_event,
)
self.appearance_mode_menu.grid(row=6, column=0, padx=20, pady=20, sticky="s")
# create home frame
self.home_frame = customtkinter.CTkFrame(
self, corner_radius=0, fg_color="transparent"
)
self.home_frame.grid_columnconfigure(0, weight=1)
self.home_frame_large_image_label = customtkinter.CTkLabel(
self.home_frame, text="", image=self.large_test_image
)
self.home_frame_large_image_label.grid(
row=0, column=0, columnspan=2, padx=20, pady=10
)
self.api_key_entry = customtkinter.CTkEntry(
self.home_frame, placeholder_text="Instagram API KEY", width=300
)
self.api_key_entry.grid(row=1, column=0, padx=20, pady=(25, 35))
self.home_frame_button_2 = customtkinter.CTkButton(
self.home_frame,
text="Renovar",
image=self.image_icon_image,
compound="right",
command=api_key,
)
self.home_frame_button_2.grid(row=1, column=1, padx=20, pady=(25, 35))
self.label1 = customtkinter.CTkLabel(
self.home_frame, text="Actualizar base de datos", fg_color="transparent"
)
self.label1.grid(row=3, column=0, padx=20, pady=2)
self.label2 = customtkinter.CTkLabel(
self.home_frame, text="Publicar producto", fg_color="transparent"
)
self.label2.grid(row=3, column=1, padx=20, pady=2)
self.home_frame_button_1 = customtkinter.CTkButton(
self.home_frame,
text="Descargar productos",
image=self.image_icon_image2,
compound="right",
width=300,
command=obtenerDatos,
)
self.home_frame_button_1.grid(row=4, column=0, padx=20, pady=2)
self.home_frame_button_3 = customtkinter.CTkButton(
self.home_frame,
text="Publicar",
image=self.image_icon_image3,
compound="right",
command=publicar,
)
self.home_frame_button_3.grid(row=4, column=1, padx=20, pady=2)
self.label3 = customtkinter.CTkLabel(
self.home_frame,
text="",
fg_color="transparent",
)
self.label3.grid(row=5, column=0, padx=20, pady=(10, 35), columnspan=2)
self.label4 = customtkinter.CTkLabel(
self.home_frame, text="", corner_radius=22, text_color="cyan"
)
self.label4.grid(row=6, column=0, padx=20, pady=2, columnspan=2)
# select default frame
self.select_frame_by_name("home")
def select_frame_by_name(self, name):
# set button color for selected button
self.home_button.configure(
fg_color=("gray75", "gray25") if name == "home" else "transparent"
)
# show selected frame
if name == "home":
self.home_frame.grid(row=0, column=1, sticky="nsew")
else:
self.home_frame.grid_forget()
def home_button_event(self):
self.select_frame_by_name("home")
def change_appearance_mode_event(self, new_appearance_mode):
customtkinter.set_appearance_mode(new_appearance_mode)
if __name__ == "__main__":
app = App()
app.mainloop()