-3

I have the following URL string:

eventid=VSl4WcedBNbjqgXe7bzQCw&timestamp=1501047125&status=ok&ldpj=-23&author=Al+Jazeera+Arabic+قناة+الجزيرة&ptchn=fiwzLy-8yKzIbsmZTzxDgw&live_chunk_readahead=3&video_id=fN2sEo7hq-E&allow_ratings=0&allow_embed=1&token=vThp8hY3AuC-jibt_jf7TEzWnuqUY7oImICFXKxkWbM=&plid=AAVVMcSvP1Tr6SGM&partnerid=27&of=sjG7v9N-R2P9uySMx2Af7Q&live_default_broadcast=1&watermark=,https://s.ytimg.com/yts/img/watermark/youtube_watermark-vflHX6b6E.png,https://s.ytimg.com/yts/img/watermark/youtube_hd_watermark-vflAzLcD6.png&streaminglib_module=1&live_storyboard_spec=https://i.ytimg.com/sb/fN2sEo7hq-E/storyboard_live_60_3x3_b0/M$M.jpg?rs=AOn4CLBoEYL8Dt3Yf1HhjHlpNUAc4jqmSg#106#60#3#3&video_verticals=[881,+397]&csn=VSl4WcedBNbjqgXe7bzQCw&muted=0&length_seconds=1800&hlsdvr=1&videostats_playback_base_url=https://s.youtube.com&title=Al+Jazeera+Arabic+Live+Stream+HD-+البث+الحي+لقناة+الجزيرة+الإخبارية+بجودة+عالية&ptk=aljazeera&iurl=https://i.ytimg.com/vi/fN2sEo7hq-E/hqdefault_live.jpg&vm=CAEQARgE&hlsvp=https://manifest.googlevideo.com/api/manifest/hls_variant/gcr/us/signature/26ADA228BD5549EC76852E925945A6EDBD8A78A9.268E76E7A0831E0DB3C36670FCA24805B0F9AD94/requiressl/yes/source/yt_live_broadcast/key/yt6/ip/72.239.175.191/ipbits/0/maudio/1/playlist_type/DVR/itag/0/expire/1501068725/sparams/gcr,go,id,ip,ipbits,itag,maudio,playlist_type,requiressl,source,expire/go/1/id/fN2sEo7hq-E.0/file/index.m3u8&pltype=contentlive&cl=162900125&iurlmq=https://i.ytimg.com/vi/fN2sEo7hq-E/mqdefault_live.jpg&enable_cardio_before_playback=1&no_get_video_log=1&short_view_count_text=3M+views&avg_rating=4.24240619416&fexp=9422596,9431012,9434289,9441392,9446364,9449243,9457141,9460072,9461821,9463594,9464546,9465833,9466793,9466795,9466797,9466851,9467217,9468797,9468799,9468805,9471755,9475953,9477113,9477691,9478524,9478670,9480034,9480475,9480535,9480795,9481684,9482647,9483080,9484209,9484514,9484643,9484706,9485999&keywords=al+jazeera+tv,al+jazeera+Arabic,الجزيره,اخبار,خبر,free+tv+channels,aljazira,اخبار+الجزيرة,الاسكندرية,الجزيرة+مصر+مباشر,al+jazeera+english+live,aljazeera+tv+live,داعش,العراق,اخبار+العراق,اخبار+سوريا,دمشق,اخبار+مصر,سوريا,بشار+الاسد,عاجل+ليبيا,عاجل,الحصاد,النشرة,نشرة,الاخبار,الاهرام,الوفد,الجزيرة+مباشر,الجمهورية,syria+news,البث+الحي,البث+الحي+لقناة+الجزيرة,جودة+عالية,hd&idpj=-4&c=WEB&innertube_client_version=1.20170718&account_playback_token=QUFFLUhqa3NJenBCUUNnZnBtMHdOb3FLSWE0cG9qQkZBZ3xBQ3Jtc0ttYkdZeEI0TExmQWo1RldLQlFZSGtLTE1wcEJGamNNZnE4UlhDaUpfRGtvSG5WbG1KUHNvbk44T2dtM2NRb0NwWmNZaU9sdGc2MTY3QUg3SldKTmtxOWNzMjVfZ0R5VDYwM1E3RDVQQnNHTU12OVphSQ==&iv3_module=1&iurlhq=https://i.ytimg.com/vi/fN2sEo7hq-E/hqdefault_live.jpg&dashmpd=https://manifest.googlevideo.com/api/manifest/dash/hfr/1/as/fmp4_audio_clear,fmp4_sd_hd_clear/source/yt_live_broadcast/key/yt6/ip/72.239.175.191/expire/1501068725/ipbits/0/signature/D9AC8668BF9CF6B884B618938C9823515B17D8F2.1FDA7E07B2B55FBCCF80C654BA9E0A1F9A48F589/playlist_type/DVR/itag/0/requiressl/yes/sparams/as,gcr,hfr,id,ip,ipbits,itag,playlist_type,requiressl,source,expire/gcr/us/id/fN2sEo7hq-E.0&ypc_ad_indicator=4&ucid=UCfiwzLy-8yKzIbsmZTzxDgw&iv_load_policy=1&iurlmaxres=https://i.ytimg.com/vi/fN2sEo7hq-E/maxresdefault_live.jpg&oid=o0grDQo8XcVLrPz5jByaaQ&iv_invideo_url=https://www.youtube.com/annotations_invideo?cap_hist=1&video_id=fN2sEo7hq-E&ei=VSl4WcedBNbjqgXe7bzQCw&cver=1.20170724&live_playback=1&fmt_list=&auth_timeout=21600000&thumbnail_url=https://i.ytimg.com/vi/fN2sEo7hq-E/default.jpg&root_ve_type=&has_cc=False&is_listed=1&url_encoded_fmt_stream_map=&fresca_module=1&enable_cardio=1&fresca_preroll=1&ypc_license_checker_module=1&iurlsd=https://i.ytimg.com/vi/fN2sEo7hq-E/sddefault_live.jpg&remarketing_url=https://googleads.g.doubleclick.net/pagead/viewthroughconversion/962985656/?backend=player_vars&cname=1&cver=AS3&data=backend%3Dplayer_vars%3Bcname%3D1%3Bcver%3DAS3%3Bptype%3Dview%3Btype%3Dview%3Butuid%3DfiwzLy-8yKzIbsmZTzxDgw%3Butvid%3DfN2sEo7hq-E&foc_id=fiwzLy-8yKzIbsmZTzxDgw&label=followon_view&ptype=view&delay=5&tmi=1&view_count=3025044&use_cipher_signature=False

