swift - Extract substring from URL string -
i have following url string:
eventid=vsl4wcedbnbjqgxe7bzqcw×tamp=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 go extracting m3u8 url above string? want once done:
i have following code stops @ point, 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 urlrequest, completionhandler: {(data, response, error) in // grab url returned youtube, url many parameters let feedback = nsstring(data: data!, encoding: string.encoding.utf8.rawvalue) // convert nsstring 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 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×tamp=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
your sample data tortuously long did not clarify question in way. it's better come minimum, complete , verifiable example.
now question, use urlcomponents
extract data url:
// assuming feedback 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×tamp=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") }
Comments
Post a Comment