def color(self): if not self.__color: try: if 'artwork_url' in self.obj and self.obj['artwork_url']: art = self.obj['artwork_url'] else: art = self.obj['user']['avatar_url'] if not art: raise ValueError() fobj = cStringIO.StringIO(self.client.get(art).raw_data) self.__color = colors.colorz(fobj, 1)[0] except: log.error("Could not get artwork colour - defaulting to black.\n%s", traceback.format_exc()) self.__color = (0, 0, 0) return self.__color
def color(self): if not self.__color: try: if 'artwork_url' in self.obj and self.obj['artwork_url']: art = self.obj['artwork_url'] else: art = self.obj['user']['avatar_url'] if not art: raise ValueError() fobj = cStringIO.StringIO(self.client.get(art).raw_data) self.__color = colors.colorz(fobj, 1)[0] except: log.error( "Could not get artwork colour - defaulting to black.\n%s", traceback.format_exc()) self.__color = (0, 0, 0) return self.__color
consumer_secret = config[1], access_token = config[2], access_token_secret = config[3]) base_url='https://api.tumblr.com/v2' colors_file = 'html/colors.json' combos = json.loads(open(colors_file).read()) dash = tumblr.get(base_url + '/user/dashboard', params={'type': 'photo'}) posts = dash.json()['response']['posts'] for latest_post in posts: post_slug = latest_post['slug'] or latest_post['reblog_key'] photo_url = latest_post['photos'][0]['original_size']['url'] photo_ext = path.splitext(photo_url)[1] if photo_ext != ".gif": photo_path = path.join("img", post_slug + photo_ext) print photo_path urlretrieve(photo_url, photo_path) try: hexes = colorz(photo_path, n=4) except TypeError: print "This photo ain't go no colors" except IndexError: print "This photo ain't got enough colors!" except IOError: print "This photo got corrupted." combos[post_slug] = hexes with open(colors_file, 'w') as f: f.write(json.dumps(combos))
from os import path from bs4 import BeautifulSoup from colors import colorz def gen_html(slug, hexes): tmpl = BeautifulSoup(open('kmeans.html')) tmpl.title.string = slug tmpl.select("#one")[0]["style"] = "background-color:" + hexes[0] tmpl.select("#two")[0]["style"] = "background-color:" + hexes[1] tmpl.select("#three")[0]["style"] = "background-color:" + hexes[2] tmpl.select("#four")[0]["style"] = "background-color:" + hexes[3] return tmpl.prettify() if __name__ == "__main__": from sys import argv try: hexes = colorz(argv[1], n=4) slug = path.split(path.splitext(argv[1])[0])[1] html = gen_html(slug, hexes) filename = path.join("html", slug + ".html") with open(filename, 'w') as f: f.write(html) except TypeError: print "This photo ain't go no colors" except IndexError: print "This photo ain't got enough colors!" except IOError: print "This photo got corrupted."