Created
February 10, 2015 02:29
-
-
Save dmitry-mukhin/46af28e47dac68f52c87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import binascii | |
import pyuploadcare | |
from pyuploadcare import conf | |
from pyuploadcare.api_resources import File | |
conf.pub_key = "demopublickey" | |
conf.secret = "demoprivatekey" | |
def upload_file(fn): | |
with open(fn, 'rb') as f: | |
uf = File.upload(f) | |
uf.store() | |
return uf.cdn_url | |
def handle_line(line): | |
tpn, name, content_type, image_h, image_w, attachment_size, profile_img = line.strip().split(',') | |
cdn_url = '' | |
try: | |
data = binascii.unhexlify(profile_img[2:]) | |
except TypeError as e: | |
print 'error processing "{}, {}": {}'.format(tpn, name, e) | |
else: | |
with open(name, 'wb') as out: | |
out.write(data) | |
cdn_url = upload_file(name) | |
os.remove(name) | |
return ','.join([tpn, name, content_type, image_h, image_w, attachment_size, cdn_url]) | |
def handle_file(fn): | |
with open('out.csv', 'wt') as out: | |
with open(fn, 'rt') as f: | |
for line in f: | |
out.write(handle_line(line) + '\n') | |
if __name__ == '__main__': | |
handle_file('base_64_img_test.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment