Skip to content

Commit abf5f52

Browse files
committed
-v1.2
1 parent daef77f commit abf5f52

File tree

4 files changed

+76
-24
lines changed

4 files changed

+76
-24
lines changed

CHANGELOG.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22

33
## v1.2
44

5-
** New Features **
5+
### New Features
66
- Recovering twitter usernames from column titles
77
- Protection against errors caused by a Twitter username that does not exist and against column titles of the wrong type
88

9-
** Changed **
9+
### Changed
1010
- Update of the 'README.md' file
1111

12-
** Feature Change **
12+
### Feature Change **
1313
- Taking into account the number of columns in the page Notion in a dynamic way
1414

15-
** Added: **
15+
### Added: **
1616
- Added "checkTwitterUsername" function that checks if a Twitter user exists
1717
- Added "getUsernames" function that retrieves the username in the column title of the Notion page
1818
- Added function "aggColumnUsername" which calls sub-functions that check that the username exists on twitter and that the title of the column is of type "heading_1
1919
- File ’CHANGELOG.md’
2020

2121
## v1.1
2222

23-
** New Features **
23+
### New Features
2424
- Display of four twitter accounts on one page Notion
2525

26-
** Changed **
26+
### Changed
2727
- Changed of the "main" function to call the different functions and add the tweets in each column
2828
- Changed of the "appendEmbed" function so that it adds a block containing a tweet generated via the variables in parameters.
2929

30-
** Added: **
30+
### Added:
3131
- Added a "getTweet" function that retrieves the id of a user's tweets
3232
- Added "getColumnIDs" function which retrieves the columns id of the Notion page
3333
- File ’README.md’
@@ -37,7 +37,7 @@
3737

3838
Initial release.
3939

40-
** Added: **
40+
### Added:
4141
- Added the "appendEmbed" function which adds a block of type "Embed" which contains the url of a tweet
4242
- Added the "apiCall" function which retrieves the content of the Notion page
4343
- File ’__init__.py’

Exemple.png

267 KB
Loading

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Various scripts to edit a Notion based on external sources
66

77
- [x] Retrieve tweets of a user with his id
88
- [x] Saves tweets in columns on the dashboard notion
9-
- [ ] Automatically retrieve the user's name from the notion dashboard
9+
- [x] Automatically retrieve the user's name from the notion dashboard
10+
- [x] Taking into account the number of columns in the page Notion in a dynamic way
1011
- [ ] Reverse the order of tweets on the notion dashboard (Newest first)
1112
- [ ] Other suggestions
1213

main.py

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from turtle import color
12
import requests
23
import tweepy
34
import json
@@ -15,32 +16,35 @@
1516
"Authorization": "Bearer " + token
1617
}
1718

18-
Usernames = ['NotionAPI','berty','DFintelligence','moul']
19-
20-
2119
def main():
2220

2321
tweet_ids_by_user = []
2422

25-
ColumnIDs = getColumnIDs()
26-
23+
agg_Col = aggColumnUsername()
24+
ColumnIDs = agg_Col[0]
25+
Usernames = agg_Col[1]
26+
2727
for username in Usernames:
2828
tweet_ids_by_user.append(getTweet(username))
2929

