Last active
August 21, 2017 05:49
-
-
Save kangfend/dca9cb6e20250d8ea559d9b75b2534b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # Copyright © 2016 Sutrisno Efendi <[email protected]> | |
| # Auto download video from akhyar.tv | |
| import argparse | |
| import os | |
| import re | |
| import requests | |
| class Downloader: | |
| def __init__(self, url): | |
| self.url = url | |
| self.files = [] | |
| self.collect_data() | |
| def collect_data(self): | |
| punctuation = "!@#$%^&*()'" | |
| response = requests.get(self.url) | |
| filename = re.findall('http://stream.asianastream.com/vod/akhyartv/(.*)/index.m3u8', response.content) | |
| if filename: | |
| self.video_filename = filename[0] | |
| self.video_name, _ = self.video_filename.split('.') | |
| self.video_name = self.video_name.translate(None, punctuation) | |
| self.temporary_path = '%s/' % self.video_name.translate(None, punctuation) | |
| if not os.path.isdir(self.temporary_path): | |
| os.mkdir(self.temporary_path) | |
| def merge_video(self): | |
| collections = " ".join(self.files) | |
| os.system('cat %s > %s.mp4' % (collections, self.video_name)) | |
| os.system('rm -rf %s/' % self.video_name) | |
| print "Video saved to %s" % '%s.mp4' % self.video_name | |
| def run(self): | |
| number = 1 | |
| while True: | |
| filename = 'seg-%s-v1-a1.ts' % number | |
| path = self.temporary_path + filename | |
| download_url = 'http://stream.asianastream.com/vod/akhyartv/%s/%s' % (self.video_filename, filename) | |
| response = requests.get(download_url) | |
| if response.status_code != 200: | |
| self.merge_video() | |
| break | |
| with open(path, 'wb') as ts_file: | |
| print download_url | |
| ts_file.write(response.content) | |
| self.files.append(path) | |
| number += 1 | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser(description='AkyarTV Downloader') | |
| parser.add_argument('url', nargs=1) | |
| args = parser.parse_args() | |
| if args.url: | |
| downloader = Downloader(args.url[0]) | |
| downloader.run() | |
| else: | |
| parser.print_help() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment