Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python3
- """
- A simple little script for getting at the m3u8 for ufc.tv videos
- For free and ethical stream viewing free from proprietary players
- Doesn't circumvent geo-blocks or login requirements
- You can get around the ufc.tv geo blocks via DNS proxies
- I have this saved as ufctv in a bin dir on my $PATH, so I can call it from wherever
- To login, run: ufctv login
- That will begin an interactive login prompt
- The login details themselves aren't saved, just the cookies for the session
- And the active session is reset any time you login with the "Keep Me Signed In" box anywhere else
- To get a video's m3u8: ufctv m3u8 $UFCTV-URL
- So for example if I want to go back and watch Belfort vs Hendo 3
- % ufctv m3u8 http://www.ufc.tv/video/belfort-vs-henderson-3
- From there you an do what you wish with the m3u8!
- You may wish to pipe the result direct to mpv (which lets you seek around and switch between qualities)
- % ufctv m3u8 http://www.ufc.tv/video/belfort-vs-henderson-3 | xargs mpv
- Or start ripping with livestreamer
- % ufctv m3u8 http://www.ufc.tv/video/belfort-vs-henderson-3 | xargs -I M3U8 livestreamer hlsvariant://M3U8 best -o belf-vs-hend.ts
- You can also rip the stream with ffmpeg, though I've found livestreamer does it a bit cleaner
- You may also wish to expand the program to add commands to do this for you instead of via shell piping
- """
- import argparse
- import getpass
- import json
- import os
- import pickle
- import re
- import subprocess
- import sys
- from requests import session
- def main():
- parser = argparse.ArgumentParser(description=
- """
- This is a command line tool to help enjoy quality UFC Fight Pass
- content in an ethical manner that respects your freedoms
- """)
- parser.add_argument('operation',
- choices=['login', 'm3u8'])
- parser.add_argument('rest', nargs='*')
- args = parser.parse_args()
- op = args.operation
- if op == 'login':
- return ufctv_login()
- if len(args.rest) < 1:
- print("Missing argument: video_url", file=sys.stderr)
- return 1
- vid_url = args.rest[0]
- uri = get_m3u8(vid_url)
- if uri is None:
- return 2
- if args.operation == 'm3u8':
- print(uri)
- return 0
- def fake_mobile_session():
- with session() as c:
- load_cookies(c)
- c.headers.update({'User-Agent': (
- "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us)"
- " AppleWebKit/528.18 (KHTML, like Gecko)"
- " Version/4.0 Mobile/7A341 Safari/528.16")})
- return c
- def get_m3u8_by_id(session, vid_id):
- puburi = "http://www.ufc.tv/service/publishpoint?type=video&id={}&format=json"
- r = session.get(puburi.format(vid_id))
- stream_info = json.loads(r.text)
- if 'path' not in stream_info:
- return None
- return stream_info['path'].replace("_iphone", "")
- def get_m3u8(uri):
- with fake_mobile_session() as c:
- r = c.get(uri)
- vid_id = find_video_id(r.text)
- if vid_id is None:
- print("Failed to find video ID in page.", file=sys.stderr)
- return None
- if not logged_in(r.text):
- print("Goof Alert: You are not logged in to UFC.TV", file=sys.stderr)
- if not video_allowed(r.text):
- print("Video is not available", file=sys.stderr)
- return None
- res = get_m3u8_by_id(c, vid_id)
- save_cookies(c)
- return res
- ufctv_sesssion_path = os.path.expanduser("~/.ufctv")
- def save_cookies(session):
- with open(ufctv_sesssion_path, 'wb') as f:
- pickle.dump(session.cookies, f)
- def load_cookies(session):
- if not os.path.isfile(ufctv_sesssion_path):
- print("No saved cookies file found -- starting from scratch", file=sys.stderr)
- return
- with open(ufctv_sesssion_path, 'rb') as f:
- session.cookies.update(pickle.load(f))
- def video_allowed(page_html):
- return '<div class="noAccess">' not in page_html
- def logged_in(page_html):
- return "Sign Out" in page_html
- def ufctv_login():
- with fake_mobile_session() as c:
- login(c)
- save_cookies(c)
- def login(sesh):
- username = input("Please enter your ufc.tv username: ")
- password = getpass.getpass("Please enter your ufc.tv password: ")
- longchoice = input("Tick the 'Keep Me Signed In' box for a long lasting session? [y/n] ")
- longsesh = 'true' if longchoice is 'y' else 'false'
- login = {
- 'username': username,
- 'password': password,
- 'cookielink': longsesh
- }
- r = sesh.post("https://www.ufc.tv/secure/authenticate", data=login)
- if 'loginsuccess' not in r.text:
- print("Login failure -- full response:\n{}\n".format(r.text), file=sys.stderr)
- return False
- return True
- def find_video_id(page_html):
- m = re.search("rel=\"image_src\" href=\".*?([0-9]+?)_.*?\.jpg\"", page_html)
- if m is None:
- return m
- return m.group(1)
- if __name__ == "__main__":
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement