Skip to content

Instantly share code, notes, and snippets.

@dmitry-mukhin
Created February 10, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-mukhin/46af28e47dac68f52c87 to your computer and use it in GitHub Desktop.
Save dmitry-mukhin/46af28e47dac68f52c87 to your computer and use it in GitHub Desktop.
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