In [1]:
import os
import urllib2
import flickrapi
from scintillate import api
from cStringIO import StringIO
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
In [2]:
flickr = api.Flickr()
In [3]:
x = flickr.flickr
arguments = {'auth_token': x.token_cache.token,
'api_key': x.api_key,
'title':'title',
'description':'description',
'tags':'tag tag2 "long tag"',
'is_public':'0',}
kwargs = flickrapi.make_utf8(arguments)
body = flickrapi.Multipart()
for arg, value in kwargs.iteritems():
part = flickrapi.Part({'name': arg}, value)
body.attach(part)
filename = '/Users/ajmendez/Desktop/transparent.gif'
filepart = flickrapi.FilePart({'name': 'photo'}, filename, 'image/jpeg')
body.attach(filepart)
print url
# print str(body)
# print body.parts
# print body.header()
# request = urllib2.Request(url)
# request.add_data(str(body))
# (header, value) = body.header()
# request.add_header(header, value)
# def callback(percentage, done):
# print '{} {}'.format(percentage, done)
# def _callback(percentage, done, seen_header=[False]):
# if seen_header[0]:
# return callback(percentage, done)
# if done:
# seen_header[0] = True
# response = flickrapi.reportinghttp.urlopen(request, __upload_callback)
# response.read()
In [64]:
# kwargs
In [65]:
class Progress(object):
def __init__(self):
self._seen = 0.0
def update(self, total, size, name):
self._seen += size
pct = (self._seen / total) * 100.0
if (int(pct)%10) == 0:
print '%s progress: %.2f' % (name, pct)
class file_with_callback(file):
def __init__(self, path, mode, callback, *args):
file.__init__(self, path, mode)
self.seek(0, os.SEEK_END)
self._total = self.tell()
self.seek(0)
self._callback = callback
self._args = args
def __len__(self):
return self._total
def read(self, size):
data = file.read(self, size)
self._callback(self._total, len(data), *self._args)
return data
# path = 'large_file.txt'
# progress = Progress()
# stream = file_with_callback(path, 'rb', progress.update, path)
In [70]:
register_openers()
# f = '/Users/ajmendez/previous_video.mp4'
f = '/Users/ajmendez/todays_video.mp4'
# fh = open(f, "rb")
progress = Progress()
fh = file_with_callback(f, 'rb',progress.update, os.path.basename(f))
body2 = {"photo": fh}
body2.update(kwargs)
datagen, headers = multipart_encode(body2)
request = urllib2.Request(url, datagen, headers)
In [60]:
def callback(percentage, done):
print '{} {}'.format(percentage, done)
def _callback(percentage, done, seen_header=[False]):
if seen_header[0]:
return callback(percentage, done)
if done:
seen_header[0] = True
# response = flickrapi.reportinghttp.urlopen(request, _callback)
# response.read()
In [ ]:
print urllib2.urlopen(request).read()
In [ ]:
# test_client.py
import urllib2
# Register the streaming http handlers with urllib2
register_openers()
# Start the multipart/form-data encoding of the file "DSC0001.jpg"
# "image1" is the name of the parameter, which is normally set
# via the "name" parameter of the HTML <input> tag.
# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = multipart_encode({"image1": open("DSC0001.jpg", "rb")})
# Create the Request object
request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers)
# Actually do the request, and get the response
print urllib2.urlopen(request).read()