Compare commits
	
		
			6 Commits
		
	
	
		
			98d307db73
			...
			c5830aeadc
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| c5830aeadc | |||
| 3b33fd65a7 | |||
| ffb9e9d5f4 | |||
| 4ce2163e19 | |||
| 65fe4b707c | |||
| 1110f3b024 | 
							
								
								
									
										1
									
								
								.idea/inspectionProfiles/profiles_settings.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								.idea/inspectionProfiles/profiles_settings.xml
									
									
									
										generated
									
									
									
								
							| @@ -1,5 +1,6 @@ | ||||
| <component name="InspectionProjectProfileManager"> | ||||
|   <settings> | ||||
|     <option name="PROJECT_PROFILE" value="Default" /> | ||||
|     <option name="USE_PROJECT_PROFILE" value="false" /> | ||||
|     <version value="1.0" /> | ||||
|   </settings> | ||||
|   | ||||
| @@ -7,9 +7,11 @@ import requests | ||||
| import inflect | ||||
| from rich.logging import RichHandler | ||||
| from rich.traceback import install | ||||
| from rich import print | ||||
| import typing as t | ||||
| from rich.console import Console | ||||
| from type_def.mvw_json_request import MVWJSONRequest | ||||
| from type_def.mvw_json_response import MVWJSONResponse | ||||
| 
 | ||||
| # Without width | ||||
| console = Console(width=180) | ||||
| p = inflect.engine() | ||||
| @@ -73,7 +75,8 @@ log_connectionpool.setLevel(logging.WARNING) | ||||
| install(show_locals=True) | ||||
| 
 | ||||
| 
 | ||||