How do I go about extracting the m3u8 url from the above string? I want it to look like this once done:

hlsvp=https://manifest.googlevideo.com/api/manifest/hls_variant/gcr/us/signature/26ADA228BD5549EC76852E925945A6EDBD8A78A9.268E76E7A0831E0DB3C36670FCA24805B0F9AD94/requiressl/yes/source/yt_live_broadcast/key/yt6/ip/72.239.175.191/ipbits/0/maudio/1/playlist_type/DVR/itag/0/expire/1501068725/sparams/gcr,go,id,ip,ipbits,itag,maudio,playlist_type,requiressl,source,expire/go/1/id/fN2sEo7hq-E.0/file/index.m3u8

I have the following code that stops at this point, as below:

extension ViewController {

/*https://stackoverflow.com/questions/35608686/how-can-i-get-the-actual-video-url-of-a-youtube-live-stream*/
func get_youtube_link(videoID: String) {

    let url: URL = URL(string: "https://www.youtube.com/get_video_info?&video_id=fN2sEo7hq-E&el=info&ps=default&eurl=&gl=US&hl=en")!

    let session = URLSession.shared

    var request = URLRequest(url: url)

    request.httpMethod = "GET"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

    let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in

        // grab the URL returned by YouTube, which is a URL with many parameters
        let feedback = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)

