소스 검색

v1.1.0
- added sort_by argument

cheskel 4 년 전
부모
커밋
477cf5c737
4개의 변경된 파일21개의 추가작업 그리고 11개의 파일을 삭제
  1. 3 1
      list_youtube_channel/__init__.py
  2. 15 7
      list_youtube_channel/get_channel.py
  3. 2 2
      setup.py
  4. 1 1
      tests/test.py

+ 3 - 1
list_youtube_channel/__init__.py

@@ -1 +1,3 @@
-from .get_channel import get_channel
+from .get_channel import get_channel, SORT_BY_NEWEST, SORT_BY_OLDEST, SORT_BY_POPULAR
+
+__version__ = '1.1.0'

+ 15 - 7
list_youtube_channel/get_channel.py

@@ -4,14 +4,18 @@ import json
 import time
 
 
-def get_channel(channel_id: str, limit: int = None, sleep: int = 1) -> Generator:
+SORT_BY_NEWEST = 'newest'
+SORT_BY_OLDEST = 'oldest'
+SORT_BY_POPULAR = 'popular'
+
+def get_channel(channel_id: str, limit: int = None, sleep: int = 1, sort_by: str = SORT_BY_NEWEST) -> Generator:
     session = requests.Session()
     session.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36'
     is_first = True
     count = 0
     while True:
         if is_first:
-            html = get_initial_data(channel_id, session)
+            html = get_initial_data(session, channel_id, sort_by)
             client = json.loads(get_json_from_html(
                 html, 'INNERTUBE_CONTEXT', 2, '"}},') + '"}}')['client']
             api_key = get_json_from_html(html, 'innertubeApiKey', 3)
@@ -37,13 +41,17 @@ def get_channel(channel_id: str, limit: int = None, sleep: int = 1) -> Generator
         time.sleep(sleep)
 
 
-def get_initial_data(channel_id: str, session: requests.Session) -> str:
-    response = session.get(
-        f'https://www.youtube.com/channel/{channel_id}/videos')
+def get_initial_data(session: requests.Session, channel_id: str, sort_by: str) -> str:
+    sort_by_map = {
+        SORT_BY_NEWEST: 'dd',
+        SORT_BY_OLDEST: 'da',
+        SORT_BY_POPULAR: 'p'
+    }
+    url = f'https://www.youtube.com/channel/{channel_id}/videos?view=0&sort={sort_by_map[sort_by]}&flow=grid'
+    response = session.get(url)
     if 'uxe=' in response.request.url:
         session.cookies.set('CONSENT', 'YES+cb', domain='.youtube.com')
-        response = session.get(
-            f'https://www.youtube.com/channel/{channel_id}/videos')
+        response = session.get(url)
 
     html = response.text
     return html

+ 2 - 2
setup.py

@@ -1,7 +1,7 @@
 from setuptools import setup
+from list_youtube_channel import __version__
 
 
-version = '1.0.0'
 
 
 with open('README.md', encoding='utf-8') as f:
@@ -12,7 +12,7 @@ with open('requirements.txt', encoding='utf-8') as f:
 
 setup(
     name = 'list_youtube_channel',
-    version = version,
+    version = __version__,
     packages = ['list_youtube_channel'],
     include_package_data = True,
     url = 'https://github.com/dermasmid/list_youtube_channel',

+ 1 - 1
tests/test.py

@@ -12,7 +12,7 @@ sys.path.insert(0, '/'.join(os.path.dirname(os.path.realpath(__file__)).split(se
 import list_youtube_channel
 
 
-videos = list_youtube_channel.get_channel("UC9-y-6csu5WGm29I7JiwpnA")
+videos = list_youtube_channel.get_channel("UC9-y-6csu5WGm29I7JiwpnA", sort_by=list_youtube_channel.SORT_BY_POPULAR)
 
 for video in videos:
     print(video['videoId'])