3030
for index,tweet_ids_list in enumerate(tweet_ids_by_user):
3131
for tweet in tweet_ids_list:
32-
appendEmbed(tweet,Usernames[index],ColumnIDs[index])
33-
34-
"""
35-
# Appel de l'ensemble des blocks de la page via l'id de page
32+
Code = appendEmbed(tweet,Usernames[index],ColumnIDs[index])
33+
print(Code)
3634

35+
#Calling all the blocks of the page via the page id
36+
"""
3737
def apiCall():
3838
39+
colomun_list_id = "314ce5cb-6882-49e0-8034-6d04ea360edb"
40+
41+
url_block_page = f"https://api.notion.com/v1/blocks/{colomun_list_id}/children"
42+
3943
response = requests.request("GET", url_block_page, headers=headers)
4044
4145
return json.loads(response.text)
4246
"""
43-
#Met à jour le lien du tweet dans le block "embed" via son id (block_id)
47+
#Updates the link of the tweet in the block "embed" via its id (block_id)
4448
'''
4549
def updateEmbed():
4650
@@ -52,7 +56,7 @@ def updateEmbed():
5256
5357
return requests.request("PATCH", url, json=updateData, headers=headers)
5458
'''
55-
#Créer un nouveau block "embed" qui contient le lien vers le tweet
59+
#Create a new "embed" block that contains the link to the tweet
5660

5761
def appendEmbed(tweet_ids,username,column_id):
5862

@@ -71,16 +75,35 @@ def appendEmbed(tweet_ids,username,column_id):
7175

7276
return ReturnCode
7377

78+
#Function that checks if the column title is heading 1 and if the twitter username exists
79+
#if this is not the case the id of the column is not kept.
80+
81+
def aggColumnUsername():
82+
83+
agg_return = [[],[]]
84+
85+
ColumnIDs = getColumnIDs()
86+
Usernames = getUsernames(ColumnIDs)
87+
88+
for index,username in enumerate(Usernames):
89+
90+
if username != "%wrong_type%" and checkTwitterUsername(username) == "user_exists":
91+
agg_return[0].append(ColumnIDs[index])
92+
agg_return[1].append(Usernames[index])
93+
94+
return agg_return
95+
96+
# Function which retrieves the columns id of the Notion page
7497

7598
def getColumnIDs():
7699

77100
column_id_return = []
78-
#On récupére l'id du block "column_list" qui est un enfant de l'id de la page
101+
#We get the id of the block "column_list" which is a child of the id of the page
79102
url_block_page_main = f"https://api.notion.com/v1/blocks/{main_page_id}/children"
80103

81104
block_column_list_id = json.loads(requests.request("GET", url_block_page_main, headers=headers).text)
82105
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
106+
#Then we get the id of the x columns of the page
84107
url_block_page_column = f"https://api.notion.com/v1/blocks/{block_column_list_id}/children"
85108

86109
columns_id = json.loads(requests.request("GET", url_block_page_column, headers=headers).text)
@@ -90,17 +113,45 @@ def getColumnIDs():
90113

91114
return column_id_return
92115

116+
# Function that retrieves the username in the column title (heading_1) of the Notion page
117+
def getUsernames(columns_id):
118+
119+
username_return = []
120+
121+
for column_id in columns_id:
122+
url_block_column = f"https://api.notion.com/v1/blocks/{column_id}/children"
123+
usernames = json.loads(requests.request("GET", url_block_column, headers=headers).text)
124+
#If the type field of the first element of the column is a "heading_1" then we retrieve its content and add it to the table in return
125+
if usernames["results"][0]["type"] == "heading_1":
126+
username_return.append(usernames["results"][0]["heading_1"]["text"][0]["text"]["content"])
127+
#Otherwise we add the string "%wrong_type%" in the return array.
128+
else:
129+
username_return.append("%wrong_type%")
130+
return username_return
131+
132+
# Function that checks if a Twitter user exists
133+
134+
def checkTwitterUsername(username):
93135

136+
client = tweepy.Client(bearer_token=BEARER_TOKEN)
137+
if not (client.get_user(username=username)).errors:
138+
return 'user_exists'
139+
else:
140+
print(client.get_user(username=username).errors[0]['detail'])
141+
return 'User does not exist'
142+
143+
#function that retrieves the id of a user's tweets
94144
def getTweet(username):
95145

96146
ids = []
97147

98148
client = tweepy.Client(bearer_token=BEARER_TOKEN)
99149

100-
#On récupére l'id Twitter de l'utilisateur
150+
#We get the user's Twitter id
151+
101152
UserID = (client.get_user(username=username)).data.id
102153

103-
#On affiche ses 5 derniers tweets
154+
#We display his last 5 tweets
104155
tweets = client.get_users_tweets(id=UserID, max_results=5)
105156
for tweet in tweets.data:
106157
ids.append(tweet.id)

0 commit comments

Comments
 (0)