OSDN Git Service

simplify codes.
authorgn64_jp <gn64_jp@4e526526-5e11-4fc0-8910-f8fd03428081>
Wed, 24 Mar 2010 04:31:14 +0000 (04:31 +0000)
committergn64_jp <gn64_jp@4e526526-5e11-4fc0-8910-f8fd03428081>
Wed, 24 Mar 2010 04:31:14 +0000 (04:31 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/rec10@523 4e526526-5e11-4fc0-8910-f8fd03428081

rec10/trunk/src/auto_move.py
rec10/trunk/src/classify.py
rec10/trunk/src/guess.py

index 0faae2d..e840a0d 100644 (file)
@@ -10,7 +10,6 @@ import re
 import time
 import shutil
 
-import n_gram
 import recdblist
 def getTitle(title):
     rT=re.compile("(.+)_(\d+)\Z")
@@ -35,28 +34,6 @@ def getTitle(title):
     elif tT5:
         ntitle=tT5.group(1)
     return ntitle
-def detNameType(title):
-    """
-    type A ---title#<number>
-    type B ---title#<number>subtitle
-    type C ---title subtitle
-    type D ---title(without number)
-    """
-    recdblist.printutf8(title)
-    #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
-    rA=re.compile("(.+)#(\d*)\s*\Z")
-    tA=rA.match(title)
-    rB=re.compile("(.+)#(\d*)\s*(\D*)")
-    tB=rB.match(title)
-    if tA:
-        recdblist.printutf8("typeA")
-        recdblist.printutf8("title="+tA.group(1))
-        recdblist.printutf8("num="+tA.group(2))
-    elif tB:
-        recdblist.printutf8("typeB")
-        recdblist.printutf8("title="+tB.group(1))
-        recdblist.printutf8("num="+tB.group(2))
-        recdblist.printutf8("subtitle="+tB.group(3))
 def search_file(temppath,recpath,ext):
     """
     録画一時フォルダ内mkvファイルを検索
@@ -118,53 +95,6 @@ def veryfySize(path):
     if os.path.getsize(path)>270*1024*1024:
         ret=1
     return ret
-def searchFolder(title,path,threshold=500):
-    """
-    titleにマッチするフォルダを探し出す。
-    """
-    folderpath=os.listdir(path)
-    lfpath=[]
-    ngram=[]
-    for ft in folderpath:
-        fullpath=os.path.join(path.encode('utf-8'), ft)
-        if os.path.isdir(fullpath):
-            lfpath.append(fullpath)
-            ftt=os.listdir(fullpath)
-            if len(ftt)>0:
-                for ft2 in ftt:
-                    try:
-                        folderpath.append(os.path.join(fullpath, ft2))
-                    except Exception, inst:
-                        ""
-        else:
-            lfpath.append(fullpath)
-    for dirp in lfpath:
-        cmpp=""
-        appp=""
-        ntitle=title
-        if os.path.isdir(dirp):
-            cmpp=os.path.dirname(dirp)
-            appp=dirp
-        else:
-            cmpp=os.path.basename(dirp)
-            appp=os.path.dirname(dirp)
-        ntitle=getTitle(title)
-        #recdblist.printutf8(cmpp)
-        cmpp=getTitle(os.path.splitext(cmpp)[0])
-        #recdblist.printutf8(cmpp+"\n")
-        p=n_gram.trigram(ntitle,cmpp)
-        if p>0:
-            ngram.append((p,appp))
-    ngram=list(set(ngram))
-    ngram.sort()
-    ngram.reverse()
-    if len(ngram)>0:
-        if ngram[0][0]>threshold:
-            return ngram[0][1]
-        else:
-            return ""
-    else:
-        return ""
 def execMove(title,temppath,recpath,ext,autodel):
     srcpath=os.path.join(temppath,title+ext)
     sf=searchFolder(title, recpath)
index d7fe312..192d60e 100644 (file)
 # Rec10 TS Recording Tools
 # Copyright (C) 2009-2010 Yukikaze
 
-import glob
-import n_gram
 import os
 import os.path
-import re
-import time
 import optparse
 import sys
-import shutil
 
 import configreader
 import recdblist
 import auto_move
+import guess
 recordedpath=unicode(configreader.getpath("recorded"),'utf-8')
 recpath=unicode(configreader.getpath("recpath"),'utf-8')
-def search_b25(path):
-    """
-    録画フォルダを検索し、各QUEの実行状況を表示する
-    """
-    b25list = glob.glob(path + "/*.b25")
-    for b25f in b25list:
-        ##b25f is title.ts.b25  avi is title.avi
-        dir = os.path.split(b25f)[0]
-        title = os.path.split(b25f)[1]
-        title = title.replace(".ts.b25", "")
-        avipath = os.path.join(dir, title + ".avi")
-        mp4path = os.path.join(dir, title + ".mp4")
-        tspath = os.path.join(dir, title + ".ts")
-        b25f = b25f.replace(".ts.b25", ".avi")
-        mode = "ts"
-        if os.path.isfile(tspath):##tsファイルが存在している
-            dtime = time.time()-os.path.getmtime(tspath)
-            dtime = int(dtime)
-            if dtime > 120:
-                if os.path.getsize(tspath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
-                    mode = "avi"
-                else:
-                    mode = "tsmiss"
-            else:
-                mode = "ts"
-        if os.path.isfile(mp4path):##tsファイルが存在している
-            dtime = time.time()-os.path.getmtime(mkvpath)
-            dtime = int(dtime)
-            if dtime > 120:
-                if os.path.getsize(avipath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
-                    mode = "fin"
-                else:
-                    mode = "avimiss"
-            else:
-                mode = "avi"
-        recdblist.printutf8(title + ":" + mode)
-def search_avi(temppath,recpath):
-    """
-    録画一時フォルダ内のaviファイルを検索
-    """
-    avilist = glob.glob(temppath + "/*.avi")
-    ret=[]
-    for avif in avilist:
-        ##b25f is title.ts.b25  avi is title.avi
-        dir = os.path.split(avif)[0]
-        title = os.path.split(avif)[1]
-        title = title.replace(".avi", "")
-        avipath = os.path.join(dir, title + ".avi")
-        dtime = time.time()-os.path.getmtime(avipath)
-        dtime = int(dtime)
-        if dtime > 300:
-            if veryfySize(avipath):
-                ret.append(title)
-    return ret
-def search_mkv(temppath,recpath):
-    """
-    録画一時フォルダ内mkvファイルを検索
-    """
-    avilist = glob.glob(temppath + "/*.mkv")
-    ret=[]
-    for avif in avilist:
-        ##b25f is title.ts.b25  avi is title.avi
-        dir = os.path.split(avif)[0]
-        title = os.path.split(avif)[1]
-        title = title.replace(".mkv", "")
-        avipath = os.path.join(dir, title + ".mkv")
-        dtime = time.time()-os.path.getmtime(avipath)
-        dtime = int(dtime)
-        if dtime > 300:
-            if veryfySize(avipath):
-                ret.append(title)
-    return ret
-def search_mp4(temppath,recpath):
-    """
-    録画一時フォルダ内mp4ファイルを検索
-    """
-    avilist = glob.glob(temppath + "/*.mp4")
-    ret=[]
-    for avif in avilist:
-        ##b25f is title.ts.b25  avi is title.avi
-        dir = os.path.split(avif)[0]
-        title = os.path.split(avif)[1]
-        title = title.replace(".mp4", "")
-        avipath = os.path.join(dir, title + ".mp4")
-        dtime = time.time()-os.path.getmtime(avipath)
-        dtime = int(dtime)
-        if dtime > 300:
-            if veryfySize(avipath):
-                ret.append(title)
-    return ret
-def veryfySize(path):
-    #vsize=[297,497,596,1196]#SD 30m 1h 1.5h 2h
-    vsize=[245,275,295,591,830]
-    vsize=vsize+[325,449,560,590,602,690,805,860,1014,1138,1237]
-    vsize=vsize+[261,535,540,616,740]#HD 30m 1h
-    #vsize=vsize+[381,895,447]
-    ret = 0
-    for size in vsize:
-        if os.path.getsize(path)>(size-10)*1024*1024 and os.path.getsize(path)<(size+10)*1024*1024:
-            ret=1
-    if os.path.getsize(path)>270*1024*1024:
-        ret=1
-    return ret
-def searchFolder(title,path):
-    """
-    titleにマッチするフォルダを探し出す。
-    """
-    folderpath=os.listdir(path)
-    lfpath=[]
-    ngram=[]
-    for ft in folderpath:
-        fullpath=os.path.join(path.encode('utf-8'), ft)
-        if os.path.isdir(fullpath):
-            lfpath.append(fullpath)
-            ftt=os.listdir(fullpath)
-            if len(ftt)>0:
-                for ft2 in ftt:
-                    try:
-                        folderpath.append(os.path.join(fullpath, ft2))
-                    except Exception, inst:
-                        #print type(inst)
-                        #print inst
-                        ""
-        else:
-            lfpath.append(fullpath)
-    for dirp in lfpath:
-        cmpp=""
-        appp=""
-        ntitle=title
-        if os.path.isdir(dirp):
-            cmpp=os.path.dirname(dirp)
-            appp=dirp
-        else:
-            cmpp=os.path.basename(dirp)
-            appp=os.path.dirname(dirp)
-        ntitle=getTitle(title)
-        #recdblist.printutf8(cmpp)
-        cmpp=getTitle(os.path.splitext(cmpp)[0])
-        #recdblist.printutf8(cmpp+"\n")
-        p=n_gram.trigram(ntitle,cmpp)
-        if p>0:
-            ngram.append((p,appp))
-    ngram=list(set(ngram))
-    ngram.sort()
-    ngram.reverse()
-    if len(ngram)>0:
-        #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
-        if ngram[0][0]>500:
-            return ngram[0][1]
-        else:
-            return ""
-    else:
-        return ""
-def getTitle(title):
-    rT=re.compile("(.+)_(\d+)\Z")
-    tT=rT.match(title)
-    rT2=re.compile("(.+)_(.+)_(\d*)\Z")#_(aichi)_2010-02-06T01:59:00.mkv
-    tT2=rT2.match(title)
-    rT3=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+:\d+:\d+\Z")#_(aichi)_2010-02-06T01:59:00.mkv
-    tT3=rT3.match(title)
-    rT4=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+-\d+-\d+\Z")#_(aichi)_2010-02-06T01-59-00.mkv
-    tT4=rT4.match(title)
-    rT5=re.compile("(.+)_(.+)_(.+)\Z")#_(aichi)_2010-02-06T01-59-00.mkv
-    tT5=rT5.match(title)
-    ntitle=title
-    if tT :
-        ntitle=tT.group(1)
-    elif tT2:
-        ntitle=tT2.group(1)
-    elif tT3:
-        ntitle=tT3.group(1)
-    elif tT4:
-        ntitle=tT4.group(1)
-    elif tT5:
-        ntitle=tT5.group(1)
-    return ntitle
-def execMove(title,temppath,recpath):
-    srcpath=os.path.join(temppath,title+".mkv")
-    #desttitle=destName(title, temppath, recpath)
-    
-    sf=searchFolder(title, recpath)
-    if sf!="":
-        destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
-        #os.path.join(sf,desttitle+".mkv")
-        #recdblist.printutf8(srcpath)
-        #recdblist.printutf8(destpath)
-        recdblist.printutf8("moving now..")
-        recdblist.printutf8(srcpath+" : "+destpath)
-        print srcpath
-        shutil.move(srcpath, destpath)
-        #shutil.copy(srcpath, destpath)
-        delpath=[os.path.join(temppath,title+".ts")]
-        delpath.append(os.path.join(temppath,title+".avi"))
-        delpath.append(os.path.join(temppath,title+".120.avi"))
-        delpath.append(os.path.join(temppath,title+".timecode.txt"))
-        delpath.append(os.path.join(temppath,title+".aac"))
-        delpath.append(os.path.join(temppath,title+".ts.b25"))
-        delpath.append(os.path.join(temppath,title+".ts.tsmix"))
-        delpath.append(os.path.join(temppath,title+".ts.log"))
-        delpath.append(os.path.join(temppath,title+".sa.avi"))
-        delpath.append(os.path.join(temppath,title+".sa.avi.log"))
-        delpath.append(os.path.join(temppath,title+".log"))
-        for dp in delpath:
-            try:
-                os.remove(dp)
-                ""
-            except:
-                ""
-def execMove_MKV(title,temppath,recpath):
-    srcpath=os.path.join(temppath,title+".mkv")
-    #desttitle=destName(title, temppath, recpath)
 
-    sf=searchFolder(title, recpath)
-    if sf!="":
-        destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
-        #os.path.join(sf,desttitle+".mkv")
-        #recdblist.printutf8(srcpath)
-        #recdblist.printutf8(destpath)
-        recdblist.printutf8("moving now..")
-        recdblist.printutf8(srcpath+" : "+destpath)
-        print srcpath
-        shutil.move(srcpath, destpath)
-        #shutil.copy(srcpath, destpath)
-        delpath=[os.path.join(temppath,title+".ts")]
-        delpath.append(os.path.join(temppath,title+".avi"))
-        delpath.append(os.path.join(temppath,title+".120.avi"))
-        delpath.append(os.path.join(temppath,title+".timecode.txt"))
-        delpath.append(os.path.join(temppath,title+".aac"))
-        delpath.append(os.path.join(temppath,title+".ts.b25"))
-        delpath.append(os.path.join(temppath,title+".ts.tsmix"))
-        delpath.append(os.path.join(temppath,title+".ts.log"))
-        delpath.append(os.path.join(temppath,title+".sa.avi"))
-        delpath.append(os.path.join(temppath,title+".sa.avi.log"))
-        delpath.append(os.path.join(temppath,title+".log"))
-        for dp in delpath:
-            try:
-                os.remove(dp)
-                ""
-            except:
-                ""
-def execMove_MP4(title,temppath,recpath):
-    srcpath=os.path.join(temppath,title+".mp4")
-    #desttitle=destName(title, temppath, recpath)
-
-    sf=searchFolder(title, recpath)
-    if sf!="":
-        destpath=os.path.join(sf,destNameMP4(title, temppath, sf)+".mp4")
-
-        #os.path.join(sf,desttitle+".mkv")
-        #recdblist.printutf8(srcpath)
-        #recdblist.printutf8(destpath)
-        recdblist.printutf8("moving now..")
-        recdblist.printutf8(srcpath+" : "+destpath)
-        print srcpath
-        shutil.move(srcpath, destpath)
-        #shutil.copy(srcpath, destpath)
-        delpath=[os.path.join(temppath,title+".ts")]
-        delpath.append(os.path.join(temppath,title+".avi"))
-        delpath.append(os.path.join(temppath,title+".120.avi"))
-        delpath.append(os.path.join(temppath,title+".timecode.txt"))
-        delpath.append(os.path.join(temppath,title+".aac"))
-        delpath.append(os.path.join(temppath,title+".ts.b25"))
-        delpath.append(os.path.join(temppath,title+".ts.tsmix"))
-        delpath.append(os.path.join(temppath,title+".ts.log"))
-        delpath.append(os.path.join(temppath,title+".sa.avi"))
-        delpath.append(os.path.join(temppath,title+".sa.avi.log"))
-        delpath.append(os.path.join(temppath,title+".log"))
-        for dp in delpath:
-            try:
-                os.remove(dp)
-                ""
-            except:
-                ""
-def destName(title,temppath,recpath):
-    """
-    titleから移動先に作るべきファイル名を作り出す。
-    """
-    dstpath=os.path.join(recpath,title+".avi")
-    srcpath=os.path.join(temppath,title+".avi")
-    if os.path.exists(dstpath):
-        gmtime=time.gmtime(os.path.getmtime(srcpath))
-        times=time.strftime("%y%m%d",gmtime)
-        title=title+"_"+times
-    return title
-def destNameMKV(title,temppath,recpath):
-    """
-    titleから移動先に作るべきファイル名を作り出す。
-    """
-    dstpath=os.path.join(recpath,title+".mkv")
-    srcpath=os.path.join(temppath,title+".mkv")
-    if os.path.exists(dstpath):
-        gmtime=time.gmtime(os.path.getmtime(srcpath))
-        times=time.strftime("%y%m%d",gmtime)
-        title=title+"_"+times
-    return title
-def destNameMP4(title,temppath,recpath):
-    """
-    titleから移動先に作るべきファイル名を作り出す。
-    """
-    dstpath=os.path.join(recpath,title+".mp4")
-    srcpath=os.path.join(temppath,title+".mp4")
-    if os.path.exists(dstpath):
-        gmtime=time.gmtime(os.path.getmtime(srcpath))
-        times=time.strftime("%y%m%d",gmtime)
-        title=title+"_"+times
-    return title
-def detName(path,title):
-    """
-    type A ---title#<number>
-    type B ---title#<number>subtitle
-    type C ---title subtitle
-    type D ---title(without number)
-    """
-    #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
-    #    recdblist.printutf8("typeA")
-    #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
-    #    recdblist.printutf8("typeB")
-
-def detNameType(title):
-    """
-    type A ---title#<number>
-    type B ---title#<number>subtitle
-    type C ---title subtitle
-    type D ---title(without number)
-    """
-    recdblist.printutf8(title)
-    #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
-    rA=re.compile("(.+)#(\d*)\s*\Z")
-    tA=rA.match(title)
-    rB=re.compile("(.+)#(\d*)\s*(\D*)")
-    tB=rB.match(title)
-    if tA:
-        recdblist.printutf8("typeA")
-        recdblist.printutf8("title="+tA.group(1))
-        recdblist.printutf8("num="+tA.group(2))
-    elif tB:
-        recdblist.printutf8("typeB")
-        recdblist.printutf8("title="+tB.group(1))
-        recdblist.printutf8("num="+tB.group(2))
-        recdblist.printutf8("subtitle="+tB.group(3))
 if __name__ == "__main__":
     usage="usage: %prog read -h"
     version="%prog 0.9.4"
@@ -371,7 +28,7 @@ if __name__ == "__main__":
     (opts,args)=parser.parse_args(sys.argv)
     if opts.ltitle!="":
         ltitle=unicode(opts.ltitle,'utf-8')
-        sf=auto_move.searchFolder(ltitle, recordedpath)
+        sf=guess.searchFolder(ltitle, recordedpath)
         print "###MKV###"
         recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mkv")+".mkv"))
         print "###MP4###"
@@ -380,7 +37,7 @@ if __name__ == "__main__":
         sa=auto_move.search_file(recpath, recordedpath, ".mkv")
         for t in sa:
             recdblist.printutf8(u"自動推測実行中-MKV")
-            sf=auto_move.searchFolder(t,recordedpath)
+            sf=guess.searchFolder(t,recordedpath)
             if sf!="":
                 recdblist.printutf8(u"移動先")
                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
@@ -391,7 +48,7 @@ if __name__ == "__main__":
         sa=auto_move.search_file(recpath, recordedpath, ".mp4")
         for t in sa:
             recdblist.printutf8(u"自動推測実行中-MP4")
-            sf=auto_move.searchFolder(t,recordedpath)
+            sf=guess.searchFolder(t,recordedpath)
             if sf!="":
                 recdblist.printutf8(u"移動先")
                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
@@ -402,12 +59,12 @@ if __name__ == "__main__":
     elif opts.list:
         sa=auto_move.search_file(recpath, recordedpath,".mkv")
         for t in sa:
-            sf=auto_move.searchFolder(t,recordedpath)
+            sf=guess.searchFolder(t,recordedpath)
             if sf!="":
                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
         sa=auto_move.search_file(recpath, recordedpath,".mp4")
         for t in sa:
-            sf=auto_move.searchFolder(t,recordedpath)
+            sf=guess.searchFolder(t,recordedpath)
             if sf!="":
                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
     elif opts.etitle!="":
@@ -415,12 +72,6 @@ if __name__ == "__main__":
             auto_move.execMove(etitle,recpath, recordedpath,".mkv",1)
         elif os.path.exists(os.path.join(recpath, etitle+".mp4")):
             auto_move.execMove(etitle,recpath, recordedpath,".mp4",1)
-        srcpath=os.path.join(temppath,title+".mkv")
-        etitle=unicode(opts.etitle,'utf-8')
-        try:
-            execMove_MKV(etitle,recpath, recordedpath)
-        except:
-            execMove_MP4(etitle,recpath, recordedpath)
     elif opts.tssearch:
         tsmovepath=""
         try:
@@ -430,7 +81,7 @@ if __name__ == "__main__":
         if tsmovepath!="":
             sa=auto_move.search_file(recpath, tsmovepath,".ts")
             for t in sa:
-                sf=auto_move.searchFolder(t,tsmovepath,700)
+                sf=guess.searchFolder(t,tsmovepath,700)
                 if sf!="":
                     recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
             sa=auto_move.search_file(recpath, tsmovepath,".ts")
@@ -445,7 +96,7 @@ if __name__ == "__main__":
         if tsmovepath!="":
             sa=auto_move.search_file(recpath, tsmovepath,".ts")
             for t in sa:
-                sf=auto_move.searchFolder(t,tsmovepath,700)
+                sf=guess.searchFolder(t,tsmovepath,700)
                 if sf!="":
                     recdblist.printutf8(u"移動先")
                     recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
index 51198da..83188c1 100644 (file)
@@ -175,7 +175,7 @@ def detNameType(title,path):
         else:
             ret['type']="C"
     return ret
-def searchFolder(title,path):
+def searchFolder(title,path,threshold=300):
     """
     titleからフォルダーを探す
     """
@@ -209,7 +209,7 @@ def searchFolder(title,path):
     ngram.reverse()
     if len(ngram)>0:
         #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
-        if ngram[0][0]>300:
+        if ngram[0][0]>threshold:
             return [ngram[0][1],ngram[0][0]]
         else:
             return ""