| class ConfigParser(configparser.ConfigParser): | ||||
| class ConfigParser( | ||||
|         configparser.ConfigParser): | ||||
|     """Can get options() without defaults | ||||
| 
 | ||||
|     Taken from https://stackoverflow.com/a/12600066. | ||||
| @@ -95,11 +98,13 @@ config = ConfigParser(defaults=internal_defaults) | ||||
| config.read(CONST.CFG_DEFAULT_FILENAME) | ||||
| 
 | ||||
| 
 | ||||
| def print_section_header(header: str) -> str: | ||||
| def print_section_header( | ||||
|         header: str) -> str: | ||||
|     return f"Loading config section '[{header}]' ..." | ||||
| 
 | ||||
| 
 | ||||
| def validate_default_section(config_obj: configparser.ConfigParser()) -> None: | ||||
| def validate_default_section( | ||||
|         config_obj: configparser.ConfigParser()) -> None: | ||||
|     log.debug(f"Loading config from file '{CONST.CFG_DEFAULT_ABS_PATH}' ...") | ||||
|     if not config_obj.sections(): | ||||
|         log.error(f"No config sections found in '{CONST.CFG_DEFAULT_ABS_PATH}'. Exiting 1 ...") | ||||
| @@ -118,7 +123,8 @@ def validate_default_section(config_obj: configparser.ConfigParser()) -> None: | ||||
|         log.debug(f"No defaults defined") | ||||
| 
 | ||||
| 
 | ||||
| def config_has_valid_section(config_obj: configparser.ConfigParser()) -> bool: | ||||
| def config_has_valid_section( | ||||
|         config_obj: configparser.ConfigParser()) -> bool: | ||||
|     has_valid_section = False | ||||
|     for config_obj_section in config_obj.sections(): | ||||
|         if set(CONST.CFG_MANDATORY).issubset(config_obj.options(config_obj_section)): | ||||
| @@ -127,15 +133,18 @@ def config_has_valid_section(config_obj: configparser.ConfigParser()) -> bool: | ||||
|     return has_valid_section | ||||
| 
 | ||||
| 
 | ||||
| def is_default(config_key: str) -> bool: | ||||
| def is_default( | ||||
|         config_key: str) -> bool: | ||||
|     return any(config_key in ini_default for ini_default in ini_defaults) | ||||
| 
 | ||||
| 
 | ||||
| def is_same_as_default(config_kv_pair: dict) -> bool: | ||||
| def is_same_as_default( | ||||
|         config_kv_pair: dict) -> bool: | ||||
|     return config_kv_pair in ini_defaults | ||||
| 
 | ||||
| 
 | ||||
| def validate_config_sections(config_obj: configparser.ConfigParser()) -> None: | ||||
| def validate_config_sections( | ||||
|         config_obj: configparser.ConfigParser()) -> None: | ||||
|     for this_section in config_obj.sections(): | ||||
|         log.debug(print_section_header(this_section)) | ||||
|         if not set(CONST.CFG_MANDATORY).issubset(config_obj.options(this_section, no_defaults=True)): | ||||
| @@ -152,22 +161,29 @@ def validate_config_sections(config_obj: configparser.ConfigParser()) -> None: | ||||
|                 log.debug(f"{kv_prefix} {key} = {config_obj[this_section][key]}") | ||||
| 
 | ||||
| 
 | ||||
| def query_string_from_file(filename: str) -> str: | ||||
| def query_string_from_file( | ||||
|         filename: str) -> str: | ||||
|     with open(filename, "r") as jsonfile: | ||||
|         query_string = jsonfile.read() | ||||
|         return query_string | ||||
| 
 | ||||
| 
 | ||||
| def get_query_payload(section_name: str, config_obj: configparser.ConfigParser()) -> JSONType: | ||||
| def get_query_payload( | ||||
|         section_name: str, | ||||
|         config_obj: configparser.ConfigParser()) -> MVWJSONRequest: | ||||
|     log.debug(f"Generating HTTP POST JSON payload ...") | ||||
|     query = config_obj.get(section_name, "query") | ||||
|     if query[0] == "@": | ||||
|         query = query.split("@", 1)[1] | ||||
|         query = query_string_from_file(query) | ||||
|     return json.loads(query) | ||||
|     got_query_payload = MVWJSONRequest(**json.loads(query)) | ||||
|     return got_query_payload | ||||
| 
 | ||||
| 
 | ||||
| def get_json_response(section_name: str, config_obj: configparser.ConfigParser(), payload: JSONType) -> JSONType: | ||||
| def get_json_response( | ||||
|         section_name: str, | ||||
|         config_obj: configparser.ConfigParser(), | ||||
|         payload: MVWJSONRequest) -> MVWJSONResponse: | ||||
|     log.debug(f"Downloading JSON list of Mediathek files that match search criteria") | ||||
|     serialized_payload = json.dumps(payload) | ||||
|     url = config_obj.get(section_name, "mvw_endpoint") | ||||
| @@ -181,19 +197,24 @@ def get_json_response(section_name: str, config_obj: configparser.ConfigParser() | ||||
|               f"""{newline.join(f"Header '{header}': '{value}'" for header, value in list(req.headers.items()))}\n""" | ||||
|               f"Payload: {payload}") | ||||
|     with s.send(prepped) as s: | ||||
|         return json.loads(s.content) | ||||
|         # return json.loads(s.content) | ||||
|         got_json_response = MVWJSONResponse(**json.loads(s.content)) | ||||
|         return got_json_response | ||||
| 
 | ||||
| 
 | ||||
| def filter_json_by_duration(section_name: str, config_obj: configparser.ConfigParser(), json_obj: JSONType) -> JSONType: | ||||
| def filter_json_by_duration( | ||||
|         section_name: str, | ||||
|         config_obj: configparser.ConfigParser(), | ||||
|         json_obj: MVWJSONResponse) -> MVWJSONResponse: | ||||
|     min_length = config_obj.getint(section_name, "min_duration") | ||||
|     if min_length >= 0: | ||||
|         log.debug(f"""Filtering JSON for minimum length of {min_length} {p.plural("second", min_length)}""") | ||||
|         # console.print(json_obj["result"]["results"][0]) | ||||
|         for result in json_obj["result"]["results"]: | ||||
|             console.log(result) | ||||
|             console.log(f"0000000000000000000000") | ||||
|             if not result["duration"] >= min_length: | ||||
|                 pass | ||||
|         console.log(json_obj) | ||||
|         #for result in json_obj["result"]["results"]: | ||||
|         #    console.log(result) | ||||
|         #    console.log(f"0000000000000000000000") | ||||
|         #    if not result["duration"] >= min_length: | ||||
|         #        pass | ||||
|                 # json_str. | ||||
|                 # console.log(f"{result}\n......................") | ||||
|         # console.log(json_obj) | ||||
| @@ -217,8 +238,10 @@ if __name__ == '__main__': | ||||
|         log.debug(f"Processing section '[{section}]' ...") | ||||
|         query_payload = get_query_payload(section, config) | ||||
|         json_response = get_json_response(section, config, query_payload) | ||||
|         log.debug(json_response) | ||||
|         quit() | ||||
|         log.debug(CONST.CFG_KNOWN_SECTION[0]) | ||||
|         if config.has_option(section, "min_duration") or config.has_option(section, "max_duration"): | ||||
|             json_matches_duration = filter_json_by_duration(section, config, json_response) | ||||
|         # console.log(json_response) | ||||
|     # console.log(json_response) | ||||
| 
 | ||||
| @@ -1,3 +1,4 @@ | ||||
| rich | ||||
| requests | ||||
| inflect | ||||
| inflect | ||||
| pydantic | ||||
| @@ -14,11 +14,15 @@ idna==3.3 | ||||
|     # via requests | ||||
| inflect==5.4.0 | ||||
|     # via -r requirements.in | ||||
| pydantic==1.9.0 | ||||
|     # via -r requirements.in | ||||
| pygments==2.11.2 | ||||
|     # via rich | ||||
| requests==2.27.1 | ||||
|     # via -r requirements.in | ||||
| rich==12.0.0 | ||||
|     # via -r requirements.in | ||||
| typing-extensions==4.1.1 | ||||
|     # via pydantic | ||||
| urllib3==1.26.8 | ||||
|     # via requests | ||||
|     # via requests | ||||
							
								
								
									
										0
									
								
								type_def/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								type_def/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										17
									
								
								type_def/mvw_json_request.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								type_def/mvw_json_request.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| from __future__ import annotations | ||||
| from typing import List | ||||
| from pydantic import BaseModel | ||||
|  | ||||
|  | ||||
| class Query(BaseModel): | ||||
|     fields: List[str] | ||||
|     query: str | ||||
|  | ||||
|  | ||||
| class MVWJSONRequest(BaseModel): | ||||
|     queries: List[Query] | ||||
|     sortBy: str | ||||
|     sortOrder: str | ||||
|     future: bool | ||||
|     offset: int | ||||
|     size: int | ||||
							
								
								
									
										37
									
								
								type_def/mvw_json_response.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								type_def/mvw_json_response.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| from __future__ import annotations | ||||
| from typing import List, Optional | ||||
| from pydantic import BaseModel | ||||
|  | ||||
|  | ||||
| class Show(BaseModel): | ||||
|     channel: str | ||||
|     topic: str | ||||
|     title: str | ||||
|     description: str | ||||
|     timestamp: int | ||||
|     duration: int | ||||
|     size: int | ||||
|     url_website: str | ||||
|     url_subtitle: str | ||||
|     url_video: str | ||||
|     url_video_low: str | ||||
|     url_video_hd: str | ||||
|     filmlisteTimestamp: str | ||||
|     id: str | ||||
|  | ||||
|  | ||||
| class QueryInfo(BaseModel): | ||||
|     filmlisteTimestamp: str | ||||
|     searchEngineTime: str | ||||
|     resultCount: int | ||||
|     totalResults: int | ||||
|  | ||||
|  | ||||
| class Result(BaseModel): | ||||
|     results: List[Show] = [] | ||||
|     queryInfo: QueryInfo | ||||
|  | ||||
|  | ||||
| class MVWJSONResponse(BaseModel): | ||||
|     result: Result | ||||
|     err: Optional[str] = None | ||||
		Reference in New Issue
	
	Block a user