Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.