def api_v2(): method = request.args.get("method", None) success = True result = None ''' {'file': {u'md': {u'video:category': u'comedy', u'video:duration': 6287L, u'video:description': u'asdf'}, u'c': -1, u'fs': datetime.datetime(2011, 4, 7, 7, 19, 49), 'name': u'http://www.veoh.com/veohplayer.swf?permalinkId=v19638583pbHaJywJ', u'src': {u'6d25345e0114c0424a06af6c': {u'url': u'http://www.veoh.com/watch/v19638583pbHaJywJ', u'm': 1, u't': 17, u'fn': {u'1361703869': {u'm': 1, u'l': 1}}, u'l': 1}, u'fe6d3bedce035eebe2a1e051': {u'url': u'http://www.veoh.com/veohplayer.swf?permalinkId=v19638583pbHaJywJ', u'm': 1, u't': 17, u'fn': {u'1361703869': {u'm': 1, u'l': 1}}, u'l': 1}}, u'bl': 0, u'tt': 1, u'm': 1, u's': 3, u'ls': datetime.datetime(2011, 4, 7, 7, 19, 49), u'_id': ObjectId('fe6d3bedce035eebe2a1e051'), 'id': '-m077c4DXuvioeBR', u'fn': {u'1361703869': {u'x': u'', 'c': 2, 'tht': 0, u'n': u'asdf'}}, u'ct': 2}, 'view': {'md': {}, 'file_type': 'video', 'source': u'veoh.com', 'url': '-m077c4DXuvioeBR', 'nfn': u'asdf', 'fnx': '', 'sources': {u'veoh.com': {'count': 2, 'join': False, 'tip': u'veoh.com', 'parts': [], 'urls': [u'http://www.veoh.com/watch/v19638583pbHaJywJ', u'http://www.veoh.com/veohplayer.swf?permalinkId=v19638583pbHaJywJ', u'magnet:?dn=asdf&'], 'icon': 'web'}}, 'efn': u'asdf', 'action': 'Download', 'fn': u'asdf', 'fnh': u'<strong>asdf</strong>'}} ''' if method == "search": result = [] files = search_files(request.args["q"], request.args) result = [{ "size": f["file"]["z"] if "z" in f["file"] else 0, "type": f["view"]["file_type"], "link": url_for("files.download", file_id=f["view"]["url"], _external=True), "metadata": {k: (_api_v2_md_parser[k](v) if k in _api_v2_md_parser else v) for k, v in f["view"]["md"].iteritems()}, } for f in (fill_data(file_data) for file_data in filesdb.get_files(get_ids(files), True))] success = True return jsonify( method = method, success = success, result = result)
def search(): ''' Realiza una búsqueda de archivo ''' # TODO: seguridad en param #si no se ha buscado nada se manda al inicio query = request.args.get("q", None) if not query: flash("write_something") return redirect(url_for("index.home")) #para evitar errores cuando en page no viene un número page = request.args.get("page", "1") if page.isdigit(): page = int(page) else: abort(404) g.title = "%s - %s" % (query, g.title) results = {"total_found":0,"total":0,"time":0} didyoumean = None tags = None if 0 < page < 101: #obtener los tags y el quiso decir tags, dym = taming_search(query, request.args.get("type", None)) #obtener los resultados y sacar la paginación profiler.checkpoint(opening=["sphinx"]) results = search_files(query,request.args,page) or results ids = get_ids(results) profiler.checkpoint(opening=["mongo"], closing=["sphinx"]) files_dict = {mid2hex(file_data["_id"]):fill_data(file_data, False, query) for file_data in get_files(ids)} profiler.checkpoint(opening=["visited"], closing=["mongo"]) save_visited(files_dict.values()) profiler.checkpoint(closing=["visited"]) files=({"file":files_dict[bin2hex(file_id[0])], "search":file_id} for file_id in ids if bin2hex(file_id[0]) in files_dict) # recupera los resultados del taming try: tags = tags.next() didyoumean = dym.next() except: pass else: files = () return render_template('files/search.html', results=results, search=request.args["q"].split(" "), files=files, pagination=Pagination(page, 10, min(results["total_found"], 1000)), didyoumean=didyoumean, tags=tags)
def search(): ''' Realiza una búsqueda de archivo ''' # TODO: seguridad en param #si no se ha buscado nada se manda al inicio query = request.args.get("q", None) if not query: flash("write_something") return redirect(url_for("index.home")) page = int(request.args.get("page", 1)) g.title = query+" - "+g.title results = {"total_found":0,"total":0,"time":0} didyoumean = None tags = None if 0 < page < 101: #obtener los tags y el quiso decir taming = taming_search(current_app.config, query, request.args.get("type", None), contextg=g._get_current_object()) #obtener los resultados y sacar la paginación profiler.checkpoint(opening=["sphinx"]) results = search_files(query,request.args,page) or results ids = get_ids(results) profiler.checkpoint(opening=["mongo"], closing=["sphinx"]) files_dict = {mid2hex(file_data["_id"]):fill_data(file_data, False, query) for file_data in get_files(ids)} profiler.checkpoint(opening=["visited"], closing=["mongo"]) save_visited(files_dict.values()) profiler.checkpoint(closing=["visited"]) files=(files_dict[bin2hex(file_id[0])] for file_id in ids if bin2hex(file_id[0]) in files_dict) # recupera los resultados del taming try: tags = taming.next() didyoumean = taming.next() except: pass else: files = () return render_template('files/search.html', results=results, search=request.args["q"].split(" "), files=files, pagination=Pagination(page, 10, min(results["total_found"], 1000)), didyoumean=didyoumean, tags=tags)
def download(file_id,file_name=None): ''' Muestra el archivo a descargar, votos, comentarios y archivos relacionados ''' def choose_filename_related(file_data): ''' Devuelve el nombre de fichero elegido ''' f=init_data(file_data) choose_filename(f) return f def comment_votes(file_id,comment): ''' Obtiene los votos de comentarios ''' comment_votes={} if "vs" in comment: for i,comment_vote in enumerate(usersdb.get_file_comment_votes(file_id)): if not comment_vote["_id"] in comment_votes: comment_votes[comment_vote["_id"][0:40]]=[0,0,0] if comment_vote["k"]>0: comment_votes[comment_vote["_id"][0:40]][0]+=1 else: comment_votes[comment_vote["_id"][0:40]][1]+=1 #si el usuario esta logueado y ha votado se guarda para mostrarlo activo if current_user.is_authenticated() and comment_vote["u"]==current_user.id: comment_votes[comment_vote["_id"][0:40]][2]=comment_vote["k"] return comment_votes #guardar los parametros desde donde se hizo la busqueda si procede args={} if request.referrer: querystring = urlparse(request.referrer).query if querystring: for params in u(querystring).encode("UTF-8").split("&"): param=params.split("=") if len(param) == 2: args[param[0]]=u(urllib.unquote_plus(param[1])) try: file_id=url2mid(file_id) except Exception as e: # Comprueba que se trate de un ID antiguo try: possible_file_id = filesdb.get_newid(file_id) if possible_file_id is None: logging.warn("%s - %s" % (e, file_id)) flash("link_not_exist", "error") abort(404) return redirect( url_for(".download", file_id=mid2url(possible_file_id), file_name=file_name), code=301) except filesdb.BogusMongoException as e: logging.exception(e) abort(503) try: data = filesdb.get_file(file_id, bl = None) except filesdb.BogusMongoException as e: logging.exception(e) abort(503) # intenta sacar el id del servidor de sphinx, # resuelve inconsistencias de los datos if not data: sid = get_id_server_from_search(file_id, file_name) if sid: try: data = filesdb.get_file(file_id, sid = sid, bl = None) except filesdb.BogusMongoException as e: logging.exception(e) abort(503) if data: if not data["bl"] in (0, None): if data["bl"] == 1: flash("link_not_exist", "error") elif data["bl"] == 3: flash("error_link_removed", "error") goback = True #block_files( mongo_ids=(data["_id"],) ) abort(404) else: flash("link_not_exist", "error") abort(404) #obtener los datos file_data=fill_data(data, True, file_name) if file_data["view"]["sources"]=={}: #si tiene todos los origenes bloqueados flash("error_link_removed", "error") abort(404) save_visited([file_data]) # Título title = u(file_data['view']['fn']) g.title = u"%s \"%s%s\"%s%s" % ( _(file_data['view']['action']).capitalize(), title[:50], "..." if len(title) > 50 else "", " - " if g.title else "", g.title) #obtener los archivos relacionados related_files = search_related(split_file(file_data["file"])[0][:10]) bin_file_id=mid2bin(file_id) ids=sorted({fid[0:3] for related in related_files for fid in get_ids(related) if fid[0]!=bin_file_id})[:5] files_related=[choose_filename_related(data) for data in get_files(ids)] #si el usuario esta logueado se comprueba si ha votado el archivo para el idioma activo vote=None if current_user.is_authenticated(): vote=usersdb.get_file_vote(file_id,current_user,g.lang) if vote is None: vote={"k":0} #formulario para enviar comentarios form = CommentForm(request.form) if request.method=='POST' and current_user.is_authenticated() and (current_user.type is None or current_user.type==0) and form.validate(): usersdb.set_file_comment(file_id,current_user,g.lang,form.t.data) form.t.data="" flash("comment_published_succesfully") #actualizar el fichero con la suma de los comentarios por idioma filesdb.update_file({"_id":file_id,"cs":usersdb.get_file_comments_sum(file_id),"s":file_data["file"]["s"]},direct_connection=True) #si tiene comentarios se guarda el número del comentario, el usuario que lo ha escrito, el comentario en si y los votos que tiene comments=[] if "cs" in file_data["file"]: comments=[(i,usersdb.find_userid(comment["_id"][0:24]),comment,comment_votes(file_id,comment)) for i,comment in enumerate(usersdb.get_file_comments(file_id,g.lang),1)] return render_template('files/download.html',file=file_data,args=args,vote=vote,files_related=files_related,comments=comments,form=form)
def download(file_id,file_name=None): ''' Muestra el archivo a descargar, votos, comentarios y archivos relacionados ''' def choose_filename_related(file_data): ''' Devuelve el nombre de fichero elegido ''' f=init_data(file_data) choose_filename(f) return f def comment_votes(file_id,comment): ''' Obtiene los votos de comentarios ''' comment_votes={} if "vs" in comment: for i,comment_vote in enumerate(usersdb.get_file_comment_votes(file_id)): if not comment_vote["_id"] in comment_votes: comment_votes[comment_vote["_id"][0:40]]=[0,0,0] if comment_vote["k"]>0: comment_votes[comment_vote["_id"][0:40]][0]+=1 else: comment_votes[comment_vote["_id"][0:40]][1]+=1 #si el usuario esta logueado y ha votado se guarda para mostrarlo activo if current_user.is_authenticated() and comment_vote["u"]==current_user.id: comment_votes[comment_vote["_id"][0:40]][2]=comment_vote["k"] return comment_votes if file_name is not None: g.title = _("download").capitalize()+" "+file_name+" - "+g.title else: g.title =_("download").capitalize() #guardar los parametros desde donde se hizo la busqueda si procede args={} if request.referrer: querystring = urlparse(request.referrer).query if querystring: for params in querystring.split("&"): param=params.split("=") if len(param) == 2: args[param[0]]=u(urllib.unquote_plus(param[1])) try: file_id=url2mid(file_id) except BaseException as e: logging.warn((e, file_id)) abort(404) data = filesdb.get_file(file_id, bl = None) if data: if not data["bl"] in (0, None): if data["bl"] == 1: flash("link_not_exist", "error") elif data["bl"] == 3: flash("error_link_removed", "error") goback = True #block_files( mongo_ids=(data["_id"],) ) abort(404) else: flash("link_not_exist", "error") abort(404) #obtener los datos file_data=fill_data(data, True, file_name) save_visited([file_data]) #obtener los archivos relacionados related_files = search_related(split_file(file_data["file"])[0][0:50]) bin_file_id=mid2bin(file_id) ids=sorted({fid for related in related_files for fid in get_ids(related) if fid[0]!=bin_file_id})[:5] files_related=[choose_filename_related(data) for data in get_files(ids)] #si el usuario esta logueado se comprueba si ha votado el archivo para el idioma activo vote=None if current_user.is_authenticated(): vote=usersdb.get_file_vote(file_id,current_user,g.lang) if vote is None: vote={"k":0} #formulario para enviar comentarios form = CommentForm(request.form) if request.method=='POST' and current_user.is_authenticated() and (current_user.type is None or current_user.type==0) and form.validate(): usersdb.set_file_comment(file_id,current_user,g.lang,form.t.data) form.t.data="" flash("comment_published_succesfully") #actualizar el fichero con la suma de los comentarios por idioma filesdb.update_file({"_id":file_id,"cs":usersdb.get_file_comments_sum(file_id),"s":file_data["file"]["s"]},direct_connection=True) #si tiene comentarios se guarda el número del comentario, el usuario que lo ha escrito, el comentario en si y los votos que tiene comments=[] if "cs" in file_data["file"]: comments=[(i,usersdb.find_userid(comment["_id"][0:24]),comment,comment_votes(file_id,comment)) for i,comment in enumerate(usersdb.get_file_comments(file_id,g.lang),1)] return render_template('files/download.html',file=file_data,args=args,vote=vote,files_related=files_related,comments=comments,form=form)
def api_v1(): method = request.args.get("method", None) results = () success = False try: if method == "getSearch": files = search_files(request.args["q"], request.args) results = enumerate(fill_data(file_data) for file_data in filesdb.get_files(get_ids(files),True)) success = True except BaseException as e: logging.debug(e) return render_template("api/v1.xml", api_method=method, results=results, success = success )
def search_ajax(): arg=dict(request.args) query = request.args.get("q", None) result = search_files(query, request.args,int(request.args.get("page", 1))) or {"total_found":0,"total":0,"time":0} taming, dym = taming_search(query, request.args.get("type", None)) try: tags = tags.next() except: tags = [] files = [{ "size": f["file"]["z"] if "z" in f["file"] else 0, "type": f["view"]["file_type"] if "file_type" in f["view"] else None, "link": url_for("files.download", file_id=f["view"]["url"], _external=True), "metadata": {k: (_api_v2_md_parser[k](v) if k in _api_v2_md_parser else v) for k, v in f["view"]["md"].iteritems()}, "html": render_template('files_ajax/file_ajax.html',file=f) } for f in (fill_data(file_data,text=query) for file_data in filesdb.get_files(get_ids(result), True))] return jsonify( result=result, files=files, tags=render_template('files_ajax/tags.html',tags=tags), no_results=render_template('files_ajax/no_results.html',filters=_filters) )