0

I am making a board where you can post text, link, photo, and video. I use python flask, JavaScript for this

For video posting part, I wanted users to input YouTube link URL and I set this to automatically change into embed address. My python and ajax code work fine with YouTube URL format but the problem is when user input nothing for the video URL or put random letter or non-YouTube link format to the video link input.

python

@app.route('/post', methods=['POST'])
def savingPost():
    ...
    link = request.form['video_give'].split("=")[1]
    embed = "https://www.youtube.com/embed/"
    video = embed + link
    
    doc = {
        ...
        'video': video,
        ...
        }
    db.collection.insert_one(doc)

    return jsonify({'msg': 'Posted!'})

javascript

function postArticle(img_location) {
        ...
        let video = $('#post-video').val();
        ...
        $.ajax({
            type: "POST",
            url: "/post",
            data: {..., video_give: video},
            success: function (response) {
                alert(response["msg"]);

            }
        })

    }

How can I set code for situations for no input or other than youtube format?

davidism
  • 121,510
  • 29
  • 395
  • 339
Uncle Kim
  • 3
  • 5
  • You could try a test-requesst against the url and see if it comes back as 200 OK with some kind of contentType that fits a video - if not, invalidate it. – Patrick Artner Jun 10 '21 at 12:31

2 Answers2

0

A similar question that helps this question is Regex for youtube URL.

You can use RegExps to validate the youtube links before you send the request.

const ytRegex = /^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/
function postArticle(img_location) {
  ...
  let video = $('#post-video').val();
  if (ytRegex.test(video)){
    // Good format
  } else {
    // Bad format
  }
  ...
}
programmerRaj
  • 1,810
  • 2
  • 9
  • 19
  • YouTube URL seems to have 3 different types. For example this kpop song video 1. embed: https://www.youtube.com/embed/f-uLnhg9Cww 2. normal: https://www.youtube.com/watch?v=f-uLnhg9Cww 3. if click share button: https://youtu.be/f-uLnhg9Cww Your answer seems to check 3 but not 2. is that right? – Uncle Kim Jun 13 '21 at 09:42
  • The regex in my answer matches all three formats. You can go to https://regex101.com/r/2JaCGc/1/ (alt url: https://web.archive.org/web/20210613142749/https://regex101.com/r/2JaCGc/1/) to see that all three examples match. – programmerRaj Jun 13 '21 at 14:29
  • Thank you, this works good with my javascript side! If input correct form, url gets to passed to api and incorrect form passes 0 when I put else {video=0}. Now I gotta work on python to filter between correct form and 0 – Uncle Kim Jun 14 '21 at 03:43
  • I used the same regex in python and it work. Thank you programmerRaj, the great. – Uncle Kim Jun 14 '21 at 06:24
0

You could verify that the posted URL is a valid youtube link on the backend using regex.

import re
    
user_input = "https://youtube.com/watch?v=2bfsoiadbf"
video_id = re.findall("http[s]?:\/\/(?:youtube\.com|youtu\.be)\/watch\?(?:\w+\=(\w+)[\&]?)*", user_input)
   if video_id:
      # do something with the youtube id
      embed = "https://www.youtube.com/embed/"
      video = embed + video_id[0]
   else:
      # respond with an error

this way you only handle the URL if it's valid

GustavMH
  • 209
  • 2
  • 5
  • I use the re.findall(...) line you advised but this seems to filter out the correct YouTube url form too. I tested this by input correct youtube url form(https://www.youtube.com/watch?v=NuHNuMwe5mo) and put else : video = 1 then the data is being submitted as 1 to db – Uncle Kim Jun 14 '21 at 03:40
  • I used programmerRaj's regex in python and used your if else format with variables link = video_receive.split("=")[1] video = embed + linkin python and it works well. Thank you and I wish you all the blessings! – Uncle Kim Jun 14 '21 at 06:26