def on_set_delete_activated(self, widget, view): selection = view.get_selection() model, paths = selection.get_selected_rows() for path in paths: iter = model.get_iter(path) id = model[iter][0] # tarname = make_tarname(model[iter][2]) tarpath = pathlib.Path(CONFIG['save_location'], tarname) target = pathlib.Path(CONFIG['complete_path'], tarname) # tarpath = os.path.join(CONFIG['save_location'], tarname) if tarpath.is_file(): try: if target.exists(): raise Exception(f'target already exist: {target}') tarpath.replace(target) # shutil.move(tarpath, CONFIG['complete_path']) Sets.delete().where(Sets.id == id).execute() Urls.delete().where(Urls.set_id == id).execute() model.remove(iter) print(target) except Exception as e: print(e) else: Sets.delete().where(Sets.id == id).execute() Urls.delete().where(Urls.set_id == id).execute() model.remove(iter)
def todo(request): salida = "" if request.method == "POST": nombre = request.POST["larga"] #pongo el http o https si hace falta if not nombre.startswith("http"): nombre = "http://" + nombre try: fila = Urls.objects.get(larga=nombre) salida += "<b>ERROR: la url '" + fila.larga + "' esta acortada ya en: " + str(fila.id) + "</b><br><br>" except Urls.DoesNotExist: salida += "Guardada correctamente la url '" + str(nombre) + "'<br><br>" #Guardo en la BD p = Urls(larga=nombre) p.save() salida += "<form action='' method='POST'>\n" salida += "Url a acortar: <input type='text' name='larga'>" salida += '<input type="submit" value="Acortar"><br>' salida += "<br><hr>Estas son las urls acortadas:<ul>" lista = Urls.objects.all() for fila in lista: salida += "<li>" + str(fila.id) + "->" + fila.larga + " " salida += "</ul>" return HttpResponse(salida)
def shorten_url(): payload = {} try: payload = request.get_json() original_url = payload.get("original_url", None) if not original_url: raise BadRequest("please provide url to shorten: %s" % original_url) url_id = payload.get("custom_id", None) if not url_id: url_id = SnowFlakeClient.get_unique_url_id() Urls.store_url(original_url=original_url, url_id=url_id) return jsonify({ "url_id": url_id, "new_url": "%s/%s" % ("http://localhost:8000", url_id) }) except BadRequest as e: return jsonify({"message": e.message}) except UniqueViolation as e: message = "No unique ids available at the moment. Please try again later." if payload.get("custom_id"): message = "custom url id: %s is not available. Please try with some other id." % payload.get( "custom_id") return jsonify({"message": message}) except Exception as e: return jsonify( {"message": "Service seems to be down. Please try again later."})
def steperr(row, set_iter, e): # set_model[set_iter][1] = Status.ERROR #active set_model[set_iter][7] += 1 Sets.update(error=set_model[set_iter][7]).where( Sets.id == row['set']).execute() Urls.update(status=str(e)).where( Urls.id == row['id']).execute()
def post(self): author1 = users.get_current_user().nickname() url = Urls(name=self.request.get('name'), shorturl=self.request.get('shorturl'), url=self.request.get('url'), author=author1) url.put() return webapp2.redirect('/')
def _get_url_from_soup(soup): for hotel in soup.find_all(attrs={'class': 'hotel_name'}): if hotel.a: try: Urls.create(url=hotel.a['href']) # print hotel.a['href'] except Exception, e: print hotel.a['href'], e
def _insertUrl(self, url, hasher): urlinfo = Urls() urlinfo.urlhash = hasher urlinfo.url = url urlinfo.save() page = Pages() page.url = urlinfo page.save() return urlinfo
def manejador(request): url_basica = "http://localhost:8000" metodo = request.method cuerpo = request.body if metodo == "GET": inicio = '<p><h1><center> Servidor acortador de URLs <center></h1><p>' formulario = '<center><FORM method="POST" action="">' + \ 'URL: <input type="text" name="url"><br>' + \ '<input type="submit" value="Enviar"></form><center>' lista = '<h4><center><p> URLs reales y acortadas hasta el momento: <p><center></h4>' try: lista_urls = Urls.objects.all() pagina = "" for url in lista_urls: pagina += "<p><a href='/" + url.url_larga + "'>" + url.url_larga + "</a>" + \ " ==> " + "<a href='/" + url.url_corta + "'>" + url.url_corta + "</a></p>" except Urls.DoesNotExist: pagina = ("<p><h1>Ha ocurrido un error. No hay paginas almacenadas</h1></p>") respuesta = inicio + formulario + lista + pagina elif metodo == "POST": try: lista_urls = Urls.objects.all() contador = len(lista_urls) except Urls.DoesNotExist: contador = 0 if len(cuerpo.split("=")) != 2 or cuerpo.split("=")[0] != "url": return HttpResponse("Error en el formulario") url = cuerpo.split("=")[1] url = urllib.unquote(url).decode('utf8') if url.split("://")[0] != "http" and url.split("://")[0] != "https": url_larga = "http://" + url else: url_larga = url try: page = Urls.objects.get(url_larga=url_larga) respuesta = ("La url que usted quiere añadir ya está en la lista de urls acortadas. Compruebe antes. ") respuesta = "<h4>" + respuesta + "La url acortada es: " + str(page.url_corta) + "</h4>" except Urls.DoesNotExist: url_acortada = "http://localhost:8000/" + str(contador) nueva = Urls(url_corta=url_acortada, url_larga=url_larga) nueva.save() respuesta= "<h3><a href=" + url_larga + ">" + url_larga + \ "</a> ==> " + "<a href=" + url_acortada + ">" + url_acortada + \ "<p></a> Vuelve a la pagina principal pinchando aqui: " + \ "<a href=" + url_basica + ">" + url_basica + "</p>" else: respuesta = "Método erróneo" return HttpResponse(respuesta)
def finish(self, set_iter, row, tarpath): set_model[set_iter][4] += 1 count = set_model[set_iter][5] done = set_model[set_iter][4] Sets.update(done=done).where(Sets.id == row['set']).execute() if done == count: set_model.remove(set_iter) # move file shutil.move(tarpath, CONFIG['complete_path']) # clean set in db Sets.delete().where(Sets.id == row['set']).execute() # register archive database? Urls.delete().where(Urls.id == row['id']).execute()
def on_row_activated(self, tree_view, path, column): model = tree_view.get_model() iter = model.get_iter(path) id = model[iter][0] qu = Urls.select(Urls.thumb).where(Urls.set_id == id).limit(4).tuples() urls = [q[0] for q in qu] popover = tree_view.thumb_view rect = tree_view.get_background_area(path, column) rect.y = rect.y + 24 popover.set_pointing_to(rect) # pool = Pool(4) def get_pix(url): req = request.Request(url, headers=firefox) response = request.urlopen(req) input_stream = Gio.MemoryInputStream.new_from_data( response.read(), None) def idle(input_stream): pixbuf = Pixbuf.new_from_stream(input_stream, None) popover.add_image(pixbuf, id) GObject.idle_add(idle, input_stream) for url in urls: t = threading.Thread(target=get_pix, args=(url, )) t.start() popover.popup()
def redirect_(url_id): try: result = Urls.get_original_url(url_id=url_id) if result: return redirect(result[0]["original_url"]) raise Exception("url_id: %s not found in db. It is probably expired." % url_id) except Exception as e: return jsonify({"message": str(e)})
def append_from_id(self, id, set_iter): qu = Urls.select(Urls, Sets).join(Sets).where(Urls.set_id == id).dicts() for q in qu: # print(q) self.que.put(( q, set_iter, )) info.que_len += self.que.qsize()
def redirect_redirect(url_id, sub_url): try: result = Urls.get_original_url(url_id=url_id) if result: url = "%s/%s" % (result[0]["original_url"], sub_url) return redirect(url) raise Exception("url_id not found in db. It is probably expired." % url_id) except Exception as e: return jsonify({"message": str(e)})
def index(request): if not request.POST: all_urls = Urls.objects.all() form = GifForm() return render(request, 'index.html', {"urls": all_urls, "form": form}) else: form = GifForm(request.POST) if form.is_valid(): formurl = form.cleaned_data['url'] url = Urls(urls=formurl) url.save() formkeys = form.cleaned_data['keywords'] keywords = formkeys.split(',') for keyz in keywords: key = Keywords(keywords=keyz) key.save() lookup = Lookup(keywords=key, urls=url) return HttpResponse(json.dumps({"success": True, "url": formurl})) else: return HttpResponse(json.dumps({"error": "Invalid form post"}))
def acorta(request,resourceName): response = "welcome to urlshort" + "<br/>" urlsList = Urls.objects.all() response += getUrls(urlsList) + "<br/>" if request.method == 'POST': if request.POST['name'] == "": return HttpResponseBadRequest("EMPTY POST") elif not request.POST['name'].startswith("http://") and not request.POST['name'].startswith("https://"): page = "http://" + request.POST['name'] newUrl = Urls(name=page) newUrl.save() response += "--Page: <a href=" + request.POST['name'] + ">" + request.POST['name'] + "</a></p>\n" response += "-- Url short: <a href=" + str(newUrl.id) + ">" + str(newUrl.id) + "</a></p>" elif request.method == 'GET': try: content = Urls.objects.get(name=resourceName) response += content.name except Urls.DoesNotExist: response += getform() return HttpResponse(response)
def acorta(request): if request.method == "GET": template = get_template('formulario_inicio.html') return HttpResponse(template.render(Context({}))) elif request.method == "POST": url = request.POST['url'] print "URL REAL ES " + str(url) if len(url) == 0: httpCode = "405 Method Not Allowed" htmlBody = "Go Away!" elif len(url) != 0: if url[0:7] == "http://": url_real = url elif url[0:8] == "https://": url_real = url else: url_real = "http://" + url print "URL REAL ES " + str(url_real) lista = Urls.objects.all() ya_acortada = False for elemento in lista: if elemento.larga == url_real: # quiere decir que ya ha sido acortada, busco su valor en la lista print url_real + "SI ESTA EN URLS_REALES!!!" url_acortada = Urls.objects.get(larga = url_real) url_acortada = url_acortada.id ya_acortada = True if ya_acortada == False: # si no esta en la lista significa que es nueva, la tengo que acortar y meterla a la lista nueva_url = Urls(larga = url_real) nueva_url.save() url_acortada = nueva_url.id template = get_template('plantilla_enlaces.html') return HttpResponse(template.render(Context({'url_acortada':url_acortada, 'url_real': url_real}))) else: httpCode = "405 Method Not Allowed" htmlBody = "Go Away!" return HttpResponse(htmlBody)
def get_comments(): while True: try: url = Urls.select().where(Urls.status=='new').limit(1).get().url # print url if not url: break Urls.update(status='done').where(Urls.url==url).execute() hotel_id = url.split('/')[2].split('.')[0] comments_driver.get(base_url + url) soup = BeautifulSoup(comments_driver.page_source, 'html.parser') try: name = soup.find(attrs={'itemprop': 'name'}).text.encode('utf-8') except Exception, e: print "hotel name not found ", e name = hotel_id Hotel.create(hotel_id=hotel_id, hotel_name=name) except Exception, e: print "Insert into hotel error ", e Urls.update(status='new').where(Urls.url==url).execute() continue
def formulario(request): salida = "" if request.method == "POST": if request.body == "": salida += "Error, debe escribir una url" else: cuerpo = request.body.split('valor=')[1] if cuerpo.find("http") == -1: cuerpo = "http://" + cuerpo else: cuerpo = cuerpo.replace('%3A%2F%2F', '://') try: fila=Urls.objects.get(larga=cuerpo) except Urls.DoesNotExist: fila = Urls(larga=cuerpo) fila.save() salida += "Url larga: " + "<a href='" + fila.larga salida += "'>" + fila.larga + "</a>" + "</br>" salida += "Url corta: " + "<a href='" + str(fila.id) salida += "'>" + str(fila.id) + "</a>" + "</br>" elif request.method == "GET": salida += '<form action="" method="POST">' salida += 'Introducir url : <input type="text" name="valor">' salida += '<input type="submit" value="Enviar">' salida += '</form>' lista = Urls.objects.all() salida += "</br></br>Las urls que hay son:<ul>" for fila in lista: salida += "<li> Url larga: " + fila.larga salida += " --> Url Corta: " + str(fila.id) salida += "</ul>" return HttpResponse(salida)
def formulario(request): salida = "" if request.method == "POST": if request.body == "": salida += "Error, debe escribir una url" else: cuerpo = request.body.split('valor=')[1] if cuerpo.find("http") == -1: cuerpo = "http://" + cuerpo else: cuerpo = cuerpo.replace('%3A%2F%2F', '://') try: fila = Urls.objects.get(larga=cuerpo) except Urls.DoesNotExist: fila = Urls(larga=cuerpo) fila.save() salida += "Url larga: " + "<a href='" + fila.larga salida += "'>" + fila.larga + "</a>" + "</br>" salida += "Url corta: " + "<a href='" + str(fila.id) salida += "'>" + str(fila.id) + "</a>" + "</br>" elif request.method == "GET": salida += '<form action="" method="POST">' salida += 'Introducir url : <input type="text" name="valor">' salida += '<input type="submit" value="Enviar">' salida += '</form>' lista = Urls.objects.all() salida += "</br></br>Las urls que hay son:<ul>" for fila in lista: salida += "<li> Url larga: " + fila.larga salida += " --> Url Corta: " + str(fila.id) salida += "</ul>" return HttpResponse(salida)
def all(request): if request.method == "GET": list = Urls.objects.all() out = "<html><body><ul>\n" for i in list: out += "<li><a href=" + i.url + ">" + str(i.id) + "</a></li>\n" out += "</ul>" + formulario + "</body></html>" elif request.method == "POST": url = request.POST.get("url") url = post(url) try: out = "Old URL" new = Urls.objects.get(url=url) except Urls.DoesNotExist: new = Urls(url=url) new.save() out = "New URL" out += "<html><body><ul>\n" out += "<li><a href=" + new.url + ">Url Original</a></li>\n" out += "<li><a href=" + str(new.id) + ">Url corta</a></li>\n" out += "</ul>" + formulario + "</body></html>" else: out = "Request not found" return HttpResponse(out)
def short(): full_url = request.form.get('full_url').strip().lower() if not full_url.startswith('http'): full_url = f'http://{full_url}' try: requests.get(full_url) except Exception as err: return {'short': 'Указанный Вами ресурс недоступен'} url = Urls.get(full_url=full_url) if url is None: short_url = shortener(full_url) short_json = {'short': short_url} else: short_json = {'short': f'{SERVER_URL}/{url.hashed_url}'} return short_json
def shortener(url): hashed_url = hashlib.md5(url.encode()).hexdigest()[:8] Urls(hashed_url=hashed_url, full_url=url) short_url = f'{SERVER_URL}/{hashed_url}' return short_url
def redirect_url(hashed_url): full_url = Urls.get(hashed_url=hashed_url) return redirect(full_url.full_url)
def get(self): urls = Urls.all() self.render_template('index.htm',{'urls':urls})