def test_is_url(self): test_cases = ( (is_url("https://dubell.io"), True), (is_url("http://dubell.io"), True), (is_url("https://bookie.dubell.io"), True), (is_url("https://dubell.io/some/path/"), True), (is_url("dubell.io"), False), (is_url("ftp://some-server.local"), False), (is_url("1.1.1.1"), False), (is_url("http://0xA9FEA9FE/"), False), (is_url("http://*****:*****@dubell.io"), False), (is_url("http://[email protected]"), False), ) for test_case in test_cases: self.assertEqual(test_case[0], test_case[1])
def add_bookmark(request): """ Add a link from the dashboard """ if request.method == "POST": req_data = request.POST data = req_data.get("data") if is_url(data): parsed_html = parse_article(data) Bookmarks.objects.create(user=request.user, link=data, description=parsed_html["description"], title=parsed_html["title"], image=parsed_html["image"], body=parsed_html["body"]) return HttpResponse(status=200) else: Bookmarks.objects.create(user=request.user, link=data) return HttpResponse(status=200) return HttpResponse(status=405)
def telegram_api(request): """ Add bookmarks via telegram """ if request.method == "POST": data = json.loads(request.body.decode()) LOGGER.debug(data) try: telegram_username = data["message"]["from"]["username"] content = data["message"]["text"] chat_id = data["message"]["chat"]["id"] except KeyError as error: LOGGER.error(error) return HttpResponse(status=400) if content.startswith("/register"): cmd = content.strip().split(" ") if len(cmd) == 1: send_message(chat_id, "You forgot to enter your code...") return HttpResponse(status=200) if len(cmd) > 2: send_message( chat_id, "Please register with this format: /register token, where token is your token from Bookie" ) return HttpResponse(status=200) token = cmd[1] try: token_exists = Telegram.objects.get(token=token) if token_exists.activated: send_message( chat_id, "Bookie has already activated your account with this token!" ) return HttpResponse(status=200) except ObjectDoesNotExist: send_message( chat_id, "Hmm, looks like that token does not exist. Did you enter it correctly?" ) return HttpResponse(status=200) if timezone.now() < (token_exists.created + timedelta(minutes=3)): token_exists.activated = True token_exists.save() result, msg = send_message( chat_id, "Success, your telegram account is now linked to Bookie!") if not result: LOGGER.error( f"Could not send message to telegram user, status code: {msg}" ) LOGGER.info( f"User \"{token_exists.user.username}\" activated telegram integration" ) return HttpResponse(status=201) token_exists.token = get_random_string(length=5) token_exists.save() result, msg = send_message( chat_id, "Your token has expired, a new one has been generted.") if not result: LOGGER.error( f"Could send message to telegram user, status code: {msg}") LOGGER.info( f"User \"{token_exists.user.username}\" failed telegram integration, \ token expired") return HttpResponse(status=200) try: telegram = Telegram.objects.get( telegram_username=telegram_username) except ObjectDoesNotExist: send_message( chat_id, "That Telegram account name does not exist in Bookie, \ have you entered your username/firstname/lastname correctly?" ) return HttpResponse(status=200) if is_url(content): parsed_html = parse_article(content) if parsed_html: Bookmarks.objects.create( user=telegram.user, link=content, description=parsed_html["description"], title=parsed_html["title"], image=parsed_html["image"], body=parsed_html["body"]) else: send_message(chat_id, "Sorry, could not add that link :/") return HttpResponse(status=200) else: Bookmarks.objects.create(user=telegram.user, link=content) return HttpResponse(status="201") if request.method == "GET": return HttpResponse("yer a wizard, harry", content_type="text/plain")