'''Convert discovery items to tracker items.

Use Python 3.
'''
import argparse
import glob
import itertools
import gzip
import json


def main():
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument('directory')
    
    args = arg_parser.parse_args()
    filenames = itertools.chain(
        glob.iglob(args.directory + '/*.gz'),
        glob.iglob(args.directory + '/*/*.gz'),
    )

    for filename in filenames:
        with gzip.GzipFile(filename, mode='rb') as in_file:
            doc = json.loads(in_file.read().decode())
        
        if 'video_type' in doc:
            flv_file_discovery(doc)
        elif doc['type'] == 'discovery':
            user_and_video_discovery(doc)
        else:
            raise Exception('Unknown discovery result')


def flv_file_discovery(doc):
    for url in doc['urls']:
        print('url:{}'.format(url))


def user_and_video_discovery(doc):
    for user in doc['users']:
        print('user:{}'.format(user))
    
    for video in doc['videos']:
        print('flv:{}'.format(video))
    

if __name__ == '__main__':
    main()

@@ -657,17 +657,16 @@
 discover
-y
 ':%0A     
@@ -1044,16 +1044,19 @@
 at(video
+%5B0%5D
 ))%0A    %0A

Fix up to match discovery results.