Skip to content

Instantly share code, notes, and snippets.

@arifsuhan
Last active November 16, 2022 08:01
Show Gist options
  • Save arifsuhan/43422637d7ed70e9c2eca24afa036ebd to your computer and use it in GitHub Desktop.
Save arifsuhan/43422637d7ed70e9c2eca24afa036ebd to your computer and use it in GitHub Desktop.
VLC Playlist for Network Streaming
beautifulsoup4==4.11.1
requests==2.28.1

Prerequisites (requirements.txt)

python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt

Code

from bs4 import BeautifulSoup
import requests
import re


def get_soup(URL):
	base_URL = URL.split("/FTP")[0]
	r = requests.get(URL)
	return BeautifulSoup(r.content, 'html.parser'), base_URL


def get_obj(soup, base_URL):
	temp = soup.find_all('a',{'href': re.compile(r"/FTP-*")})
	return [ {'title': x.text , 'link': base_URL + x['href']} for x in temp]


def create_playlist(filename, data):

	with open(filename,"w") as file:
		file.write("#EXTM3U\n")

		for temp in data:
			file.write("#EXTINF:3244," + temp['title'] + "\n")
			file.write(temp['link'] + "\n")

# https://stackoverflow.com/questions/5194057/better-way-to-convert-file-sizes-in-python
def get_size(soup, base_URL):
	
	res = 0

	obj = soup.find_all('td',{'class': "fb-s"})
	clean_obj = [ float(x.text.replace("KB","")) for x in obj if x.text!="" ]
	
	# covert KB to bytes
	temp = sum(clean_obj) * 1024
	
	print(f"{temp/float(1<<20):,.0f} MB")
	print(f"{temp/float(1<<30):,.0f} GB")

URL = [FTP_URL]
soup, base_URL = get_soup(URL)
obj = get_obj(soup, base_URL)
create_playlist( [Filename.m3u], obj)

Example to Run

URL = "http://server4.ftpbd.net/FTP-4/English%20%26%20Foreign%20TV%20Series/Daredevil%20%28TV%20Series%202015-2018%20%29/720%20%5BDual%20Audio%5D/Season%201%20%5BHindi%20%20%2B%20English%5D%20720p/"
soup, base_URL = get_soup(URL)
obj = get_obj(soup, base_URL)
create_playlist("Daredevil_S1.m3u", obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment