示例#1
0
 def POST(self):
     #ufile=self.request.params['userfile']
     ufile = self.request.FILES['userfile']
     #if ufile:
     #name=ufile.filename
     name = ufile.name
     mtype =os.path.splitext(name)[1][1:]
     #bits = self.param('userfile')
     bits = ''.join(ufile.chunks())
     media=Media(name = name, mtype = mtype, bits = bits)
     media.put()
     self.write(simplejson.dumps({'name':media.name,'size':media.size,'id':str(media.key())}))
示例#2
0
 def GET(self):
     extstr=self.param('ext')
     ext=extstr.split('|')
     files=Media.all()
     if extstr!='*':
         files=files.filter('mtype IN',ext)
     self.render2('views/admin/upload.html',{'ext':extstr,'files':files})
示例#3
0
 def GET(self):
     try:
         page_index=int(self.param('page'))
     except:
         page_index=1
     files = Media.all().order('-date')
     files,links=Pager(query=files,items_per_page=15).fetch(page_index)
     self.render('views/admin/filemanager.html',{'files' : files,'pager':links})
示例#4
0
    def POST(self): # delete files
        #delids = self.request.POST.getall('del')
        delids = self.request.POST.getlist('del')
        if delids:
            for id in delids:
                file = Media.get_by_id(int(id))
                file.delete()

        self.redirect('/admin/filemanager')
示例#5
0
文件: media.py 项目: allenbo/easyblog
def search(request):
  context = Media.get_num_of_state()
  if request.method == 'GET' and 'q' in request.GET:
    q = request.GET.get('q', '')
    if q:
      medias = Media.objects.filter(description__icontains = q)
      context['medias'] = medias
      return render(request, 'admin/media_show.html', context)
  else:
    return render(request, 'admin/error.html')
示例#6
0
 def GET(self, slug):
     media=Media.get(slug)
     if media:
         self.response.headers['Expires'] = 'Thu, 15 Apr 3010 20:00:00 GMT'
         self.response.headers['Cache-Control'] = 'max-age=3600,public'
         self.response.headers['Content-Type'] = str(media.mtype)
         self.response.out.write(media.bits)
         a=self.param('a')
         if a and a.lower()=='download':
             media.download+=1
             media.put()
示例#7
0
文件: media.py 项目: allenbo/easyblog
def show(request):
  context = Media.get_num_of_state()
  if request.method == 'GET' and request.GET:
    form = MediaFilterForm(request.GET)
    if form.is_valid():
      attached = form.cleaned_data['attached']
      if attached == 'true':
        medias = Media.objects.filter(attached = True)
      else:
        medias = Media.objects.filter(attached = False)
    else:
      return render(request, 'admin/error.html')
  else:
    medias = Media.objects.all()
  context['medias'] = medias
  return render(request, 'admin/media_show.html', context)
示例#8
0
def upload(request):
    if request.method == 'POST':
        form = UserForm(request.POST, request.FILES)
        if form.is_valid():
            # username = form.cleaned_data['user_name']
            # head_img = form.cleaned_data['headImg']
            Media(
                username=request.POST.get('username'),
                img=request.FILES.get('img'),
                video=request.FILES.get('video'),
                # django 里面上传文件默认只处理单个文件上传,批量上传的时候request.FILES 的类型
                # 为 MultiValueDict,这种字典类是特殊定义的,要取得list 需要调用 getlist方法:
                time=datetime.now()).save()

            return HttpResponseRedirect('/blog/upload')
    else:
        form = UserForm()
    return render(request, "register.html", {'form': form})
示例#9
0
def tweetsOperator(statusDatas):
    index = 0
    if len(statusDatas) > 0:
        for status in statusDatas:
            status_json_dic = status._json

            # 获取用户及推文信息
            # 创建时间
            created_at = status_json_dic['created_at']
            # python date
            created_at_python_date = transform_twitter_date_to_python_date(
                created_at)
            # 推文ID
            post_id_str = status_json_dic['id_str']
            # 推文文字
            post_text = status_json_dic['text']

            user_info_dic = status_json_dic['user']
            user_id_str = user_info_dic['id_str']
            user_screen_name = user_info_dic['screen_name']

            # 判断不是转推
            if 'retweeted_status' not in status_json_dic:
                # 判断字典中是否有extended_entities字段,有的话说明有多个媒体文件
                if 'extended_entities' in status_json_dic:
                    # 获取媒体字段
                    extended_entities_dic = status_json_dic[
                        'extended_entities']
                    media_list = extended_entities_dic['media']

                    for media in media_list:
                        media_id_str = media['id_str']
                        media_type = media['type']
                        media_url = media['media_url']

                        # 如果媒体已在删除列表中记录,跳过不进行下载
                        deleted_media_record = DeletedMedia.objects.filter(
                            post_id_str=post_id_str, media_id_str=media_id_str)
                        if deleted_media_record:
                            # 如果有记录,此媒体不进行保存,跳到下一个
                            continue

                        # 图片名称
                        photo_file_name = os.path.basename(media_url)
                        # 获取存储全路径
                        dest_file_full_path = get_file_local_full_path(
                            user_screen_name, photo_file_name)

                        media_data = Media(user_id_str=user_id_str,
                                           post_id_str=post_id_str,
                                           post_text=post_text,
                                           media_id_str=media_id_str,
                                           media_type=media_type,
                                           remote_url=media_url,
                                           created_at=created_at_python_date,
                                           local_url=dest_file_full_path,
                                           is_cover=False)

                        save_media_data_into_database(media_data)
                        # 获取文件夹路径
                        dest_dev_path = get_poster_local_store_dev_path(
                            user_screen_name)
                        # 存储图片
                        download_file_from_url(dest_dev_path,
                                               dest_file_full_path, media_url)

                        if media_type == 'video':
                            video_info_list = media['video_info']['variants']
                            bitrate_temp = 0
                            best_video_url = ""
                            video_file_name = ""
                            # 获取最高清视频地址
                            for video_info in video_info_list:
                                if 'bitrate' in video_info:
                                    bitrate = video_info['bitrate']
                                    if bitrate > bitrate_temp:
                                        bitrate_temp = bitrate
                                        best_video_url = video_info['url']

                            video_file_name_str = os.path.basename(
                                best_video_url)
                            if '?' in video_file_name_str:
                                video_file_name = video_file_name_str.split(
                                    "?")[0]
                            else:
                                video_file_name = video_file_name_str

                            # 下载视频
                            if len(video_file_name) > 0:
                                http_video_url = best_video_url.replace(
                                    "https", "http")
                                dest_video_full_path = get_file_local_full_path(
                                    user_screen_name, video_file_name)

                                # 存储视频媒体数据到数据库
                                media_just_saved = load_media_data_from_database(
                                    media_data)
                                if media_just_saved is not None:
                                    media_just_saved.local_video_url = dest_video_full_path
                                    media_just_saved.save()

                                # 下载视频媒体文件
                                download_file_from_url(dest_dev_path,
                                                       dest_video_full_path,
                                                       http_video_url)

            index = index + 1