Skip to content

Instantly share code, notes, and snippets.

@anoadragon453
Created May 12, 2015 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoadragon453/3be8cbcc3447732dd6bf to your computer and use it in GitHub Desktop.
Save anoadragon453/3be8cbcc3447732dd6bf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# House Hunters International episode downloader
# Code referenced from https://github.com/pantuts/asskick and https://stackoverflow.com/questions/12708751/python-writing-on-a-text-file
# Created by Andrew Morgan (2015)
from bs4 import BeautifulSoup
import os
import re
import requests
import subprocess
import sys
import math
torrent_download_dir = os.getcwd() + "/../torrents_watch/"
def check_if_previously_downloaded(torrent_name):
with open(os.getcwd() + "/" + 'torrent_list.txt','r+') as torrent_list:
for line in torrent_list:
if line.strip() == torrent_name:
print "torrent already downloaded"
return True
else:
torrent_list.write(torrent_name + "\n")
return False
def download_torrent(url):
fname = torrent_download_dir + '/' + url.split('title=')[-1] + '.torrent'
if check_if_previously_downloaded(url.split('title=')[-1] + '.torrent'):
return
print ("Downloading " + url.split('title=')[-1])
# http://stackoverflow.com/a/14114741/1302018
try:
r = requests.get(url, stream=True)
with open(fname, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
except requests.exceptions.RequestException as e:
print('\n' + str(e))
sys.exit(1)
return fname
def retrieve_torrent_listings(url,torrent):
try:
cont = requests.get(url)
except requests.exceptions.RequestException as e:
raise SystemExit('\n' + str(e))
# check if no torrents found
if not re.findall(r'Download torrent file', str(cont.content)):
print('Torrents found: 0')
retrieve_torrent_listings()
else:
soup = BeautifulSoup(cont.content)
al = [s.get_text() for s in soup.find_all('td', {'class':'center'})]
href = [a.get('href') for a in soup.find_all('a', {'title':'Download torrent file'})]
size = [t.get_text() for t in soup.find_all('td', {'class':'nobr'}) ]
title = [ti.get_text() for ti in soup.find_all('a', {'class':'cellMainLink'})]
age = al[2::5]
seeders = al[3::5]
leechers = al[4::5]
# Check if torrent has seeders
if (int(seeders[torrent-1]) > 0):
print (seeders[torrent-1] + " seeders found.")
fname = download_torrent(href[int(torrent)-1])
else:
print ("No seeders found")
def download_show(initial_torrent_amount):
torrent_amount = initial_torrent_amount
pages = int(math.ceil(torrent_amount/25))
print ("pages: " + str(pages))
for i in xrange(1,pages + 1):
if pages > 1 and i == pages:
torrent_amount = initial_torrent_amount % 25.0
else:
torrent_amount = 25
for j in xrange(1,int(torrent_amount)+1):
print ("\nTrying page " + str(i) + " torrent num #" + str(j))
try:
retrieve_torrent_listings("http://kickass.to/usearch/house%20hunters%20international%20720%20DOCERE/" + str(i) + "/?field=time_add&sorder=desc",j)
except IndexError, e:
print "Reached end of torrent queue"
return
download_show(300.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment