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})
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