OSDN Git Service

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