def main(argv):
  logging.config.fileConfig(open("logging.ini"))
  path = argv[0]

  files_start = getfiles(path)
  time.sleep(0.1)
  files_end = getfiles(path)

  # Find the files which haven't changed size.
  unchanged = {}
  for filename, start_size in files_start.iteritems():
    try:
      end_size = files_end[filename]
    except __HOLE__:
      continue

    if start_size == end_size:
      unchanged[filename] = end_size

  logging.info("Found %s files to possibly upload (%s changing)",
      len(unchanged), len(files_end)-len(unchanged))

  # Connect to youtube's SFTP server
  sftp = pexpect.spawn("sftp -oIdentityFile=%s %s@%s" % (
      PRIVATE_KEY, USERNAME.lower(), HOST))
  sftp.expect([PROMPT, SURE])
  logging.info('First line for start %r %r', sftp.before, sftp.after)
  if sftp.after[:5] == SURE[:5]:
    logging.info('Never seen %s before, accepting SSH host key.' % HOST)
    sftp.sendline('yes')
    sftp.expect(PROMPT)

  # open a SFTP channel
  sftp_dirs = []
  for dirname, size in sftp_listdir(sftp, '/').items():
    sftp_dirs.append(dirname)

  for filename, size in unchanged.iteritems():
    # Video files will be gigabytes in size
    #if size < 1e9:
    #  print filename, "File too small", size
    #  continue

    basename = filename[:filename.rfind('.')]
    statusfile = "status-%s.xml" % basename
    successfile = basename+'.uploaded'

    # Has the file already been uploaded?
    if os.path.exists(path+'/'+successfile):
      logging.info("%s is already uploaded and processed.", filename)
      continue

    # Create the directory
    if basename not in sftp_dirs:
      sftp_mkdir(sftp, basename)

    files = {}
    for filename, size in sftp_listdir(sftp, basename).items():
      files[filename] = size

    # Check if the we have a completion status report
    if statusfile in files:
      logging.info("%s already uploaded and processed.", filename)
      if statusfile not in unchanged:
         sftp_get(sftp, basename+'/'+statusfile, path+'/'+statusfile, progress)
      continue

    if "delivery.complete" in files:
      logging.info("%s already uploaded and awaiting processing.", filename)
      continue

    if os.path.exists(path+'/'+statusfile):
      logging.info("%s is already uploaded and waiting status response.", filename)
      continue

    size = os.stat(path+'/'+filename).st_size

    # Upload the actual video
    upload = False
    if filename not in files:
      logging.info("%s does not exist on server.", filename)
      upload = True

    elif files[filename] != size:
      logging.info(
          "%s filesize differs remote: %s, local: %s.", filename, files[filename], size)
      upload = True

    success = False

    # Create the metadata
    if basename+".xml" not in files:
      datestr = basename[basename.rfind('.')+1:]
      date = time.strptime(datestr, "%Y%m%d-%H%M%S")

      xmlfilename = basename+".xml"

      info = guess_event(date)
      info['date'] = datestr
      info['humandate'] = time.strftime('%A, %d %B %Y', date)
      info['filename'] = filename
      info['username'] = USERNAME
      info['password'] = PASSWORD
      logging.info("Guessing the event is %s", info['shortname'])

      data = """\
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
 xmlns:media="http://search.yahoo.com/mrss"
 xmlns:yt="http://www.youtube.com/schemas/yt/0.2">
 <channel>
   <yt:notification_email>tansell@google.com</yt:notification_email>
   <yt:account>
     <yt:username>%(username)s</yt:username>
     <yt:password>%(password)s</yt:password>
   </yt:account>
   <yt:owner_name>%(username)s</yt:owner_name>

   <item>
     <yt:action>Insert</yt:action>
     <media:title>%(shortname)s - %(date)s</media:title>
     <media:content url="file://%(filename)s" >
       <media:description type="plain">Talk at %(name)s given on %(humandate)s</media:description>
       <media:keywords>open source, google, sydney, %(tags)s</media:keywords>
       <media:category>Science &amp; Technology</media:category>
       <media:rating scheme="urn:simple">nonadult</media:rating>
     </media:content>
     <yt:language>en</yt:language>
     <yt:date_recorded>2005-08-01</yt:date_recorded>

     <yt:location>
       <yt:country>AU</yt:country>
       <yt:zip_code>NSW 2009</yt:zip_code>
       <yt:location_text>Pyrmont</yt:location_text>
     </yt:location>

     <yt:start_time>2007-07-07T07:07:07</yt:start_time> <!-- A date
in the past -->

     <yt:community>
       <yt:allow_comments>Always</yt:allow_comments>
       <yt:allow_responses>Never</yt:allow_responses>
       <yt:allow_ratings>true</yt:allow_ratings>
       <yt:allow_embedding>true</yt:allow_embedding>
     </yt:community>
   </item>
 </channel>
</rss>
""" % info
      f = open(path+"/"+xmlfilename, "w")
      f.write(data)
      f.close()

      logging.debug("XML Metatdata:\n%s", data)
      sftp_write(sftp, basename+'/'+xmlfilename, data)

      logging.info("%s uploaded metadata", xmlfilename)

    if upload:
      logging.info("%s uploading.", filename)
      sftp_put(sftp, path+'/'+filename, basename+'/'+filename, progress)
      logging.info("%s uploaded.", filename)
      success = True
    else:
      success = True

    if success:
      deliveryname = "delivery.complete"

      if deliveryname not in files:
        logging.info("%s uploading completion file", deliveryname)
        sftp_write(sftp, basename+'/'+deliveryname, '')
        logging.info("%s uploading completion file", deliveryname)

      f = open(path+'/'+successfile, 'w')
      f.write(str(time.time()))
      f.write('\n')
      f.close()

  logging.info('Upload done.')
dataset/ETHPy150Open timvideos/streaming-system/tools/youtube/putvideo.py/main