#!/usr/bin/env python
import sys ,os
import ncutils

#check syntax
import optparse
parser = optparse.OptionParser()
opts, args = parser.parse_args()
assert len(args) == 0

#config check
import json
confpath = os.path.expanduser(os.path.join( "~", ".ncutils"))
with open(confpath) as f:
    conf = json.load(f)

#do the work
assert ncutils.login(conf["username"], conf["password"])
queue = ncutils.Mylist(conf["queue_id"])
dstdir = conf["queue_storage"]

os.chdir(dstdir)
targets = queue.items
for target in targets:
    try:
        print "start: smid:%s" % (target.video.smid,)
        target.video.download()
        queue.delete_item(target)
    except Exception, e:
        print "err" + e.__str__()
