Skip to content

Commit a204054

Browse files
authored
Add page title (#20)
* Add notion page title * refactor for lint * refactor for lint, test * refactor for lint, test
1 parent 33a2b83 commit a204054

File tree

5 files changed

+99
-10
lines changed

5 files changed

+99
-10
lines changed

.style.yapf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ based_on_style = yapf
33
spaces_before_comment = 4
44
indent_width: 4
55
split_before_logical_operator = true
6-
column_limit = 80
6+
column_limit = 79

tests/youtube2notion/test_youtube2notion.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ def setUp(self):
1515
output_dir = './tmp/%s/' % video_id
1616
notion_token_v2 = ''
1717
notion_page_url = ''
18+
info_title = ''
19+
info_author_name = ''
20+
info_author_url = ''
1821

1922
self.y2n = Youtube2notion(
2023
video_id=video_id,
2124
output_dir=output_dir,
2225
notion_token_v2=notion_token_v2,
23-
notion_page_url=notion_page_url)
26+
notion_page_url=notion_page_url,
27+
info_title=info_title,
28+
info_author_name=info_author_name,
29+
info_author_url=info_author_url)
2430

2531
def test_execute(self):
2632
self.y2n.execute()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
3+
from youtube2notion.youtube_info import YoutubeInfo
4+
5+
6+
class TestYoutubeInfo(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.video_id = '5oNcoj4G0xc'
10+
11+
def test_get_information_element_1(self):
12+
self.assertIsNotNone(
13+
YoutubeInfo.get_information_element(self.video_id))
14+
15+
def test_get_information_element_2(self):
16+
self.assertIsNotNone(
17+
YoutubeInfo.get_information_element('Kc_cvAXCs4Y'))

youtube2notion/youtube2notion.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from youtube2notion.ffmpeg import Ffmpeg
33
from youtube2notion.youtube_video import YoutubeVideo
44
from youtube2notion.youtube_subtitle import SubtitleElement, YoutubeSubtitle
5+
from youtube2notion.youtube_info import InformationElement, YoutubeInfo
56
from pathlib import Path
67
from notion.client import NotionClient
78
from notion.block import PageBlock
@@ -10,12 +11,17 @@
1011

1112
class Youtube2notion:
1213

13-
def __init__(self,
14-
video_id: str,
15-
output_dir: str = '',
16-
notion_token_v2: str = '',
17-
notion_page_url: str = '',
18-
subtitle_language: str = 'ko'):
14+
def __init__(
15+
self,
16+
video_id: str,
17+
output_dir: str = '',
18+
notion_token_v2: str = '',
19+
notion_page_url: str = '',
20+
subtitle_language: str = 'ko',
21+
info_title: str = '',
22+
info_author_name: str = '',
23+
info_author_url: str = '',
24+
):
1925
self.video_id = video_id
2026
self.output_dir = output_dir
2127
self.images_output_dir = self.output_dir + 'images/'
@@ -25,13 +31,23 @@ def __init__(self,
2531

2632
self.subtitle_language = subtitle_language
2733

34+
self.info_title = info_title
35+
self.info_author_name = info_author_name
36+
self.info_author_url = info_author_url
37+
2838
def _download_video(self) -> str:
2939
return YoutubeVideo.download(self.video_id, self.output_dir)
3040

3141
def _get_subtitle_elements(self) -> list[SubtitleElement]:
3242
return YoutubeSubtitle.get_subtitle_elements(self.video_id,
3343
[self.subtitle_language])
3444

45+
def _get_info_element(self) -> InformationElement:
46+
info_element = YoutubeInfo.get_information_element(self.video_id)
47+
self.info_title = info_element.title
48+
self.info_author_name = info_element.author_name
49+
self.info_author_url = info_element.author_url
50+
3551
def _take_screenshots(self, input_filename: str):
3652
Path(self.images_output_dir).mkdir(parents=True, exist_ok=True)
3753

@@ -62,17 +78,18 @@ def _upload_to_notion(self, md_file: str, notion_token_v2: str,
6278
with open(md_file, 'r', encoding='utf-8') as f:
6379
new_page = page.children.add_new(
6480
PageBlock,
65-
title=self.video_id + '(' + self.subtitle_language + ')')
81+
title=self.info_title + '(' + self.subtitle_language + ')')
6682
upload(f, new_page)
6783

6884
def execute(self):
6985
subtitle_elements = self._get_subtitle_elements()
86+
self._get_info_element()
7087

7188
downloaded_video_filename = self._download_video()
7289
self._take_screenshots(downloaded_video_filename)
7390

7491
md = self._generage_markdown(
75-
title=self.video_id + '(' + self.subtitle_language + ')',
92+
title=self.info_title + '(' + self.subtitle_language + ')',
7693
subtitle_elements=subtitle_elements,
7794
images_dir='./images/')
7895

youtube2notion/youtube_info.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import requests
2+
3+
4+
class InformationElement:
5+
6+
def __init__(
7+
self,
8+
title: str = '',
9+
author_name: str = '',
10+
author_url: str = '',
11+
):
12+
self._title = title
13+
self._author_name = author_name
14+
self._author_url = author_url
15+
16+
@property
17+
def title(self):
18+
return self._title
19+
20+
@property
21+
def author_name(self):
22+
return self._author_name
23+
24+
@property
25+
def author_url(self):
26+
return self._author_url
27+
28+
29+
class YoutubeInfo:
30+
31+
@classmethod
32+
def get_information_element(cls, video_id: str) -> InformationElement:
33+
34+
params = {
35+
"format": "json",
36+
"url": "https://www.youtube.com/watch?v=%s" % video_id
37+
}
38+
res = requests.get("https://www.youtube.com/oembed", params=params)
39+
40+
if res.status_code == 200:
41+
res_json = res.json()
42+
43+
return InformationElement(
44+
title=res_json.get('title'),
45+
author_name=res_json.get('author_name'),
46+
author_url=res_json.get('author_url'),
47+
)
48+
else:
49+
return InformationElement(title=video_id)

0 commit comments

Comments
 (0)