-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
111 lines (75 loc) · 2.97 KB
/
main.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
import requests
import tweepy
import json
import os
token = os.environ["NOTION_TOKEN_PUT"]
BEARER_TOKEN = os.environ["NOTION_BEARER_TOKEN"]
main_page_id = 'fa2142e7178a4af980ef0bc2bcde14ec'
headers = {
"Accept": "application/json",
"Notion-Version": "2021-08-16",
"Content-Type": "application/json",
"Authorization": "Bearer " + token
}
Usernames = ['NotionAPI','berty','DFintelligence','moul']
def main():
tweet_ids_by_user = []
ColumnIDs = getColumnIDs()
for username in Usernames:
tweet_ids_by_user.append(getTweet(username))
for index,tweet_ids_list in enumerate(tweet_ids_by_user):
for tweet in tweet_ids_list:
appendEmbed(tweet,Usernames[index],ColumnIDs[index])
"""
# Appel de l'ensemble des blocks de la page via l'id de page
def apiCall():
response = requests.request("GET", url_block_page, headers=headers)
return json.loads(response.text)
"""
#Met à jour le lien du tweet dans le block "embed" via son id (block_id)
'''
def updateEmbed():
updateData = {
"embed":{
"url":"https://twitter.com/NotionAPI/status/1430576039650942976?s=20&t=R92L0tBPzRXD42TS-Yyalw"
}
}
return requests.request("PATCH", url, json=updateData, headers=headers)
'''
#Créer un nouveau block "embed" qui contient le lien vers le tweet
def appendEmbed(tweet_ids,username,column_id):
url_block_column = f"https://api.notion.com/v1/blocks/{column_id}/children"
appData = [{
"object": "block",
"type": "embed",
"embed": {
"url": f"https://twitter.com/{username}/status/{tweet_ids}"
}
}
]
ReturnCode = requests.request("PATCH", url_block_column, json={"children": appData}, headers=headers)
return ReturnCode
def getColumnIDs():
column_id_return = []
#On récupére l'id du block "column_list" qui est un enfant de l'id de la page
url_block_page_main = f"https://api.notion.com/v1/blocks/{main_page_id}/children"
block_column_list_id = json.loads(requests.request("GET", url_block_page_main, headers=headers).text)
block_column_list_id = block_column_list_id["results"][0]["id"]
#Puis on récupére l'id des x colonnes de la page
url_block_page_column = f"https://api.notion.com/v1/blocks/{block_column_list_id}/children"
columns_id = json.loads(requests.request("GET", url_block_page_column, headers=headers).text)
for column_id in columns_id["results"]:
column_id_return.append(column_id["id"])
return column_id_return
def getTweet(username):
ids = []
client = tweepy.Client(bearer_token=BEARER_TOKEN)
#On récupére l'id Twitter de l'utilisateur
UserID = (client.get_user(username=username)).data.id
#On affiche ses 5 derniers tweets
tweets = client.get_users_tweets(id=UserID, max_results=5)
for tweet in tweets.data:
ids.append(tweet.id)
return ids
if __name__ == '__main__':
main()