Coverage for mymeco/cmdline.py: 11%

92 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-05 22:06 +0000

1# coding: utf-8 

2"""Command line support for mymeco.""" 

3import sys 

4import os 

5import argparse 

6from lxml import etree 

7 

8 

9from mymeco.config import Configuration 

10from mymeco import logger 

11from mymeco.files.video import Video 

12from mymeco.tmdb.utils import Tmdb 

13from mymeco.kodi.movie import Movie 

14 

15 

16def _compute(config, filename, searchterms=None, year=None): 

17 video = Video(filename) 

18 movie_title = '.'.join(os.path.basename(filename).split('.')[:-1]) 

19 tmdb = Tmdb(config.tmdb()['token'], 'fr-FR') 

20 if searchterms is None: 

21 searchterms = movie_title 

22 output_list = list(tmdb.search_movie(searchterms, year)) 

23 index = 1 

24 if not output_list: 

25 print('Search result is empty.', file=sys.stderr) 

26 return 

27 

28 for result in output_list: 

29 print( 

30 '{}. {} ({}) - {} - {}'.format( 

31 index, result.title, result.original_title, 

32 result.release_date, result.poster_path 

33 ) 

34 ) 

35 index += 1 

36 

37 movie_id = None 

38 while movie_id is None: 

39 try: 

40 if len(output_list) > 1: 

41 choice = int(input('Choose a movie:')) 

42 else: 

43 choice = 1 

44 movie_id = output_list[choice - 1].id 

45 except IndexError: 

46 print('{} is outside result range. Retry.'.format(choice), 

47 file=sys.stderr) 

48 movie_id = None 

49 except ValueError: 

50 print('Not a number. Retry.', file=sys.stderr) 

51 movie_id = None 

52 

53 movie = Movie(movie_id, tmdb) 

54 movie.set_technical(video) 

55 movie.sorttitle = os.path.basename(filename) 

56 

57 nfo_file = '.'.join(filename.split('.')[:-1] + ['nfo']) 

58 with open(nfo_file, 'w') as nfo: 

59 movie.nfo(nfo) 

60 

61 

62def _update(config, filenames): 

63 for filename in filenames: 

64 nfo_file = '.'.join(filename.split('.')[:-1] + ['nfo']) 

65 if not os.path.isfile(nfo_file): 

66 print('NFO file not found ({})'.format(nfo_file)) 

67 continue 

68 

69 video = Video(filename) 

70 # Retrieve tmdb movie id in nfo file 

71 movie_id = None 

72 for action, element in etree.iterparse(nfo_file, tag='uniqueid'): 

73 if element.attrib['type'] == 'tmdb': 

74 movie_id = element.text 

75 break 

76 if movie_id is None: 

77 print('No TMDB id found in NFO file.') 

78 continue 

79 

80 tmdb = Tmdb(config.tmdb()['token'], 'fr-FR') 

81 movie = Movie(movie_id, tmdb) 

82 movie.sorttitle = os.path.basename(filename) 

83 movie.set_technical(video) 

84 

85 with open(nfo_file, 'w') as nfo: 

86 movie.nfo(nfo) 

87 

88 

89def __parse_args(args): 

90 """Parse command line arguments.""" 

91 parser = argparse.ArgumentParser( 

92 description='Mymecoctl generate nfo file from Video file.' 

93 ) 

94 parser.add_argument( 

95 '--search', '-s', help='Use these terms in TMDB movie search request.', 

96 type=str 

97 

98 ) 

99 parser.add_argument( 

100 '--year', '-y', help='Look for movie released in the given year', 

101 type=int 

102 ) 

103 parser.add_argument( 

104 '--update', '-u', help='Update nfo already existing nfo files.', 

105 action='store_true' 

106 ) 

107 parser.add_argument('FILENAME', nargs='*') 

108 

109 parsed = parser.parse_args(args) 

110 

111 print(parsed.update) 

112 print(parsed.FILENAME) 

113 if not parsed.update and len(parsed.FILENAME) != 1: 

114 print('Multiple video file is available only with --update flag.', 

115 file=sys.stderr) 

116 sys.exit(1) 

117 

118 return parsed 

119 

120 

121def mymecoctl(argv=None): 

122 """Entrypoint to create nfo file next to video file.""" 

123 if argv is None: 

124 argv = sys.argv[1:] 

125 

126 config = Configuration() 

127 logger.configure(**config.log()) 

128 

129 parsed = __parse_args(argv) 

130 

131 if parsed.update: 

132 _update(config, parsed.FILENAME) 

133 sys.exit(0) 

134 

135 try: 

136 if os.path.isfile(parsed.FILENAME[0]): 

137 _compute(config, parsed.FILENAME[0], parsed.search, parsed.year) 

138 else: 

139 print( 

140 '{} does not exists.'.format(parsed.FILENAME[0]), 

141 file=sys.stderr 

142 ) 

143 except KeyboardInterrupt: 

144 print('Abort')