        // convert NSString to String
        var newString = String(feedback!)

        //https://www.uraimo.com/swiftbites/swift-url-encoding/
        newString = newString.removingPercentEncoding!
        newString = newString.replacingOccurrences(of: "%2C", with: ",")

        print(newString)


    })

    task.resume()

}

}

This is YouTube's web query data received in RAW format:

partnerid=27&root_ve_type=&c=WEB&iurl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fhqdefault_live.jpg&of=GC6OCn1lTVgpgJyrp2hYjw&hlsdvr=1&eventid=FEx5We_CJMut4QSH_5OYCw&ypc_license_checker_module=1&auth_timeout=21600000&videostats_playback_base_url=https%3A%2F%2Fs.youtube.com&token=WIs368zOOjdA6jb7Wo5HIeCUIjDqF6aTm40Qf9Lm8xY%3D&csn=FEx5We_CJMut4QSH_5OYCw&plid=AAVVQxkmPLDdDSm-&allow_ratings=1&live_playback=1&live_default_broadcast=1&fexp=9422596%2C9431012%2C9434289%2C9441386%2C9446364%2C9449243%2C9457141%2C9460072%2C9461821%2C9463594%2C9464546%2C9465833%2C9466793%2C9466795%2C9466797%2C9466851%2C9467217%2C9468797%2C9468799%2C9468805%2C9471755%2C9475953%2C9477113%2C9477691%2C9478524%2C9478670%2C9480034%2C9480475%2C9480535%2C9480795%2C9481684%2C9482647%2C9483080%2C9483701%2C9484209%2C9484514%2C9484643%2C9484706%2C9485999&fresca_module=1&idpj=-7&delay=5&iurlmq=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fmqdefault_live.jpg&avg_rating=4.53108348135&ypc_ad_indicator=4&remarketing_url=https%3A%2F%2Fgoogleads.g.doubleclick.net%2Fpagead%2Fviewthroughconversion%2F962985656%2F%3Fbackend%3Dplayer_vars%26cname%3D1%26cver%3DAS3%26foc_id%3DSrZ3UV4jOidv8ppoVuvW9Q%26label%3Dfollowon_view%26ptype%3Dno_rmkt&innertube_client_version=1.20170718&video_verticals=%5B16%2C+396%5D&ldpj=-27&title=euronews+LIVE&tmi=1&watermark=%2Chttps%3A%2F%2Fs.ytimg.com%2Fyts%2Fimg%2Fwatermark%2Fyoutube_watermark-vflHX6b6E.png%2Chttps%3A%2F%2Fs.ytimg.com%2Fyts%2Fimg%2Fwatermark%2Fyoutube_hd_watermark-vflAzLcD6.png&cver=1.20170725&iv3_module=1&no_get_video_log=1&has_cc=False&thumbnail_url=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fdefault.jpg&iv_invideo_url=https%3A%2F%2Fwww.youtube.com%2Fannotations_invideo%3Fcap_hist%3D1%26video_id%3DVmxIA8L3Xz8%26ei%3DFEx5We_CJMut4QSH_5OYCw&author=euronews+%28in+English%29&status=ok&ptk=youtube_none&fresca_preroll=1&iurlsd=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fsddefault_live.jpg&length_seconds=1800&view_count=159557&is_listed=1&vm=CAEQABgE&enable_cardio_before_playback=1&dashmpd=https%3A%2F%2Fmanifest.googlevideo.com%2Fapi%2Fmanifest%2Fdash%2Fas%2Ffmp4_audio_clear%252Cfmp4_sd_hd_clear%2Fgcr%2Fus%2Fexpire%2F1501143156%2Fsignature%2F7779193FA964EC075A94C62E6720B2380AEE39A0.B546BFA286574BA5D7BE116159ACC45130650938%2Fkey%2Fyt6%2Fip%2F72.239.175.191%2Fplaylist_type%2FDVR%2Fitag%2F0%2Fipbits%2F0%2Fsource%2Fyt_live_broadcast%2Frequiressl%2Fyes%2Fhfr%2F1%2Fsparams%2Fas%252Cgcr%252Chfr%252Cid%252Cip%252Cipbits%252Citag%252Cplaylist_type%252Crequiressl%252Csource%252Cexpire%2Fid%2FVmxIA8L3Xz8.0&hlsvp=https%3A%2F%2Fmanifest.googlevideo.com%2Fapi%2Fmanifest%2Fhls_variant%2Fgcr%2Fus%2Fkey%2Fyt6%2Fip%2F72.239.175.191%2Fplaylist_type%2FDVR%2Fexpire%2F1501143156%2Fitag%2F0%2Fsource%2Fyt_live_broadcast%2Fmaudio%2F1%2Fid%2FVmxIA8L3Xz8.0%2Fsparams%2Fgcr%252Cgo%252Cid%252Cip%252Cipbits%252Citag%252Cmaudio%252Cplaylist_type%252Crequiressl%252Csource%252Cexpire%2Fipbits%2F0%2Fgo%2F1%2Frequiressl%2Fyes%2Fsignature%2FB225396C754E6783728F71F0C1AD4CFE9AFC3839.B2C6E2FFCB07B136D4AECFA5A8F001DFEC084543%2Ffile%2Findex.m3u8&timestamp=1501121556&iurlmaxres=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fmaxresdefault_live.jpg&use_cipher_signature=False&live_chunk_readahead=3&iv_load_policy=1&live_storyboard_spec=https%3A%2F%2Fi.ytimg.com%2Fsb%2FVmxIA8L3Xz8%2Fstoryboard_live_60_3x3_b0%2FM%24M.jpg%3Frs%3DAOn4CLBjUwFPDoNQCv904oDJpmFEM5xg7g%23106%2360%233%233&ucid=UCSrZ3UV4jOidv8ppoVuvW9Q&allow_embed=1&pltype=contentugclive&short_view_count_text=159K+views&fmt_list=&keywords=&enable_cardio=1&video_id=VmxIA8L3Xz8&streaminglib_module=1&url_encoded_fmt_stream_map=&cl=162900125&account_playback_token=QUFFLUhqbl9LME8tdzdIT1VJaG1qRk1VZ3U1OFNnMW1PZ3xBQ3Jtc0ttY0dMZWZJdGtkUVNqTjlFWHpubjE2Rjg5OGxlU21fUndLa3Y0THoyMWpGa3ZXWDN2R3JkR1JHS2tEcm51OXhTbTFqYlNGTHVKY1hrOWllcFlHRU9NNHFZTjZNNGRZQjZ1eFc2VGxjMHpScWFDWnUxdw%3D%3D&muted=0&iurlhq=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fhqdefault_live.jpg

