Skip to content

Instantly share code, notes, and snippets.

@michael34435
Last active August 29, 2015 14:23
Show Gist options
  • Save michael34435/b4648a5a077d55f3f872 to your computer and use it in GitHub Desktop.
Save michael34435/b4648a5a077d55f3f872 to your computer and use it in GitHub Desktop.
download file from dmhy to your local system via put.io
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, requests, json, re, sys, codecs
from path import path
from pyquery import PyQuery as pq
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
__TOKEN__ = '{HERE_IS_YOUR_TOKEN}'
def __get_dmhy(page=1, download_list=None):
result = []
urls = ['http://share.dmhy.org', 'http://share.dmhy.org/topics/list/page/2', 'http://share.dmhy.org/topics/list/page/3', 'http://share.dmhy.org/topics/list/page/4', 'http://share.dmhy.org/topics/list/page/5']
for url in urls:
r = pq(url=url)
title_key = 0
for title_data in r('td.title>a').items():
for data in download_list:
match = re.match(data['regex'], title_data.eq(0).text())
if match:
magnet_link = r('a.arrow-magnet')
magnet = magnet_link.eq(title_key).attr('href')
filename = data['sample'].replace('{EPISODE}', match.group(1))
save_to = data['save_to']
result.append({'magnet': magnet, 'filename': filename, 'save_to': save_to})
title_key = title_key + 1
return result
def __send_download_request(magnet=None, filename=None, save_to=None):
files_list = json.loads(requests.get('https://api.put.io/v2/files/list?oauth_token=%s' % __TOKEN__).text)
print '[INFO] File detected:', os.path.isfile(save_to + '/' + filename), filename
print '[INFO] Ready to upload magnet link:', (not __check_file_exists(files_list, filename) and not os.path.isfile(save_to + '/' + filename))
print '[INFO] Ready to dowload file without uploading magnet link:', (__check_file_exists(files_list, filename) and not os.path.isfile(save_to + '/' + filename))
print '[INFO] magnet link is current upload and file is exists in put.io(should be false to prepare download):', (__check_file_exists(files_list, filename) and __has_upload_magnet(magnet))
if filename is not None and filename != '' and not __check_file_exists(files_list, filename) and not os.path.isfile(save_to + '/' + filename) and not (__check_file_exists(files_list, filename) and __has_upload_magnet(magnet)):
try:
post_back_data = json.loads(requests.post('https://api.put.io/v2/transfers/add?oauth_token=%s' % __TOKEN__, data={'url': magnet}).text)
if post_back_data['status'] == 'OK':
post_id = post_back_data['transfer']['id']
__write_pending(post_id=post_id, save_to=save_to, filename=filename, magnet=magnet)
except ValueError:
print '[ERROR] Upload magnet link error, link: %s' % magnet
elif __check_file_exists(files_list, filename) and not os.path.isfile(save_to + '/' + filename):
__write_pending(post_id=__check_file_exists(files_list, filename), save_to=save_to, filename=filename)
def __write_pending(post_id=None, save_to=None, filename=None, magnet=None):
pending = __get_pending_list()
pending.append({'id': post_id, 'save_to': save_to, 'filename': filename, 'magnet': magnet})
with open('/home/pi/dmhy/pending.json', 'w') as fn:
fn.write(json.dumps(pending))
fn.close()
def __has_upload_magnet(magnet):
pending = __get_pending_list()
for pending_data in pending:
if pending_data['magnet'] == magnet:
return True
return False
def __request_downloadable_file():
pending = __get_pending_list()
files_list = json.loads(requests.get('https://api.put.io/v2/files/list?oauth_token=%s' % __TOKEN__).text)
files_list = files_list['files']
result = []
files_name = []
for file_data in files_list:
for pending_data in pending:
if pending_data['filename'] == file_data['name'] and pending_data['filename'] not in files_name:
files_name.append(pending_data['filename'])
result.append({'id': file_data['id'], 'save_to': pending_data['save_to'], 'filename': pending_data['filename']})
for del_value in result:
for value in pending:
if del_value['filename'] == value['filename']:
pending.remove(value)
with open('/home/pi/dmhy/pending.json', 'w') as pending_fn:
pending_fn.write(json.dumps(pending))
pending_fn.close()
return result
def __get_download_list():
return json.loads(path('/home/pi/dmhy/list.json').bytes())
def __get_pending_list():
return json.loads(path('/home/pi/dmhy/pending.json').bytes())
def __check_file_exists(files_list=None, filename=None):
for value in files_list['files']:
if value['name'] == filename:
return value['id']
return False
def __download_and_delete(oid=None, save_to=None, filename=None):
if not os.path.isdir(save_to):
os.makedirs(save_to)
if oid is not None:
d = requests.get('https://api.put.io/v2/files/%s/download?oauth_token=%s' % (oid, __TOKEN__), stream=True)
with open(save_to + '/' + filename, 'wb') as fn:
for chunk in d.iter_content(chunk_size=1024):
if chunk:
fn.write(chunk)
requests.post('https://api.put.io/v2/files/delete?oauth_token=%s' % __TOKEN__, data={'file_ids': oid})
fn.close()
def check_pid():
current_pid = os.getpid()
if os.path.isfile('/home/pi/dmhy/pidfile.pid'):
with open('/home/pi/dmhy/pidfile.pid', 'rb') as pid:
old_pid = pid.read()
pid.close()
if old_pid.isdigit():
try:
os.kill(int(old_pid), 0)
except OSError:
pass
else:
sys.exit(0)
with open('/home/pi/dmhy/pidfile.pid', 'w') as pid:
pid.seek(0)
pid.write(str(current_pid))
pid.close()
def main():
check_pid()
print '[INFO] Starting download page from DMHY'
dmhy = __get_dmhy(page=1, download_list=__get_download_list())
print '[INFO] Sending download request to put.io'
for data in dmhy:
__send_download_request(magnet=data['magnet'], filename=data['filename'], save_to=data['save_to'])
print '[INFO] Request downloadable file from put.io'
downloads = __request_downloadable_file()
for download_data in downloads:
print '[INFO] id: %s, save_to: %s' % (download_data['id'], download_data['save_to'])
__download_and_delete(oid=download_data['id'], save_to=download_data['save_to'], filename=download_data['filename'])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment