Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """
- A simple little script for getting at the m3u8 for ufc.tv videos
- For free and ethical stream viewing free from proprietary players
- 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
- import logging
- from requests import session
- ufctv_session_path = os.path.expanduser("~/.ufctv")
- """
- Geo Block Elision
- Will only request first ufc.tv main page with proxy
- Nothing else will go through proxy
- Seems to work that way, thanks ukbro
- Have to proxy the HTTPS base site now, HTTP site doesn't cross-pollinate with HTTPS
- Can use an HTTP, HTTPS, SOCKS4/5 proxy, all good
- Added it as an argument now so you can just go
- --proxy "http://123.345.567.789:111"
- or whatever from command line
- """
- proxy_settings = {}
- def main():
- logging.basicConfig(level=logging.INFO)
- 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('video_url', nargs='?')
- parser.add_argument('-c', '--cam', default='0',
- help="Requests a different camera -- only works for PPVs!"
- )
- parser.add_argument('--proxy', help="Attempt to dodge geoblocking using a proxy")
- args = parser.parse_args()
- if args.proxy:
- proxy_settings['http'] = args.proxy
- proxy_settings['https'] = args.proxy
- op = args.operation
- if op == 'login':
- return ufctv_login()
- if not args.video_url:
- print("Missing argument: video_url", file=sys.stderr)
- return 1
- uri = get_m3u8(args.video_url, args.cam)
- if uri is None:
- return 2
- if args.operation == 'm3u8':
- print(uri)
- return 0
- def ufctv_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")})
- #let ufc.tv "feel the proxy"
- if 'https' in proxy_settings:
- c.get("https://www.ufc.tv/", proxies=proxy_settings)
- return c
- def get_m3u8_by_id(session, vid_id, cam):
- puburi = "https://www.ufc.tv/service/publishpoint"
- request = {
- 'format': 'json',
- 'type': 'video',
- 'id': vid_id,
- 'cam': cam
- }
- r = session.post(puburi, data=request)
- stream_info = json.loads(r.text)
- if 'path' not in stream_info:
- print("No stream m3u8 found, sorry.", file=sys.stderr)
- return None
- return stream_info['path'].replace("_iphone", "")
- def get_m3u8(uri, cam):
- with ufctv_session() as c:
- r = c.get(uri)
- if not logged_in(r.text):
- print("Goof Alert: You are not logged in to UFC.TV", file=sys.stderr)
- if video_geo_blocked(r.text):
- print("Geo-Block detected.", file=sys.stderr)
- 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 video_allowed(r.text):
- print("Video is not available to you for whatever reason", file=sys.stderr)
- return None
- return get_m3u8_by_id(c, vid_id, cam)
- def save_cookies(session):
- with open(ufctv_session_path, 'wb') as f:
- pickle.dump(session.cookies, f)
- def load_cookies(session):
- if not os.path.isfile(ufctv_session_path):
- return
- with open(ufctv_session_path, 'rb') as f:
- session.cookies.update(pickle.load(f))
- def video_geo_blocked(html):
- return '<div class="blackout-content">' in html
- def video_allowed(page_html):
- return '<div class="noAccess">' not in page_html
- def logged_in(page_html):
- return "var isLoggedIn = true;" in page_html
- def ufctv_login():
- if os.path.isfile(ufctv_session_path):
- os.remove(ufctv_session_path)
- with ufctv_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())
Add Comment
Please, Sign In to add comment