oyouareatubeo

mkk-upload.py

Apr 23rd, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import re
  4. import os
  5. import urllib2
  6. import simplejson as json
  7.  
  8.  
  9. # Configuration.
  10. S3_KEYS = "U7TwGpIjERYE9cx4:47gEsUpL4y4tR4iE"
  11.  
  12.  
  13. #______________________________________________________________________________
  14.  
  15. def de_camel(string):
  16. string = string.strip('./ avi')
  17. string = string.replace('_', ' ')
  18. #return re.sub('((?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z]))', ' ', string)
  19. string = re.sub("([a-z])([A-Z])","\g<1> \g<2>",string)
  20. string = re.sub("(-)(\w)","\g<1> \g<2>",string)
  21. string = re.sub("(\w)(-)","\g<1> \g<2>",string)
  22. return re.sub("([a-z]|[A-Z])(\d)","\g<1> \g<2>",string)
  23.  
  24. #______________________________________________________________________________
  25.  
  26. def upload(file, identifier, title, subjects):
  27. file_name = file.split('/')[-1]
  28. print 'uploading "%s" to http://archive.org/details/%s' % (file_name, identifier)
  29. upload_data = open(file).read()
  30. request = urllib2.Request('http://s3.us.archive.org/%s/%s' % (identifier, file_name), data=upload_data)
  31. opener = urllib2.build_opener(urllib2.HTTPHandler)
  32. request.add_header('Content-Type', 'video/avi')
  33. request.add_header('x-amz-auto-make-bucket', '1')
  34. request.add_header('authorization', 'LOW %s' % S3_KEYS)
  35.  
  36. """ <METADATA> """
  37. request.add_header('x-archive-meta-mediatype', 'movies')
  38. request.add_header('x-archive-meta-collection', 'michael-kan-kan-archives')
  39. request.add_header('x-archive-queue-derive', '0')
  40. #request.add_header('x-archive-meta-description', description)
  41. request.add_header('x-archive-meta-title', title)
  42. i=0
  43. for subject in subjects:
  44. header = 'x-archive-meta-subject0%s' % i
  45. request.add_header(header, subject)
  46. i+=1
  47. """ </METADATA> """
  48.  
  49. request.get_method = lambda: 'PUT'
  50.  
  51. try:
  52. url = opener.open(request)
  53. status = url.getcode()
  54. if status == 200:
  55. return ('\n\nSUCCESS :: http://archive.org/details/%s"\n\n' % identifier)
  56. except urllib2.HTTPError, e:
  57. return ('\n\nERROR: HTTP status code: %s\n\n' % e.code)
  58.  
  59. #______________________________________________________________________________
  60.  
  61. def main():
  62.  
  63. files = []
  64. for dirname, dirnames, filenames in os.walk('.'):
  65. for file in filenames:
  66. if file.endswith('.avi'):
  67. files.append((dirname,file))
  68.  
  69. for f in files:
  70. file = os.path.join(f[0],f[1])
  71. title = de_camel(f[1])
  72. identifier = "%s-%s" % ('mkk',title.lower().replace(' ','-'))
  73. identifier = identifier.replace('---','-')
  74. subjects = de_camel(f[0]).split('/')
  75. jstor = json.loads(urllib2.urlopen('http://archive.org/metadata/%s' % identifier).read())
  76. if not jstor:
  77. upload(file, identifier, title, subjects)
  78. else:
  79. print 'SKIPPING (already exists) :: "%s"' % identifier
  80.  
  81. if __name__ == '__main__':
  82. main()
Advertisement
Add Comment
Please, Sign In to add comment