Skip to content

Commit bd7580e

Browse files
committed
PoC which works -v1.1
1 parent 5ef8b3a commit bd7580e

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
# notionput
22
Various scripts to edit a Notion based on external sources
3+
4+
5+
## Progress of the script :
6+
7+
- [x] Retrieve tweets of a user with his id
8+
- [x] Saves tweets in columns on the dashboard notion
9+
- [ ] Automatically retrieve the user's name from the notion dashboard
10+
- [ ] Reverse the order of tweets on the notion dashboard (Newest first)
11+
- [ ] Other suggestions
12+
13+
## How to use this script :
14+
15+
Create a developer twitter account and get this "BEARER_TOKEN".
16+
Add this token as environment variable "NOTION_BEARER_TOKEN"
17+
Create an integration in notion and get the token, don't forget to share the document with the integration.
18+
Add this token as environment variable "NOTION_TOKEN_PUT"
19+
20+
Add the id of the notion page in the main_page_id variable
21+
22+
Finally, write in the "Usernames" table the @ of the users whose tweets you want to retrieve
23+
24+
### Exemple after execution :
25+
26+
![Notion page with some tweets](Exemple.png)
27+
28+
29+
30+
31+

main.py

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import requests
2+
import tweepy
23
import json
34
import os
45

56
token = os.environ["NOTION_TOKEN_PUT"]
7+
BEARER_TOKEN = os.environ["NOTION_BEARER_TOKEN"]
68

7-
block_id = 'e42cdf2c-ef06-4059-b88c-2b159415c902'
8-
block_page_id = 'fa2142e7178a4af980ef0bc2bcde14ec'
9-
10-
url_block_page = f"https://api.notion.com/v1/blocks/{block_page_id}/children"
11-
url = f"https://api.notion.com/v1/blocks/{block_id}"
9+
main_page_id = 'fa2142e7178a4af980ef0bc2bcde14ec'
1210

1311
headers = {
1412
"Accept": "application/json",
@@ -17,23 +15,33 @@
1715
"Authorization": "Bearer " + token
1816
}
1917

18+
Usernames = ['NotionAPI','berty','DFintelligence','moul']
19+
2020

2121
def main():
2222

23-
#print(updateEmbed())
24-
25-
print(appendEmbed())
23+
tweet_ids_by_user = []
24+
25+
ColumnIDs = getColumnIDs()
26+
27+
for username in Usernames:
28+
tweet_ids_by_user.append(getTweet(username))
2629

30+
for index,tweet_ids_list in enumerate(tweet_ids_by_user):
31+
for tweet in tweet_ids_list:
32+
appendEmbed(tweet,Usernames[index],ColumnIDs[index])
33+
34+
"""
2735
# Appel de l'ensemble des blocks de la page via l'id de page
2836
2937
def apiCall():
3038
3139
response = requests.request("GET", url_block_page, headers=headers)
3240
3341
return json.loads(response.text)
34-
42+
"""
3543
#Met à jour le lien du tweet dans le block "embed" via son id (block_id)
36-
44+
'''
3745
def updateEmbed():
3846
3947
updateData = {
@@ -43,22 +51,61 @@ def updateEmbed():
4351
}
4452
4553
return requests.request("PATCH", url, json=updateData, headers=headers)
46-
54+
'''
4755
#Créer un nouveau block "embed" qui contient le lien vers le tweet
4856

49-
def appendEmbed():
57+
def appendEmbed(tweet_ids,username,column_id):
58+
59+
url_block_column = f"https://api.notion.com/v1/blocks/{column_id}/children"
5060

5161
appData = [{
5262
"object": "block",
5363
"type": "embed",
5464
"embed": {
55-
"url": "https://twitter.com/ZeratoR/status/1493276555052826624?s=20&t=UFnSjZGiFRcZQ9lHUv0JtA"
65+
"url": f"https://twitter.com/{username}/status/{tweet_ids}"
5666
}
5767
}
5868
]
59-
60-
return requests.request("PATCH", url_block_page, json={"children": appData}, headers=headers)
6169

70+
ReturnCode = requests.request("PATCH", url_block_column, json={"children": appData}, headers=headers)
71+
72+
return ReturnCode
73+
74+
75+
def getColumnIDs():
76+
77+
column_id_return = []
78+
#On récupére l'id du block "column_list" qui est un enfant de l'id de la page
79+
url_block_page_main = f"https://api.notion.com/v1/blocks/{main_page_id}/children"
80+
81+
block_column_list_id = json.loads(requests.request("GET", url_block_page_main, headers=headers).text)
82+
block_column_list_id = block_column_list_id["results"][0]["id"]
83+
#Puis on récupére l'id des x colonnes de la page
84+
url_block_page_column = f"https://api.notion.com/v1/blocks/{block_column_list_id}/children"
85+
86+
columns_id = json.loads(requests.request("GET", url_block_page_column, headers=headers).text)
87+
88+
for column_id in columns_id["results"]:
89+
column_id_return.append(column_id["id"])
90+
91+
return column_id_return
92+
93+
94+
def getTweet(username):
95+
96+
ids = []
97+
98+
client = tweepy.Client(bearer_token=BEARER_TOKEN)
99+
100+
#On récupére l'id Twitter de l'utilisateur
101+
UserID = (client.get_user(username=username)).data.id
102+
103+
#On affiche ses 5 derniers tweets
104+
tweets = client.get_users_tweets(id=UserID, max_results=5)
105+
for tweet in tweets.data:
106+
ids.append(tweet.id)
107+
108+
return ids
62109

63110
if __name__ == '__main__':
64111
main()

0 commit comments

Comments
 (0)