MCPcopy
hub / github.com/hellerve/programming-talks / main

Function main

scripts/add_years.py:60–101  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

58
59
60def main():
61 parser = optparse.OptionParser('usage%prog -f <target_file>')
62 parser.add_option('-f', dest='input_file', type='string',
63 help='specify input file')
64
65 (options, args) = parser.parse_args()
66 input_file = options.input_file
67
68 if not has_youtubedl():
69 print('error: you do not have youtube-dl available')
70 return
71
72 log = []
73
74 number_of_lines = get_number_of_lines(input_file)
75 with open(input_file, 'r+') as f:
76 data = f.read()
77 new_data = []
78 for i, line in enumerate(data.split('\n'), 1):
79 print('Parsing line: ' + str(i) + ' of ' + str(number_of_lines))
80 has_been_added_earlier = re.findall('\(\d\d\d\d\)', line)
81 if has_been_added_earlier:
82 new_data.append(line)
83 print(line)
84 else:
85 url_match = re.findall('(http[s]?://\S+)\)', line)
86 if url_match:
87 link = url_match[0]
88 year = get_release_year(link)
89 if year:
90 new_line = line
91 new_line += print_year(year)
92 new_data.append(''.join(new_line))
93 print(''.join(new_line))
94 else:
95 new_data.append(line)
96 log.append('Failed: to get year for ' + link)
97 else:
98 new_data.append(line)
99 f.seek(0) # set file cursor to start of file
100 f.write('\n'.join(new_data))
101 handle_log(log)
102
103
104if __name__ == '__main__':

Callers 1

add_years.pyFile · 0.70

Calls 5

has_youtubedlFunction · 0.85
get_release_yearFunction · 0.85
print_yearFunction · 0.85
get_number_of_linesFunction · 0.70
handle_logFunction · 0.70

Tested by

no test coverage detected