OSDN Git Service

implement seriesnum determinant system.
[rec10/rec10-git.git] / rec10 / trunk / src / classify.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5
6 import os
7 import os.path
8 import optparse
9 import sys
10
11 import configreader
12 import recdblist
13 import auto_move
14 import guess
15 recordedpath=unicode(configreader.getpath("recorded"),'utf-8')
16 recpath=unicode(configreader.getpath("recpath"),'utf-8')
17
18 if __name__ == "__main__":
19     usage="usage: %prog read -h"
20     version="%prog 0.9.4"
21     parser=optparse.OptionParser(usage=usage,version=version)
22     parser.add_option("-s","--search",action="store",type="string",dest="ltitle",default="",metavar="TITLE",help="test to search where the title should be contained(test for -e)")
23     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
24     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
25     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -A)")
26     parser.add_option("-t","--tssearch",action="store_true",dest="tssearch",default=False,help="Ts move auto search")
27     parser.add_option("-T","--Tsmove",action="store_true",dest="tsmove",default=False,help="Ts auto move.")
28     parser.add_option("-S","--SeriesNum",action="store",type="string",dest="seriespath",default="",metavar="TITLE",help="Search Series Number in the path.")
29     #parser.add_option("-f","--filenumlist",action="store_true",dest="filenumlist",default=False,help="Guess Program Number.")
30
31     (opts,args)=parser.parse_args(sys.argv)
32     if opts.ltitle!="":##-sの場合
33         ltitle=unicode(opts.ltitle,'utf-8')
34         sf=guess.searchFolder(ltitle, recordedpath)
35         print "###MKV###"
36         recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mkv")+".mkv"))
37         print "\n"
38         print "###MP4###"
39         recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mp4")+".mp4"))
40         print "\n"
41     elif opts.auto:##-A の場合
42         sa=auto_move.search_file(recpath, recordedpath, ".mkv")
43         for t in sa:
44             recdblist.printutf8(u"自動推測実行中-MKV")
45             sf=guess.searchFolder(t,recordedpath)
46             if sf!="":
47                 recdblist.printutf8(u"移動先")
48                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
49                 recdblist.printutf8(u"実行中")
50                 auto_move.execMove(t, recpath, recordedpath,".mkv",1)
51             else:
52                 recdblist.printutf8(t+" can't find matching folder")
53         sa=auto_move.search_file(recpath, recordedpath, ".mp4")
54         for t in sa:
55             recdblist.printutf8(u"自動推測実行中-MP4")
56             sf=guess.searchFolder(t,recordedpath)
57             if sf!="":
58                 recdblist.printutf8(u"移動先")
59                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
60                 recdblist.printutf8(u"実行中")
61                 auto_move.execMove(t, recpath, recordedpath,".mp4",1)
62             else:
63                 recdblist.printutf8(t+" can't find matching folder")
64     elif opts.list:##-lの場合
65         sa=auto_move.search_file(recpath, recordedpath,".mkv")
66         for t in sa:
67             sf=guess.searchFolder(t,recordedpath)
68             if sf!="":
69                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
70         sa=auto_move.search_file(recpath, recordedpath,".mp4")
71         for t in sa:
72             sf=guess.searchFolder(t,recordedpath)
73             if sf!="":
74                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
75     elif opts.etitle!="":
76         if os.path.exists(os.path.join(recpath, etitle+".mkv")):
77             auto_move.execMove(etitle,recpath, recordedpath,".mkv",1)
78         elif os.path.exists(os.path.join(recpath, etitle+".mp4")):
79             auto_move.execMove(etitle,recpath, recordedpath,".mp4",1)
80     elif opts.tssearch:## -tの場合
81         tsmovepath=""
82         try:
83             tsmovepath=unicode(configreader.getpath("ts_movepath"),'utf-8')
84         except:
85             ""
86         if tsmovepath!="":
87             sa=auto_move.search_file(recpath, tsmovepath,".ts")
88             for t in sa:
89                 sf=guess.searchFolder(t,tsmovepath,700)
90                 if sf!="":
91                     recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
92                     print "\n"
93             sa=auto_move.search_file(recpath, tsmovepath,".ts")
94         else:
95             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。")
96     elif opts.tsmove:##-Tの場合
97         tsmovepath=""
98         try:
99             tsmovepath=unicode(configreader.getpath("ts_movepath"),'utf-8')
100         except:
101             ""
102         if tsmovepath!="":
103             sa=auto_move.search_file(recpath, tsmovepath,".ts")
104             for t in sa:
105                 sf=guess.searchFolder(t,tsmovepath,700)
106                 if sf!="":
107                     recdblist.printutf8(u"移動先")
108                     recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
109                     recdblist.printutf8(u"実行中")
110                     auto_move.execMove(t, recpath, tsmovepath,".ts",0)
111             sa=auto_move.search_file(recpath, tsmovepath,".ts")
112         else:
113             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。")
114     elif opts.seriespath!="":##-Sの場合##与えられたパスにシリーズがそろっているかを調べる。
115         ss=guess.detSeriesNum(opts.seriespath)
116         for sstitle, ssv in ss:
117             if len(ssv)>0:
118                 maxt=0
119                 bt=0
120                 alr=[]
121                 for ssi,ssiv in ssv:
122                     alr.append(ssi)
123                     if maxt<ssi:
124                         maxt=ssi
125                 alr=list(set(alr))
126                 alr.sort()
127                 alr.reverse()
128                 nuke=""
129                 for i in xrange(maxt):
130                     if alr.count(i)==0:
131                         nuke=nuke+str(i)+","
132                 if len(nuke)>0:
133                     recdblist.printutf8(sstitle+" "+str(maxt))
134                 else:
135                     recdblist.printutf8(u"**"+sstitle+" |"+str(maxt)+"| "+nuke)
136
137