PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/main.py

https://github.com/purplemass/YouTube
Python | 177 lines | 139 code | 19 blank | 19 comment | 8 complexity | ae66d397e80113f0978bbea4cb8000a8 MD5 | raw file
  1. # ------------------------------------------------------------------------------
  2. # import
  3. # ------------------------------------------------------------------------------
  4. import sys
  5. import time
  6. from datetime import datetime
  7. import yaml
  8. # ------------------------------------------------------------------------------
  9. # get settings
  10. # ------------------------------------------------------------------------------
  11. date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  12. error = False
  13. running = True
  14. settings_file = 'settings.yaml'
  15. try:
  16. f = open(settings_file)
  17. except:
  18. print 'ERROR: Cannot open %s' % settings_file
  19. error = True
  20. try:
  21. settings = yaml.load(f)
  22. except:
  23. print 'ERROR: Cannot load YAML file'
  24. error = True
  25. try:
  26. f.close()
  27. except:
  28. print 'ERROR: Cannot close %s' % settings_file
  29. error = True
  30. # ------------------------------------------------------------------------------
  31. # process settings
  32. # ------------------------------------------------------------------------------
  33. if (not error):
  34. # main vars
  35. paths = settings['paths']
  36. application = settings['application']
  37. credentials = settings['credentials']
  38. tags = settings['tags']
  39. gmail = settings['gmail']
  40. pause_time = application['pause_time']
  41. pause_time_quota = application['pause_time_quota']
  42. test_mode = application['test_mode']
  43. youtube_feed = settings['youtube_feed'] % credentials['username']
  44. else:
  45. sys.exit('Existing program')
  46. # ------------------------------------------------------------------------------
  47. # import our classes
  48. # ------------------------------------------------------------------------------
  49. import commonops
  50. common = commonops.CommonOps(application, gmail)
  51. import fileops
  52. import youtube
  53. # ------------------------------------------------------------------------------
  54. # Main program
  55. # ------------------------------------------------------------------------------
  56. if (__name__ == '__main__'):
  57. youtube = youtube.YouTube(credentials, application, tags)
  58. # if arg is list then list all videos for our
  59. # YouTube account - then exit
  60. #
  61. try:
  62. arg = sys.argv[1]
  63. except:
  64. arg = ''
  65. if arg == 'list':
  66. #youtube.GetDeveloperTagList('imag_dev')
  67. youtube.GetAndPrintVideoFeed(youtube_feed)
  68. sys.exit('Exiting program')
  69. if not test_mode:
  70. youtube.Login()
  71. fileops = fileops.FileOps(paths, application)
  72. while running:
  73. common.PrintLine(True)
  74. # write date/time
  75. date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  76. common.log(1, 'Date/Time:[T][T]%s' % date_time)
  77. file_name = fileops.ProcessFolder()
  78. if (file_name == False):
  79. time.sleep(pause_time)
  80. else:
  81. ret = youtube.UploadVideo(paths['local_incoming'] + file_name)
  82. # check the status
  83. if (ret == 'uploaded'):
  84. ret = fileops.ArchiveFile(file_name, False)
  85. # reject with a very long pause
  86. elif (ret == 'too_many_recent_calls'):
  87. body = gmail['quota_body'] % (file_name, (pause_time_quota/60))
  88. common.Mail('%s' % body)
  89. common.log(2, '... waiting for %s minutes to reset YouTube quota ...' % (pause_time_quota/60))
  90. time.sleep(pause_time_quota)
  91. # reject with a short pause
  92. #elif ( (ret == 'rejected') or (ret == 'failed') or (ret == 'error') ):
  93. else:
  94. ret = fileops.ArchiveFile(file_name, True)
  95. body = gmail['rejected_body'] % (file_name)
  96. common.Mail('%s' % body)
  97. time.sleep(2)
  98. # ------------------------------------------------------------------------------
  99. # reference:
  100. # ------------------------------------------------------------------------------
  101. """
  102. http://code.google.com/apis/youtube/1.0/developers_guide_python.html
  103. http://code.google.com/apis/youtube/2.0/developers_guide_protocol_video_feeds.html
  104. http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.youtube.html
  105. Dev Tag Issue?:
  106. http://markmail.org/thread/ekb6zmuwrnlk2tvd
  107. http://groups.jonzu.com/z_apis_adding-developer-tags.html#comments
  108. http://stackoverflow.com/questions/3677453/youtube-api-php-retrieving-a-private-video-under-my-account
  109. http://gdata.youtube.com/demo/
  110. Quota
  111. http://apiblog.youtube.com/2010/02/best-practices-for-avoiding-quota.html
  112. PyYaml:
  113. http://mikkel.elmholdt.dk/?p=4
  114. # write file
  115. f = open('newtree.yaml', "w")
  116. yaml.dump(settings, f)
  117. f.close()
  118. """
  119. # ------------------------------------------------------------------------------
  120. # code may be used later:
  121. # ------------------------------------------------------------------------------
  122. #youtube_feed = 'http://gdata.youtube.com/feeds/api/users/FocusCamTest/uploads/mgOKxPAOg2g'
  123. #youtube_feed = 'http://gdata.youtube.com/feeds/api/users/default/uploads/Pu43mzXcw9Q' #mgOKxPAOg2g
  124. #youtube_feed = '%s/Pu43mzXcw9Q' % youtube_feed
  125. # show tagged videos for testing:
  126. #youtube.GetDeveloperTagList(tags['developer'][0])
  127. # get all videos
  128. #youtube.GetAndPrintVideoFeed(youtube_feed)
  129. #sys.exit()
  130. # get a single video
  131. #youtube.GetAndPrintSingleVideo('Glny4jSciVI')
  132. # ------------------------------------------------------------------------------