howlongtobeat.py (1928B)
1 from howlongtobeatpy import HowLongToBeat 2 import sys 3 import json 4 5 BOLD = "\033[1m" 6 RESET = "\033[0m" 7 RED = "\033[31m" 8 GREEN = "\033[32m" 9 YELLOW = "\033[33m" 10 BLUE = "\033[34m" 11 MAGENTA = "\033[35m" 12 CYAN = "\033[36m" 13 14 15 def printHelp(): 16 print("Usage: " + sys.argv[0].split("/")[-1] + " [-a = all] <game_name> (quotes optional)") 17 18 if len(sys.argv) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help" or (sys.argv[1] == "-a" and len(sys.argv) == 2): 19 printHelp() 20 exit(0) 21 22 args = sys.argv[1:] 23 24 all = False 25 outputJson = False 26 while args[0].startswith("-"): 27 if args[0] == "-a": 28 all = True 29 if args[0] == "-j": 30 outputJson = True 31 if len(args) == 1: 32 printHelp() 33 exit(1) 34 args = args[1:] 35 36 37 gameName = ' '.join(args).rstrip() 38 39 if not outputJson: 40 print("Searching for game: " + gameName) 41 42 # Added `0.0`, otherwise sometimes you get 0 results if the string match isn't big enough 43 # e.g. typing "Lufia" wouldn't return anything, because you are missing 80% of the title of each game 44 results = HowLongToBeat(0.0).search(gameName) 45 46 length = len(results) 47 48 if results == None or length == 0: 49 if outputJson: 50 print("[]") 51 else: 52 print("No results") 53 exit(1) 54 55 if not all and length > 5: 56 results = results[:5] 57 length = 5 58 59 if outputJson: 60 output = [] 61 for result in results: 62 game = { 63 "game_name": result.game_name, 64 "main_story": result.main_story, 65 "main_extra": result.main_extra, 66 "completionist": result.completionist, 67 } 68 output.append(game) 69 print(json.dumps(output, indent=2, sort_keys=True, default=str)) 70 else: 71 print("") 72 for i, result in enumerate(results): 73 print(f'{BOLD}{GREEN}{result.game_name}{RESET}') 74 print(f'{BOLD}{BLUE}Main Story:{RESET} {BOLD}{result.main_story}{RESET} hours') 75 print(f'{BOLD}{CYAN}Main + Extra:{RESET} {BOLD}{result.main_extra}{RESET} hours') 76 print(f'{BOLD}{MAGENTA}Completionist:{RESET} {BOLD}{result.completionist}{RESET} hours') 77 if not i == length - 1: 78 print("")