import urllib2
import bs4
import lixian_cli
import socket
import os
from datetime import datetime
import ConfigParser

MAX_LIMIT = 5
CONFIG_FILE_NAME = "download.config"
filename = "the-daily_show"

#----------------------------------------------------------------------
def read_last_link():
	"""read the last downloaded link"""
	try:
		config = ConfigParser.ConfigParser()
		config.read(CONFIG_FILE_NAME)
		return unicode(config.get("DEFAULT", "last_link"))
	except Exception, e:
		print e
		return None
	
#----------------------------------------------------------------------
def write_last_link(last_link):
	"""write last downloaded link into config"""
	config = ConfigParser.ConfigParser()
	config.set("DEFAULT", "last_link", last_link)
	with open(CONFIG_FILE_NAME, "w") as config_file:
		config.write(config_file)

print "_" * 60
print str(datetime.now()) + ":Starting download......"


f = open(filename, "w")
#url = "http://eztv.it/shows/67/the-daily-show/"
url = "http://www.ttmeiju.com/meiju/The.daily.show.html"
f = urllib2.urlopen(url)
#content = urllib2.urlopen(url, timeout=3)
#html = []
#try:
	#line = content.readline()
	#while line:
		#html.append(line)
		#f.write(line)
		#line = content.readline()
	#f.close()
#except socket.timeout, e:
	#print "error:" + str(e)
	#f.close


##print html

#f = open(filename, "r")

"""
sample:
<a href="http://www.ttmeiju.com/download.php?id=15816" target="_blank" title="BT"><img src="http://www.ttmeiju.com/templates/ttmeiju/images/d1.gif" alt="BT"></a>
"""
soup = bs4.BeautifulSoup(f)

div = soup.find("div", "seedlistdiv")
if div is None:
	print "Error: seedlistdiv is not found!"
	exit()
	
all_list = div.findAll("a", title="BT")
if all_list is None or len(all_list) == 0:
	print "can't get manget links"
	exit()

print "Total count is %d" % len(all_list)

node_list = all_list[0: MAX_LIMIT]
link_list = [ node.get("href") for node in node_list]

#the list is sorted by file from old to new
link_list.reverse()
print "The Top %d list:" % (MAX_LIMIT)
for link in link_list: print link

last_link = read_last_link()
print "Last downloaed file is : %s" % last_link

new_index = 0

for i in range(0, len(link_list)):

	if link_list[i] == last_link:
		# Begin fetch with next file
		new_index = i + 1
		break

download_link_list = link_list[new_index:]


print "Download List is : " 
for link in download_link_list: print link


try:
	for link in download_link_list:
	

		#print singe_link
		#lixian_cli.add_task("\"" + singe_link + "\"")
		
		#download file from new to old until it meets last downloaed file 
		if link == last_link:
			print "The file has been added: %s" % singe_link
			break;
		
		command = ["--bt", link]
		lixian_cli.add_task(command)
		#print "\n%s: %s" % (datetime.now() , command)
	
		last_link = link
except Exception, e:
	print e
	
write_last_link(last_link)
print "\nIt's finished!"

413be36804aaf65a9f9b18c2c0ea90ccb50913d2
import urllib2
import bs4
import lixian_cli
import socket
import os
from datetime import datetime
import ConfigParser

MAX_LIMIT = 5
CONFIG_FILE_NAME = "download.config"
filename = "the-daily_show"

#----------------------------------------------------------------------
def read_last_link():
	"""read the last downloaded link"""
	try:
		config = ConfigParser.ConfigParser()
		config.read(CONFIG_FILE_NAME)
		return unicode(config.get("DEFAULT", "last_link"))
	except Exception, e:
		print e
		return None
	
#----------------------------------------------------------------------
def write_last_link(last_link):
	"""write last downloaded link into config"""
	config = ConfigParser.ConfigParser()
	config.set("DEFAULT", "last_link", last_link)
	with open(CONFIG_FILE_NAME, "w") as config_file:
		config.write(config_file)

print "_" * 60
print str(datetime.now()) + ":Starting download......"


f = open(filename, "w")
#url = "http://eztv.it/shows/67/the-daily-show/"
url = "http://www.ttmeiju.com/meiju/The.daily.show.html"
f = urllib2.urlopen(url)
#content = urllib2.urlopen(url, timeout=3)
#html = []
#try:
	#line = content.readline()
	#while line:
		#html.append(line)
		#f.write(line)
		#line = content.readline()
	#f.close()
#except socket.timeout, e:
	#print "error:" + str(e)
	#f.close


##print html

#f = open(filename, "r")

"""
sample:
<a href="http://www.ttmeiju.com/download.php?id=15816" target="_blank" title="BT"><img src="http://www.ttmeiju.com/templates/ttmeiju/images/d1.gif" alt="BT"></a>
"""
soup = bs4.BeautifulSoup(f)

div = soup.find("div", "seedlistdiv")
if div is None:
	print "Error: seedlistdiv is not found!"
	exit()
	
all_list = div.findAll("a", title="BT")
if all_list is None or len(all_list) == 0:
	print "can't get manget links"
	exit()

print "Total count is %d" % len(all_list)

node_list = all_list[0: MAX_LIMIT]
link_list = [ node.get("href") for node in node_list]

#the list is sorted by file from old to new
link_list.reverse()
print "The Top %d list:" % (MAX_LIMIT)
for link in link_list: print link

last_link = read_last_link()
print "Last downloaed file is : %s" % last_link

new_index = 0

for i in range(0, len(link_list)):

	if link_list[i] == last_link:
		# Begin fetch with next file
		new_index = i + 1
		break

download_link_list = link_list[new_index:]


print "Download List is : " 
for link in download_link_list: print link


try:
	for link in download_link_list:
	

		#print singe_link
		#lixian_cli.add_task("\"" + singe_link + "\"")
		
		#download file from new to old until it meets last downloaed file 
		if link == last_link:
			print "The file has been added: %s" % singe_link
			break;
		
		command = ["--bt", link]
		lixian_cli.add_task(command)
		#print "\n%s: %s" % (datetime.now() , command)
	
		last_link = link
except Exception, e:
	print e
	
write_last_link(last_link)
print "\nIt's finished!"