halkibsi
  • 335
  • 2
  • 14
  • Why was my question voted down? Let me know so that I understand what I did wrong as not to repeat it. Thanks! – halkibsi Jul 26 '17 at 05:07
  • 2
    Most likely because your question shows no effort of any kind. There are countless examples of extracting substrings in Swift. Do some basic research. Try something. Then [edit] your question to show the code you have tried (as text, not as an image or a link) and clearly explain what you need help with. – rmaddy Jul 26 '17 at 05:17
  • Thank you, but I have been working on this for severa hours. I wanted the question to be basic and easy to understand. That was my mistake. I will edit as you suggested. Thanks! – halkibsi Jul 26 '17 at 05:21
  • 1
    What is the output of `print(newString)`? Is it the first string in your original question? Is that the value where you need the leading and trailing `x`s removed? Why are there `x`'s on both ends of that string? – rmaddy Jul 26 '17 at 05:33
  • I have updated the question to reflect the answer to your question. – halkibsi Jul 26 '17 at 05:35
  • But is `newString` that giant "URL string" at the start of your question or is it something else? – rmaddy Jul 26 '17 at 05:37
  • Yes, the newString is that giant string block returned by YouTube – halkibsi Jul 26 '17 at 05:40

2 Answers2

2

So, I found the answer. I'll post it just incase someone needs it.

