简体   繁体   中英

Python: print a line if a specific string exists in the URL file

I am writing a script in Python which should print the link to the sub-profile (01.m3u8, 02.m3u8, 108.m3u8, etc.). This is my code:

#!/usr/bin/env python

from urllib import quote
import requests

input_file = quote(raw_input("Please enter the input file path: "), safe=':''/')

read_file = requests.get(input_file, verify = False)
for line in read_file:
        if "m3u8" in line:
                print line

The input stream is an HLS manifest file. This is the syntax of it:

#EXTM3U
#EXT-X-VERSION:4
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="deu_1",DEFAULT=YES,AUTOSELECT=YES,LANGUAGE="deu",URI="06.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="deu_2",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE="deu",URI="07.m3u8"
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="DEU_32_149",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE="DEU",URI="108.m3u8"
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=206701,RESOLUTION=320x180,FRAME-RATE=12.500,AUDIO="audio",SUBTITLES="subs"
01.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=681504,RESOLUTION=320x180,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
02.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1243460,RESOLUTION=480x270,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
03.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1917808,RESOLUTION=960x540,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
04.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3532769,RESOLUTION=1280x720,FRAME-RATE=50.000,AUDIO="audio",SUBTITLES="subs"
05.m3u8

And when I run my script the output is:

LECT=YES,LANGUAGE="deu",URI="06.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="deu_2",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE=
"deu",URI="07.m3u8"
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="DEU_32_149",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE="DEU",URI=
"108.m3u8"
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=206701,RESOLUTION=320x180,FRAME-RATE=12.500,AUDIO="audio",SUBTITLES="subs"

01.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=681504,RESOLUTION=320x180,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
0
2.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1243460,RESOLUTION=480x270,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
03
.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1917808,RESOLUTION=960x540,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
04.
m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3532769,RESOLUTION=1280x720,FRAME-RATE=50.000,AUDIO="audio",SUBTITLES="subs"
05.
m3u8

I know that currently I am printing the whole line, but why then it reports:

05.
m3u8

and not 05.m3u8

Apparently there is a m3u8 library in Python which can do exactly what I am looking for:

#!/usr/bin/env python
from urllib import quote
import m3u8

input_file = quote(raw_input("Please enter the input file path: "), safe=':''/')

m3u8_main = m3u8.load(input_file)
for playlist in m3u8_main.playlists:
        print playlist.uri

The full documentation can be found at: m3u8 library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM