初试python

初试python

自动下载经济之声英语之夜昨日的wma文件的python script.

[Copy to clipboard] [ - ]
CODE:
#!/bin/env python

import sys, os, time;
from datetime import date;


# 1. set dest file.
prefix = 'mms://211.89.225.102/cnr/channel2/yyzy';
postfix = '210000.wma';
today = date.fromtimestamp( time.time() );
yesterday = today.fromordinal( today.toordinal() - 1 );

target_name = prefix + yesterday.strftime('%Y%m%d') + postfix;
print 'target_name = ', target_name;

# 2. download use flashget
path = 'C:\\Program Files\\fget\\';
bin = 'flashget.exe';

dest_store = 'G:\\';

cmd = path + bin;
arg1 = " " + target_name;
arg2 = " " + dest_store;

print 'cmd = ', cmd, 'arg1 = ', arg1, 'arg2 = ', arg2;

os.execlp( cmd, arg1, arg2);

flashget原来还可以通过命令行来用呢。
python有自己的urllib/urlib2,很好用的,可以试试。

楼主的程序一个indent都没有用到,咋一看还看不出是python来,哈哈。
对了,那些行末的;都去掉吧,看着有点怪异,呵呵


QUOTE:
原帖由 mirage.cn 于 2007-5-9 14:49 发表
flashget原来还可以通过命令行来用呢。
python有自己的urllib/urlib2,很好用的,可以试试。

楼主的程序一个indent都没有用到,咋一看还看不出是python来,哈哈。

主程序,怎么indent呢?
另外,C++用惯了,还是习惯末尾+ “;”
吓一跳。原来是怕蛇(PYTHON)
改进版:

[Copy to clipboard] [ - ]
CODE:
#!/bin/env python

import sys, os, time
from datetime import date

class Days:
    ''' enum variables for days '''
    BEGIN_DAY = 1

    MONDAY = BEGIN_DAY
    TUESDAY = 2
    WEDNESDAY = 3
    THURSDAY = 4
    FRIDAY = 5
    SATURDAY = 6
    SUNDAY = 7

    DAYS7 = 8

    TODAY = 9
    YESTERDAY = 10

    END_DAY = YESTERDAY + 1

    def IsWeek(self, day):
        ''' Is week? '''
        return (day in range(Days.MONDAY, Days.SUNDAY + 1))

    def IsToday(self, day):
        ''' Is today? '''
        return day == Days.TODAY

    def IsYesterday(self, day):
        ''' Is yesterday? '''
        return day == Days.YESTERDAY
   
# end def of class Days

# def of func: getFileName
def getFileName(day = Days.YESTERDAY):
    ''' get file name according to day '''
    prefix = 'mms://211.89.225.102/cnr/channel2/yyzy'
    postfix = '210000.wma'

    today = date.fromtimestamp( time.time() )

    # must be int and in range!
    if day not in range(Days.BEGIN_DAY, Days.END_DAY):
        raise Exception, 'parameter day is not valid!'

    def getResult( prefix, day, postfix ):
        return prefix + day.strftime('%Y%m%d') + postfix
   
    if Days().IsWeek(day):
        # week day, do here
        if today.isoweekday() < day:
            raise Exception, 'could fetch day after today!'

        destday = today.fromordinal( today.toordinal() - ( today.isoweekday() - day ) )

        return getResult( prefix, destday, postfix )

    elif Days().IsToday(day):
        # today, do here
        return getResult( prefix, today, postfix )

    elif Days().IsYesterday(day):
        # yesterday, do here
        yesterday = today.fromordinal( today.toordinal() - 1 )

        return getResult( prefix, yesterday, postfix )
   
    else:
        # for Days.7DAYS
        raise Exception, 'not supported day!'

def download(target_name, dest, path = 'C:\\Program Files\\fget\\', bin = 'flashget.exe'):
    cmd = path + bin
    arg1 = ' ' + target_name
    arg2 = ' ' + dest
    print 'cmd = ', cmd, 'arg1 = ', arg1, 'arg2 = ', arg2

    os.execlp( cmd, arg1, arg2 )
   
# def of func: main   
def main():
    ''' main, program entry point '''
    target_name = getFileName()
    dest_store = 'G:\\'
    download( target_name, dest_store )
   
if __name__ == '__main__':
    main()

哪天按照楼主的实例写一个法国台的,呵呵。