Here you go: Extract the M3U8 video link from a YouTube live channel in Swift 3

I'd like to point out that Code Different's answer is more elequent than mine. Give them both a try and see what fits your project needs :)

//
//  EXT_youtube_JSON.swift
//  YoutubeStreamURL
//
//  Created by Haitham Alkibsi on 7/25/17.
//  Copyright © 2017 Haitham Al-Kibsi. All rights reserved.
//
import Foundation

extension ViewController {

    /* https://stackoverflow.com/questions/35608686/how-can-i-get-the-actual-video-url-of-a-youtube-live-stream */
    func downloadJSON_youtube(videoID: String) {

        let url: URL = URL(string: "https://www.youtube.com/get_video_info?&video_id=\(videoID)&el=info&ps=default&eurl=&gl=US&hl=en")!

        let session = URLSession.shared

        var request = URLRequest(url: url)

        request.httpMethod = "GET"
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

        let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in

            // grab the URL returned by YouTube, which has many parameters
            var youtubeReply = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!

            // remove percent encoding
            //https://www.uraimo.com/swiftbites/swift-url-encoding/
            youtubeReply = youtubeReply.removingPercentEncoding!
            youtubeReply = youtubeReply.replacingOccurrences(of: "%2C", with: ",")


            //https://www.dotnetperls.com/find-swift
            let firstResult = youtubeReply.range(of: "hlsvp=", options: NSString.CompareOptions.literal, range: youtubeReply.startIndex..<youtubeReply.endIndex, locale: nil)

            if let range = firstResult {

                // Start of range of found string.
                let start = range.lowerBound

                // Display string starting at first index.
                var firstExtraction = youtubeReply[start..<youtubeReply.endIndex]
                firstExtraction = firstExtraction.chopPrefix(6)


                let secondResult = firstExtraction.range(of: ".m3u8", options: NSString.CompareOptions.literal, range: firstExtraction.startIndex..<firstExtraction.endIndex, locale: nil)

                if let range2 = secondResult {

                    let start = range2.lowerBound

                    let secondExtraction = firstExtraction[firstExtraction.startIndex..<start]

                    let m3u8_URL = secondExtraction + ".m3u8"
                    print(m3u8_URL)
                }
            }
        })

        task.resume()

    }
}

//https://stackoverflow.com/questions/28445917/what-is-the-most-succinct-way-to-remove-the-first-character-from-a-string-in-swi
extension String {
    func chopPrefix(_ count: Int = 1) -> String {
        return substring(from: index(startIndex, offsetBy: count))
    }

    func chopSuffix(_ count: Int = 1) -> String {
        return substring(to: index(endIndex, offsetBy: -count))
    }
}
halkibsi
  • 335
  • 2
  • 14
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/16831120) – Swati Jul 26 '17 at 07:25
  • @Swati I have updated the answer as requested. Please remove your down vote of the answer. The way you guys moderate this community sucks and has made this experience very negative. – halkibsi Jul 26 '17 at 22:40
  • i havent downvoted the answer: just added a review reason. I have learnt from my experience that links dont work all the time and answers become invalid – Swati Jul 27 '17 at 06:09
  • Your code gives me a chuckle: `String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!`. Could have done that with `String(data: data!, encoding: .utf8)!`. Code Different's answer is better. – Jenny Feb 11 '18 at 22:03
1

Your sample data is tortuously long but did not help clarify your question in any way. It's better to come up with Minimum, Complete and Verifiable example.

Now to your question, use URLComponents to extract data from the URL:

