88
99
1010class VideoSearch (Command ):
11- """Search video info from a list of IDs or URLs (or CSV filename with URLs/IDs inside), generate CSV output (simplified video dict schema or option to get full video info)
11+ """
12+ Search video info from a list of IDs or URLs (or CSV filename with URLs/IDs inside),
13+ generate CSV output (simplified video dict schema or option to get full video info)
1214 """
1315 name = "video-search"
1416 arguments = [
1517 {"name" : "--ids" , "type" : str , "help" : "Video IDs" , "nargs" : "*" },
1618 {"name" : "--urls" , "type" : str , "help" : "Video URLs" , "nargs" : "*" },
1719 {"name" : "--input-file-path" , "type" : str , "help" : "Input CSV file path with URLs/IDs" },
1820 {"name" : "--output-file-path" , "type" : str , "help" : "Output CSV file path" },
19- {"name" : "--full-info" , "type" : bool , "help" : "Option to get full video info" , "default" : False }
21+ {"name" : "--full-info" , "type" : bool , "help" : "Option to get full video info" , "default" : False },
22+ {"name" : "--url-column-name" , "type" : str , "help" : "URL column name on csv input files" },
23+ {"name" : "--id-column-name" , "type" : str , "help" : "Channel ID column name on csv output files" }
2024 ]
2125
2226 ID_COLUMN_NAME : str = "video_id"
@@ -28,23 +32,6 @@ class VideoSearch(Command):
2832 "id" , "title" , "description" , "published_at" , "view_count" , "like_count" , "comment_count"
2933 ]
3034
31- @staticmethod
32- def filter_fields (video_info : Dict , info_columns : Optional [List ] = None ) -> Dict :
33- """Filters the fields of a dictionary containing video information based on specified columns.
34-
35- Args:
36- video_info (Dict): A dictionary containing video information.
37- info_columns (Optional[List], optional): A list specifying which fields to include in the filtered output.
38- If None, returns the entire video_info dictionary. Defaults to None.
39-
40- Returns:
41- A dictionary containing only the fields specified in info_columns (if provided)
42- or the entire video_info dictionary if info_columns is None.
43- """
44- return {
45- field : value for field , value in video_info .items () if field in info_columns
46- } if info_columns else video_info
47-
4835 @classmethod
4936 def execute (cls : Self , ** kwargs ) -> str :
5037 """
@@ -68,21 +55,20 @@ def execute(cls: Self, **kwargs) -> str:
6855 """
6956 ids = kwargs .get ("ids" , [])
7057 urls = kwargs .get ("urls" , [])
71- input_file_path = kwargs .get ("input_file_path" )
7258 output_file_path = kwargs .get ("output_file_path" )
7359 api_key = kwargs .get ("api_key" )
7460 full_info = kwargs .get ("full_info" , False )
7561
62+ url_column_name = kwargs .get ("url_column_name" , cls .URL_COLUMN_NAME )
63+ id_column_name = kwargs .get ("id_column_name" , cls .ID_COLUMN_NAME )
64+
7665 info_columns = VideoSearch .FULL_INFO_COLUMNS if full_info else VideoSearch .INFO_COLUMNS
7766
78- if input_file_path :
79- with open (input_file_path , mode = 'r' ) as infile :
80- reader = csv .DictReader (infile )
81- for row in reader :
82- if cls .ID_COLUMN_NAME in row and row [cls .ID_COLUMN_NAME ]:
83- ids .append (row [cls .ID_COLUMN_NAME ])
84- elif cls .URL_COLUMN_NAME in row and row [cls .URL_COLUMN_NAME ]:
85- urls .append (row [cls .URL_COLUMN_NAME ])
67+ if (input_file_path := kwargs .get ("input_file_path" )):
68+ if (urls_from_csv := cls .data_from_csv (input_file_path , url_column_name )):
69+ ids += [cls .video_id_from_url (url ) for url in urls_from_csv ]
70+ if (ids_from_csv := cls .data_from_csv (input_file_path , id_column_name )):
71+ ids += ids_from_csv
8672
8773 if not ids and not urls :
8874 raise Exception ("Either 'ids' or 'urls' must be provided for the video-search command" )
@@ -95,7 +81,7 @@ def execute(cls: Self, **kwargs) -> str:
9581 # Remove duplicated
9682 ids = list (set (ids ))
9783 videos_infos = list (youtube .videos_infos ([_id for _id in ids if _id ]))
98-
84+
9985 return cls .data_to_csv (
10086 data = [
10187 VideoSearch .filter_fields (
0 commit comments