import requests
import json
import time
import uuid
import re
from collections import namedtuple
from concurrent import futures

with open('unicode_escape_net.txt','r') as f:
    res = f.read()

VideoInfo = namedtuple('VideoInfo','contentId contentType id name')
PdfInfo = namedtuple('PdfInfo','contentId contentType id name')

class Analyzer(object):
    Videos = []
    Pdfs = []
    
    def __init__(self,data):
        self.res = data
        
    
    def distribute(self,):
        for item in self._gen_itmeiter():
            
            if item[1] == '1':
                self.Videos.append( VideoInfo(*item) )
            elif item[1] == '3':
                self.Pdfs.append( PdfInfo(*item) )
        return 

    def _gen_itmeiter(self):
        self.chapters = re.findall(r'homeworks=\w+;.+id=(\d+).+name="(.+)";', self.res)
        for index_1,chapter in enumerate(self.chapters, start=1):
            lessons = re.findall(r'chapterId=' + chapter[0] + r'.+contentType=1.+id=(\d+).+name="(.+)".+test', self.res)

            for index_2,lesson in enumerate(lessons, start=1):
                all_videos = re.findall(r'contentId=(\d+).+contentType=(1).+id=(\d+).+lessonId=' +
                    lesson[0] + r'.+name="(.+)"', self.res)

                for index_3,item in enumerate(all_videos, start=1):
                    item = list(item)
                    item[3] = str(index_1)+'.'+str(index_2)+'.'+str(index_3)+' '+item[3]
                    yield item

                # yield from gen_videos
                
                all_pdfs = re.findall(r'contentId=(\d+).+contentType=(3).+id=(\d+).+lessonId=' +
                    lesson[0] + r'.+name="(.+)"', self.res)
                # yield from gen_pdfs
                for index_3,item in enumerate(all_pdfs,start=1):
                    item = list(item)
                    item[3] = str(index_1)+'.'+str(index_2)+'.'+str(index_3)+' '+item[3]
                    yield item
    

if __name__ == '__main__':
    a = Analyzer(data=res)
    a.distribute()
    # print(a.Videos)
    for i in a.Pdfs:
        print(i)
    # print(a.chapters)