// Assuming feedback is what you get back from YouTube
let feedback = "partnerid=27&root_ve_type=&c=WEB&iurl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fhqdefault_live.jpg&of=GC6OCn1lTVgpgJyrp2hYjw&hlsdvr=1&eventid=FEx5We_CJMut4QSH_5OYCw&ypc_license_checker_module=1&auth_timeout=21600000&videostats_playback_base_url=https%3A%2F%2Fs.youtube.com&token=WIs368zOOjdA6jb7Wo5HIeCUIjDqF6aTm40Qf9Lm8xY%3D&csn=FEx5We_CJMut4QSH_5OYCw&plid=AAVVQxkmPLDdDSm-&allow_ratings=1&live_playback=1&live_default_broadcast=1&fexp=9422596%2C9431012%2C9434289%2C9441386%2C9446364%2C9449243%2C9457141%2C9460072%2C9461821%2C9463594%2C9464546%2C9465833%2C9466793%2C9466795%2C9466797%2C9466851%2C9467217%2C9468797%2C9468799%2C9468805%2C9471755%2C9475953%2C9477113%2C9477691%2C9478524%2C9478670%2C9480034%2C9480475%2C9480535%2C9480795%2C9481684%2C9482647%2C9483080%2C9483701%2C9484209%2C9484514%2C9484643%2C9484706%2C9485999&fresca_module=1&idpj=-7&delay=5&iurlmq=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fmqdefault_live.jpg&avg_rating=4.53108348135&ypc_ad_indicator=4&remarketing_url=https%3A%2F%2Fgoogleads.g.doubleclick.net%2Fpagead%2Fviewthroughconversion%2F962985656%2F%3Fbackend%3Dplayer_vars%26cname%3D1%26cver%3DAS3%26foc_id%3DSrZ3UV4jOidv8ppoVuvW9Q%26label%3Dfollowon_view%26ptype%3Dno_rmkt&innertube_client_version=1.20170718&video_verticals=%5B16%2C+396%5D&ldpj=-27&title=euronews+LIVE&tmi=1&watermark=%2Chttps%3A%2F%2Fs.ytimg.com%2Fyts%2Fimg%2Fwatermark%2Fyoutube_watermark-vflHX6b6E.png%2Chttps%3A%2F%2Fs.ytimg.com%2Fyts%2Fimg%2Fwatermark%2Fyoutube_hd_watermark-vflAzLcD6.png&cver=1.20170725&iv3_module=1&no_get_video_log=1&has_cc=False&thumbnail_url=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fdefault.jpg&iv_invideo_url=https%3A%2F%2Fwww.youtube.com%2Fannotations_invideo%3Fcap_hist%3D1%26video_id%3DVmxIA8L3Xz8%26ei%3DFEx5We_CJMut4QSH_5OYCw&author=euronews+%28in+English%29&status=ok&ptk=youtube_none&fresca_preroll=1&iurlsd=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fsddefault_live.jpg&length_seconds=1800&view_count=159557&is_listed=1&vm=CAEQABgE&enable_cardio_before_playback=1&dashmpd=https%3A%2F%2Fmanifest.googlevideo.com%2Fapi%2Fmanifest%2Fdash%2Fas%2Ffmp4_audio_clear%252Cfmp4_sd_hd_clear%2Fgcr%2Fus%2Fexpire%2F1501143156%2Fsignature%2F7779193FA964EC075A94C62E6720B2380AEE39A0.B546BFA286574BA5D7BE116159ACC45130650938%2Fkey%2Fyt6%2Fip%2F72.239.175.191%2Fplaylist_type%2FDVR%2Fitag%2F0%2Fipbits%2F0%2Fsource%2Fyt_live_broadcast%2Frequiressl%2Fyes%2Fhfr%2F1%2Fsparams%2Fas%252Cgcr%252Chfr%252Cid%252Cip%252Cipbits%252Citag%252Cplaylist_type%252Crequiressl%252Csource%252Cexpire%2Fid%2FVmxIA8L3Xz8.0&hlsvp=https%3A%2F%2Fmanifest.googlevideo.com%2Fapi%2Fmanifest%2Fhls_variant%2Fgcr%2Fus%2Fkey%2Fyt6%2Fip%2F72.239.175.191%2Fplaylist_type%2FDVR%2Fexpire%2F1501143156%2Fitag%2F0%2Fsource%2Fyt_live_broadcast%2Fmaudio%2F1%2Fid%2FVmxIA8L3Xz8.0%2Fsparams%2Fgcr%252Cgo%252Cid%252Cip%252Cipbits%252Citag%252Cmaudio%252Cplaylist_type%252Crequiressl%252Csource%252Cexpire%2Fipbits%2F0%2Fgo%2F1%2Frequiressl%2Fyes%2Fsignature%2FB225396C754E6783728F71F0C1AD4CFE9AFC3839.B2C6E2FFCB07B136D4AECFA5A8F001DFEC084543%2Ffile%2Findex.m3u8&timestamp=1501121556&iurlmaxres=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fmaxresdefault_live.jpg&use_cipher_signature=False&live_chunk_readahead=3&iv_load_policy=1&live_storyboard_spec=https%3A%2F%2Fi.ytimg.com%2Fsb%2FVmxIA8L3Xz8%2Fstoryboard_live_60_3x3_b0%2FM%24M.jpg%3Frs%3DAOn4CLBjUwFPDoNQCv904oDJpmFEM5xg7g%23106%2360%233%233&ucid=UCSrZ3UV4jOidv8ppoVuvW9Q&allow_embed=1&pltype=contentugclive&short_view_count_text=159K+views&fmt_list=&keywords=&enable_cardio=1&video_id=VmxIA8L3Xz8&streaminglib_module=1&url_encoded_fmt_stream_map=&cl=162900125&account_playback_token=QUFFLUhqbl9LME8tdzdIT1VJaG1qRk1VZ3U1OFNnMW1PZ3xBQ3Jtc0ttY0dMZWZJdGtkUVNqTjlFWHpubjE2Rjg5OGxlU21fUndLa3Y0THoyMWpGa3ZXWDN2R3JkR1JHS2tEcm51OXhTbTFqYlNGTHVKY1hrOWllcFlHRU9NNHFZTjZNNGRZQjZ1eFc2VGxjMHpScWFDWnUxdw%3D%3D&muted=0&iurlhq=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVmxIA8L3Xz8%2Fhqdefault_live.jpg"
let urlString = "https://www.youtube.com?" + feedback
let components = URLComponents(string: urlString)!

