-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion_modifier.py
167 lines (127 loc) · 4.65 KB
/
notion_modifier.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
import time
from youtube_helper import get_title, get_channel_name
def notion_modifier(inputfile, outputfile, modify_type= 'add_bullet'):
"""
Modify text in the 'inputfile' with various modify type and write the output to the 'outputfile'
Parameters:
inputfile (string): the name of the input file which will be processed ('.txt' format)
outputfile (string): the name of the output file which will be used to write the output ('.txt' format)
modify_type (string): the type of the modification that will be done to the text, (option: 'add_newline', 'add_bullet', 'add_number', 'normalize_notion_link', 'add_youtube_title_and_channel_name')
"""
# Read the file
with open(inputfile, 'r') as f :
# Get the input text
text = f.read()
# Type: Add New Line
if (modify_type == 'add_newline'):
text = add_newline(text)
# Type: Add Bullet
if (modify_type == 'add_bullet'):
text = add_bullet(text)
# Type: Add Number
if (modify_type == 'add_number'):
text = add_number(text)
# Type: Normalize Notion Link
if (modify_type == 'normalize_notion_link'):
text = normalize_notion_link(text)
# Type: Add Youtube Title & Channel Name
if (modify_type == 'add_youtube_title_and_channel_name'):
text = add_youtube_title_and_channel_name(text)
# Write text to the output file
with open(outputfile, 'w') as f :
f.write(text)
def add_newline(text):
"""
Add new line for every new line in the 'text', so that the output will have double new line for each new line from the input
Parameters:
text (string): the text which will be processed
Returns:
text_output (string): the output text that already processed
"""
# Add new line for each new line
text_output = text.replace("\n", "\n\n")
# Return the output
return text_output
def add_bullet(text):
"""
Add '- ' (hyphen and spaces) in start of each line of the 'text'
Parameters:
text (string): the text which will be processed
Returns:
text_output (string): the output text that already processed
"""
# Add bullet in the first line
text_output = f"- {text}"
# Add bullet for all lines after the first line
text_output = text_output.replace("\n", "\n- ")
# Return the output
return text_output
def add_number(text):
"""
Add number in start of each line from of the text
The number will be from 1 to the number of the lines
The format is like this -> ("1. ")
Parameters:
text (string): the text which will be processed
Returns:
text_output (string): the output text that already processed
"""
# Split the text for each line
text_splitted = text.split('\n')
# Initialize the output text
text_output = ""
# Add number in the start of each line
for i, data in enumerate(text_splitted):
text_output = f"{text_output}{i+1}. {data}\n"
# Return the output
return text_output
def normalize_notion_link(text):
"""
This function used to normalize the format of text with url link in notion
Example Input:
- [https://www.youtube.com/watch?v=H8kocPOT5v0](https://www.youtube.com/watch?v=H8kocPOT5v0) Polynomial Regression in Python
- [https://www.geeksforgeeks.org/life-cycle-phases-of-project-management/](https://www.geeksforgeeks.org/life-cycle-phases-of-project-management/)
Example Output:
- https://www.youtube.com/watch?v=H8kocPOT5v0
- https://www.geeksforgeeks.org/life-cycle-phases-of-project-management
"""
# Split the text for each line
url_list = text.split("\n")
# Initialize the output text
text_output = ""
for data in url_list:
# Check whether it's a link format or just a text
if '[' in data:
# Get the url only
url = data.split("[")[1].split("]")[0]
# Append to text_output
text_output = f"{text_output}- {url}\n"
else:
text_output = f"{text_output}{data}\n"
# Return the output
return text_output
def add_youtube_title_and_channel_name(text):
# Split the text for each line
url_list = text.split("\n")
# Initialize the output text
text_output = ""
# Initialize time counter
begin = time.time()
for data in url_list:
# Check whether it's a link format or just a text
if 'http' in data:
# Get the pure url
url = f"http{data.split('http')[1]}"
# Get the title & channel name
title = get_title(url)
channel_name = get_channel_name(url)
# Append to text_output
text_output = f"{text_output}- {url} {title} - {channel_name}\n"
else:
text_output = f"{text_output}{data}\n"
# Get processing time
time.sleep(1) # store end time
end = time.time() # total time taken
print(f"Processing complete {end - begin} seconds")
# Return the output
return text_output