def serve_mogilefs_file(request, key=None): """ Called when a user requests an image. Either reproxy the path to perlbal, or serve the image outright """ # not the best way to do this, since we create a client each time mimetype = mimetypes.guess_type(key)[0] or "application/x-octet-stream" client = mogilefs.Client(settings.MOGILEFS_DOMAIN, settings.MOGILEFS_TRACKERS) if hasattr(settings, "SERVE_WITH_PERLBAL") and settings.SERVE_WITH_PERLBAL: # we're reproxying with perlbal # check the path cache path = cache.get(key) if not path: path = client.get_paths(key) cache.set(key, path, 60) if path: response = HttpResponse(content_type=mimetype) response['X-REPROXY-URL'] = path[0] else: response = HttpResponseNotFound() else: # we don't have perlbal, let's just serve the image via django file_data = client[key] if file_data: response = HttpResponse(file_data, mimetype=mimetype) else: response = HttpResponseNotFound() return response
def __init__(self, base_url=settings.MEDIA_URL): # the MOGILEFS_MEDIA_URL overrides MEDIA_URL if hasattr(settings, 'MOGILEFS_MEDIA_URL'): self.base_url = settings.MOGILEFS_MEDIA_URL else: self.base_url = base_url for var in ('MOGILEFS_TRACKERS', 'MOGILEFS_DOMAIN',): if not hasattr(settings, var): raise ImproperlyConfigured("You must define %s to use the MogileFS backend." % var) self.trackers = settings.MOGILEFS_TRACKERS self.domain = settings.MOGILEFS_DOMAIN self.client = mogilefs.Client(self.domain, self.trackers)
def getActorBaseInfo(self, actor_id): info_fields = [ 'title', 'nickname', 'english_name', 'sex', 'occupation', 'nationality', 'region', 'birthday', 'faith', 'zodiac', 'birthplace', 'height', 'weight', 'blood_type' ] url = 'http://data.ent.sina.com.cn/star/starone.php?id=%s&dpc=1' % actor_id print url try: content = _urllibRequest(url) except: print 'baseinfo错误, 跳过' return False content = content.decode('gb18030', 'ignore').encode('utf-8') if content.find(u'组合名称') != -1: return False #f = open('/tmp/sina_actor.tmp', 'w+') #f.write(content) #f.close() myMassage = [ (re.compile('<style.*?>([\s\S]*?)</style>'), lambda x: ''), (re.compile('<script.*?>([\s\S]*?)</script>'), lambda x: ''), (re.compile('style="(.*?)"'), lambda x: ''), (re.compile('<tr(.*?)>'), lambda x: '') ] html_content = BeautifulSoup.BeautifulSoup(content, fromEncoding='utf-8', markupMassage=myMassage) table = html_content.find('table', attrs={'class': 'inf'}) tds = table.findAll('td', limit=14) i = 0 base_data = {} for td in tds: field_name = info_fields[i] match = re.compile('<.*?>') field_data = match.sub('', str(td)) if field_data and field_data != u'暂无': base_data[field_name] = field_data i = i + 1 #处理照片 photo_img = html_content.find('img', attrs={'rel': 'v:photo'}) photo_img_url = photo_img['src'] if photo_img_url: try: source_img_data = _urllibRequest(photo_img_url) image_data = StringIO(source_img_data) #原图 img_s = image_data #缩略图 img_thumb = StringIO() im = Image.open(image_data) im.thumbnail((120, 120), Image.NEAREST) im.save(img_thumb, 'jpeg') #print img_thumb.getvalue() #exit() source_key = '%d%s' % (time.time(), random.randint(100, 999)) thumb_key = '%d%s_120' % (time.time(), random.randint( 100, 999)) source_name = source_key + '.jpg' thumb_name = thumb_key + '.jpeg' #文件存储到mogilefs #c = mogilefs.Client(domain='5ifoto', trackers=['192.168.1.51:6001']) c = mogilefs.Client(domain='5itv', trackers=['192.168.1.31:6001']) c.send_file(source_name, img_s, 'image') c.send_file(thumb_name, img_thumb, 'image') #保存mysql文件系统数据 thumb_value = '{"120":"%s"}' % thumb_name self.db.query( "INSERT INTO `attachments`(`file_name`, `source_name`, `file_key`, `category_id`, `thumb`, `created_at`, `updated_at`) \ VALUES (%s, %s, %s, '90', %s, now(), now());", (source_name, source_name, source_key, thumb_value)) base_data['cover'] = source_name except: print '跳过错误图片' return base_data