if let queryItems = components.queryItems,
    let hlsvp = queryItems.first(where: { $0.name == "hlsvp" })
{
    print(hlsvp.value!)
} else {
    print("hlsvp not found")
}
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Your answer has a URL in at, the sample data returned by YouTube is NOT a URL.. otherwise I would of used URLComponents. – halkibsi Jul 26 '17 at 22:44
  • 1
    What does exactly the web query return? You can always add an arbitrary domain name to it to make it a URL (it just has to be well-formed, doesn't need to be valid) – Code Different Jul 26 '17 at 22:46
  • The web query returns that mega code block you hated so much :) I added an arbitrary domain name and attempted to run your code block, but unfortunately components always returned Nil. – halkibsi Jul 26 '17 at 23:50
  • What is the value of `feedback`, before you percent-escape anything? Add the raw string to your question – Code Different Jul 26 '17 at 23:52
  • "feedback" is that BIG yellow block I keep referring you to that is already at the top of my question. – halkibsi Jul 27 '17 at 00:55
  • 1
    I want the raw string, **before you percent-escape anything**. Looking at your code, that's not the case. What you showed is the variable `newString`, not `feedback` – Code Different Jul 27 '17 at 01:02
  • Got it. Pasting it at the end of the question now. – halkibsi Jul 27 '17 at 02:13