changing model, adding subtitles query, building structure

This commit is contained in:
Fred Pauchet 2011-12-28 16:10:02 +01:00
parent f3b14d2516
commit b6ef8c14da
3 changed files with 84 additions and 90 deletions

47
pigeonhole/model.py Normal file
View File

@ -0,0 +1,47 @@
from subQuery import *
class Structure(object):
def __init__(self):
print 'building myself'
class Show(object):
""" Represents a show file; ie. a file associated to its fullname """
path = None
name = None
directory = None
url = None
Seasons = list()
def append(self, season):
self.Seasons.append(season)
season.parent = self
def __init__(self, fullname, filename):
self.path = fullname
self.name = filename
self.directory = os.path.dirname(self.path)
url = queryShow(self.name)
def __str__(self):
return self.name
class Season(object):
""" Represents a season within a show """
seasonnumber = None
parent = None
def __init__(self, seasonnumber):
self.seasonnumber = seasonnumber
class Episode(object):
""" Represents an episode within a season """
title = None
def __init__(self, title):
self.title = title

View File

@ -5,6 +5,7 @@ import re
import shutil
import filecmp
import config
from model import Show
class Folder(object):
""" Directory show instanciation, relative to a path on the disk
@ -24,22 +25,6 @@ class Folder(object):
def __str__(self):
return self.name + ' [' + self.directory + ']'
class Show(object):
""" Represents a show file; ie. a file associated to its fullname """
path = None
name = None
directory = None
def __init__(self, fullname, filename):
self.path = fullname
self.name = filename
self.directory = os.path.dirname(self.path)
def __str__(self):
return self.name
class PigeonHole(object):
""" Takes all the media files in a (download) folder and sort
them into the corresponding folder, based on the found file name

View File

@ -10,6 +10,26 @@ from BeautifulSoup import BeautifulSoup
languages = ('en', 'es', 'fr', 'de')
""" not documented yet
"""
class CustomUrl(object):
fullUrl = None
suffix = None
base = None
def __init__(self, base, suffix):
self.base = base
self.suffix = suffix
self.fullUrl = base + suffix
def __str__(self):
return str(self.fullUrl)
def __unicode__(self):
return str(self.fullUrl)
def replace(self, oldstr, newstr):
return CustomUrl(self.base, self.suffix.replace(oldstr, newstr))
"""
Querying a base url with a specific regex and a query.
@ -21,6 +41,7 @@ languages = ('en', 'es', 'fr', 'de')
It will query the url, adds the query string and will fetch every href link that match the regular expression.
"""
def queryUrl(baseurl, paramindicator, regex, querystring):
#print '\tProbing ' + baseurl + ' ' + paramindicator + ' ' + regex + ' ' + querystring
socket = urllib2.urlopen(baseurl + paramindicator + querystring)
soup = BeautifulSoup(socket.read())
socket.close()
@ -31,7 +52,7 @@ def queryUrl(baseurl, paramindicator, regex, querystring):
for tag in tags:
bsoup = BeautifulSoup(str(tag))
mylist.append(baseurl + bsoup.a['href'])
mylist.append(CustomUrl(baseurl, bsoup.a['href']))
return mylist
@ -39,65 +60,14 @@ def queryShow(showname):
return queryUrl('http://www.tvsubtitles.net', '/search.php?q=', '/tvshow-([A-Za-z0-9]*)\.html$', showname.replace(' ', '%20'))
def querySeason(showname, seasonnumber):
return [x.replace('.html', '-' + str(seasonnumber) + '.html') for x in queryShow(showname)]
"""Supposed to return the url, according to the show name and season number"""
def getUrl(showname, seasonNumber, episodenumber, language):
Raise("not implemented yet")
pass
def query(showname):
results = queryShow(showname)
# a yield here would be cool ! :)
if len(results) == 1:
print results[0]
return results[0]
elif len(results) == 0:
print "No results found for " + showname
return None
else:
print "Here are the possible results for " + showname
for res in results:
print "\t" + str(res)
return None
""" Get a specific season, based on the show name and the season number
eg. getSeason('suits', 1)
getSeason('dexter', 3)
"""
def getSeason(showname, seasonNumber):
season = query(showname)
#idem
if season is not None:
print str(season).replace('.html', '-' + str(seasonNumber) + '.html')
else:
print "no season found"
""" Get a specific episode, based on the show name, the season number and the episode number
eg. getEpisode('being erica', 2, 12)
getEpisode('the big bang theory', 3, 15)
"""
def getEpisode(showname, seasonNumber, episodeNumber):
raise Exception('not yet implemented')
season = query(showname, seasonNumber)
urllib2.urlopen(season)
#idem
if episode is not None:
print str(episode).replace('.html', '-')
else:
print "no episode found"
"""Supposed to send to the right page, according to the right episode number"""
def getUrl(showname, seasonNumber, episodeNumber, language):
pass
""" Write a shortcut to a specific web page and fix the shortcutname within the writtent file.
@ -118,22 +88,14 @@ def writeUrlShortcut(folderpath, filename, url, shortcutname):
def walk(foldername):
for root, dirs, files in os.walk(foldername):
for directory in dirs:
if query(directory) is not None:
yield directory
def echo(var):
for x in var:
print x
if __name__ == "__main__":
#queryUrl('http://www.tvsubtitles.net/search?q=', 'tvshow')
for x in walk(r'C:\temp'):
print x
# query('the big bang theory')
# query('being erica')
# query('white collar')
# query('scrubs')
# query('castle')
for match in walk(r'C:\Tmp'):
print match
# getSeason('the big bang theory', 2)
# getSeason('white collar', 1)
# getSeason('suits', 1)
# getSeason('being erica', 2)
echo(queryShow('saison 1'))