-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
161 lines (130 loc) · 5.67 KB
/
app.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
import os
import json
from xmlrpc.client import ResponseError
import requests
from dotenv import load_dotenv
from datetime import datetime, timedelta, time
import pytz
import re
# Load the enviroment variables.
load_dotenv()
# Create the variables with enviroment values
token = os.getenv("TOKEN_NOTION")
database_notion = os.getenv("DATABASE_NOTION")
tokenMiro = os.getenv("TOKEN_MIRO")
boardMiro = os.getenv("BOARD_MIRO")
# Delay 30 minutes the current time
UTC = pytz.utc
date = datetime.now(UTC) - timedelta(minutes=30)
dateStr = str(date)
date2 = dateStr.split('.')[0]
dateFinal = date2.replace(' ', 'T')
# Function to write the notion URL in the correct sticky note.
def PatchSticky(tokenMiro, id, text, url2, type):
content = f"{text} \n <a href = \"{url2}\" > SÁBANA"
if type == 'sticky_notes':
url = f"https://api.miro.com/v2/boards/{boardMiro}/sticky_notes/{id}"
elif type == 'shapes':
url = f"https://api.miro.com/v2/boards/{boardMiro}/shapes/{id}"
payload = {
"data": {
"content": content,
}
}
headers = {
"accept": "application/json",
'Authorization': tokenMiro,
'Content-Type': 'application/json'
}
response = requests.patch(url, headers=headers, json=payload)
print(response.status_code)
print(f'[OK!] {type} rewrited')
# Function to search the sticky note in Miro with match in "sábana"
def QueryMiro(tokenMiro, titleNotion, urlNotion):
url = f"https://api.miro.com/v2/boards/{boardMiro}/items?limit=50&type=sticky_note&type=shape"
payload={}
headers = {
'Authorization': tokenMiro,
'Cookie': 'AWSALBTG=+MhOBmsm+TDVnXg84oNfFdPwusCsJ9Mj+Pn60eKqU9LHccjG/Tb1u4kmqfiEB3UBkHpshqkiXr/NNRbQ7Y0MQipkLf9AUCTR1XBZbuQcz2N5biEfMJBfa1zJRXrdC2B927M+7jBh5P/i8jS3rxmchhyqhxYEXAcgukV5ejjfnvck; AWSALBTGCORS=+MhOBmsm+TDVnXg84oNfFdPwusCsJ9Mj+Pn60eKqU9LHccjG/Tb1u4kmqfiEB3UBkHpshqkiXr/NNRbQ7Y0MQipkLf9AUCTR1XBZbuQcz2N5biEfMJBfa1zJRXrdC2B927M+7jBh5P/i8jS3rxmchhyqhxYEXAcgukV5ejjfnvck'
}
response = requests.request("GET", url, headers=headers, data=payload)
jsonResponseMiro = json.loads(response.text)
for itemMiro in jsonResponseMiro['data']:
title = itemMiro['data']['content']
id = itemMiro['id']
type = itemMiro['type']
if re.search(titleNotion, title, re.IGNORECASE)!= None:
if type == 'sticky_note':
try:
PatchSticky(tokenMiro=tokenMiro, id=id, text=title, url2=urlNotion, type='sticky_notes')
break
except:
print(f'[ERROR]Something went wrong [PATCHSTICKY method {type}]')
elif type == 'shape':
try:
PatchSticky(tokenMiro=tokenMiro, id=id, text=title, url2=urlNotion, type='shapes')
break
except:
print(f'[ERROR]Something went wrong [PATCHSTICKY method {type}]')
nextLink = jsonResponseMiro['links']['next']
QueryNextLink(tokenMiro, jsonResponseMiro, nextLink, titleNotion, urlNotion)
# Function to search the sticky note in Miro with match in "sábana" if the previous sheet had not match.
def QueryNextLink(tokenMiro, jsonResponseMiro, nextLink, titleNotion, urlNotion):
url = nextLink
payload={}
headers = {
'Authorization': tokenMiro,
'Cookie': 'AWSALBTG=CaJQ225fs+q/WSOjddI8TCtHxmo+gloZhND5J7dfAV9zX8Dr66L3Zi+pfoZ2IN7bgyB08CLEMfeGxzqpNye4OL13mL1OUW99U4MRwX8u4oZWiYygaE8bfOBx17wiTmrS+z0aH7QGEorc+CkNYasIYWUim0id0OpKDiPwWWiG1L3o; AWSALBTGCORS=CaJQ225fs+q/WSOjddI8TCtHxmo+gloZhND5J7dfAV9zX8Dr66L3Zi+pfoZ2IN7bgyB08CLEMfeGxzqpNye4OL13mL1OUW99U4MRwX8u4oZWiYygaE8bfOBx17wiTmrS+z0aH7QGEorc+CkNYasIYWUim0id0OpKDiPwWWiG1L3o'
}
response = requests.request("GET", url, headers=headers, data=payload)
jsonResponseMiro = json.loads(response.text)
for itemMiro in jsonResponseMiro['data']:
title = itemMiro['data']['content']
id = itemMiro['id']
type = itemMiro['type']
if re.search(titleNotion, title, re.IGNORECASE) != None:
if type == 'sticky_note':
try:
PatchSticky(tokenMiro=tokenMiro, id=id, text=title, url2=urlNotion, type='sticky_notes')
break
except:
print(f'[ERROR]Something went wrong [PATCHSTICKY method {type}]')
elif type == 'shape':
try:
PatchSticky(tokenMiro=tokenMiro, id=id, text=title, url2=urlNotion, type='shapes')
break
except:
print(f'[ERROR]Something went wrong [PATCHSTICKY method {type}]')
try:
nextLink = jsonResponseMiro['links']['next']
QueryNextLink(tokenMiro, jsonResponseMiro, nextLink, titleNotion, urlNotion)
except:
print('FIN')
# Function to get the sabanas created 30 minutes ago.
def QueryNotion(token, database_notion, tokenMiro, dateFinal, QueryMiro):
url = f"https://api.notion.com/v1/databases/{database_notion}/query"
payload = json.dumps( {
"filter": {
"property": "Creado",
"created_time": {
"after": dateFinal
}
}
})
headers = {
'Notion-Version': '2022-06-28',
'Authorization': token,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
jsonResponse = json.loads(response.text)
for data in jsonResponse['results']:
try:
titleNotion = data['properties']['Pedido']['title'][0]['plain_text']
urlNotion= data['url']
print(
f"Pedido: {titleNotion} URL: {urlNotion} DATE:{data['properties']['Creado']['created_time']}")
QueryMiro(tokenMiro, titleNotion, urlNotion)
except:
print('[ERROR] Something went wrong')
QueryNotion(token, database_notion, tokenMiro, dateFinal, QueryMiro)