No functionality changes, just moved the functions around for better readability.
This commit is contained in:
92
findStray.py
92
findStray.py
@@ -52,26 +52,11 @@ def XOR(list1, list2):
|
||||
return set(list1) ^ set(list2)
|
||||
|
||||
|
||||
|
||||
def getNewFolderContents():
|
||||
showNames = getShowNames().keys()
|
||||
folderContents = filter( lambda f: not f.startswith('.'), os.listdir(showDir))
|
||||
|
||||
return XOR(folderContents, showNames)
|
||||
|
||||
|
||||
def checkForSingleEpisodes(folderItem):
|
||||
showName, hit = getFuzzyName(folderItem)
|
||||
episodeMatch = re.findall(re.sub(' ', '.', showName)+'\.S[0-9]{1,2}E[0-9]{1,2}\.', folderItem)
|
||||
|
||||
if episodeMatch:
|
||||
return True
|
||||
|
||||
|
||||
|
||||
def getByIdentifier(folderItem, identifier):
|
||||
itemMatch = re.findall(identifier + '[0-9]{1,2}', folderItem)
|
||||
# TODO Should be more precise than first item in list
|
||||
item = re.sub(identifier, '', itemMatch[0])
|
||||
# TODO Should be checking for errors
|
||||
return item
|
||||
|
||||
def getItemChildren(folder):
|
||||
@@ -90,30 +75,6 @@ def getItemChildren(folder):
|
||||
|
||||
return media_items, subtitles, trash
|
||||
|
||||
def getEpisodeInfo(folderItem):
|
||||
showName, hit = getFuzzyName(folderItem)
|
||||
season = getByIdentifier(folderItem, 'S')
|
||||
episode = getByIdentifier(folderItem, 'E')
|
||||
media_items, subtitles, trash = getItemChildren(folderItem)
|
||||
|
||||
episodeInfo = []
|
||||
episodeInfo = {'original': folderItem,
|
||||
'full_path': showDir + folderItem,
|
||||
'name': showName,
|
||||
'season': season,
|
||||
'episode': episode,
|
||||
'media_items': media_items,
|
||||
'subtitles': subtitles,
|
||||
'trash': trash,
|
||||
'tweet_id': None,
|
||||
'reponse_id': None,
|
||||
'verified': '0',
|
||||
'moved': '0'}
|
||||
|
||||
|
||||
addToDB(episodeInfo)
|
||||
return episodeInfo
|
||||
|
||||
|
||||
def addToDB(episodeInfo):
|
||||
conn = sqlite3.connect(dbPath)
|
||||
@@ -143,7 +104,56 @@ def addToDB(episodeInfo):
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
|
||||
def getNewFolderContents():
|
||||
# TODO Should not do on keys, if empty.
|
||||
showNames = getShowNames().keys()
|
||||
# TODO Better way to filter non dotfiles, dirread in filter?
|
||||
# Should maybe all dirs be checked at start?
|
||||
folderContents = filter( lambda f: not f.startswith('.'), os.listdir(showDir))
|
||||
|
||||
return XOR(folderContents, showNames) # OK
|
||||
|
||||
|
||||
def checkForSingleEpisodes(folderItem):
|
||||
# TODO also if empty, should be checked earlier
|
||||
showName, hit = getFuzzyName(folderItem)
|
||||
|
||||
episodeMatch = re.findall(re.sub(' ', '.', showName)+'\.S[0-9]{1,2}E[0-9]{1,2}\.', folderItem)
|
||||
if episodeMatch:
|
||||
return True # OK
|
||||
|
||||
|
||||
def getEpisodeInfo(folderItem):
|
||||
showName, hit = getFuzzyName(folderItem)
|
||||
season = getByIdentifier(folderItem, 'S')
|
||||
episode = getByIdentifier(folderItem, 'E')
|
||||
media_items, subtitles, trash = getItemChildren(folderItem)
|
||||
|
||||
episodeInfo = []
|
||||
episodeInfo = {'original': folderItem,
|
||||
'full_path': showDir + folderItem,
|
||||
'name': showName,
|
||||
'season': season,
|
||||
'episode': episode,
|
||||
'media_items': media_items,
|
||||
'subtitles': subtitles,
|
||||
'trash': trash,
|
||||
'tweet_id': None,
|
||||
'reponse_id': None,
|
||||
'verified': '0',
|
||||
'moved': '0'}
|
||||
|
||||
|
||||
addToDB(episodeInfo)
|
||||
return episodeInfo
|
||||
|
||||
|
||||
|
||||
def findStray():
|
||||
# TODO What if null or tries to pass down error
|
||||
for item in getNewFolderContents():
|
||||
if checkForSingleEpisodes(item):
|
||||
pprint(getEpisodeInfo(item))
|
||||
|
||||
@@ -17,40 +17,6 @@ auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
|
||||
auth.set_access_token(access_token, access_token_secret)
|
||||
api = tweepy.API(auth)
|
||||
|
||||
def unpackEpisodes():
|
||||
conn = sqlite3.connect(dbPath)
|
||||
c = conn.cursor()
|
||||
|
||||
cursor = c.execute('SELECT * FROM stray_episodes WHERE verified = 1 AND moved = 0')
|
||||
episodeList = []
|
||||
for row in c.fetchall():
|
||||
columnNames = [description[0] for description in cursor.description]
|
||||
|
||||
episodeDict = dict.fromkeys(columnNames)
|
||||
|
||||
for i, key in enumerate(episodeDict.keys()):
|
||||
episodeDict[key] = row[i]
|
||||
|
||||
episodeList.append(episodeDict)
|
||||
|
||||
conn.close()
|
||||
|
||||
return episodeList
|
||||
|
||||
|
||||
def createFolders(episode):
|
||||
showDir = '/media/hdd1/tv/%s/'% episode['name']
|
||||
episodeFormat = '%s S%sE%s/'% (episode['name'], episode['season'], episode['episode'])
|
||||
seasonFormat = '%s Season %s/'% (episode['name'], episode['season'])
|
||||
|
||||
if not os.path.isdir(showDir + seasonFormat):
|
||||
os.makedirs(showDir + seasonFormat)
|
||||
|
||||
if not os.path.isdir(showDir + seasonFormat + episodeFormat):
|
||||
os.makedirs(showDir + seasonFormat + episodeFormat)
|
||||
|
||||
|
||||
|
||||
def newnameMediaitems(media_items):
|
||||
# media_items = [['New.Girl.S06E18.720p.HDTV.x264-EZTV.srt', '-EZTV', 'nl'], ['New.Girl.S06E18.720p.HDTV.x264-FLEET.srt', '-FLEET', 'en']]
|
||||
media_items = json.loads(media_items)
|
||||
@@ -80,11 +46,46 @@ def updateMovedStatus(episodeDict):
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
def unpackEpisodes():
|
||||
conn = sqlite3.connect(dbPath)
|
||||
c = conn.cursor()
|
||||
|
||||
cursor = c.execute('SELECT * FROM stray_episodes WHERE verified = 1 AND moved = 0')
|
||||
episodeList = []
|
||||
for row in c.fetchall():
|
||||
columnNames = [description[0] for description in cursor.description]
|
||||
|
||||
episodeDict = dict.fromkeys(columnNames)
|
||||
|
||||
for i, key in enumerate(episodeDict.keys()):
|
||||
episodeDict[key] = row[i]
|
||||
|
||||
episodeList.append(episodeDict)
|
||||
|
||||
conn.close()
|
||||
|
||||
return episodeList
|
||||
|
||||
def createFolders(episode):
|
||||
showDir = '/media/hdd1/tv/%s/'% episode['name']
|
||||
episodeFormat = '%s S%sE%s/'% (episode['name'], episode['season'], episode['episode'])
|
||||
seasonFormat = '%s Season %s/'% (episode['name'], episode['season'])
|
||||
|
||||
if not os.path.isdir(showDir + seasonFormat):
|
||||
os.makedirs(showDir + seasonFormat)
|
||||
|
||||
if not os.path.isdir(showDir + seasonFormat + episodeFormat):
|
||||
os.makedirs(showDir + seasonFormat + episodeFormat)
|
||||
|
||||
def moveFiles(episode):
|
||||
# TODO All this should be imported from config file
|
||||
showDir = '/media/hdd1/tv/'
|
||||
episodeFormat = '%s S%sE%s/'% (episode['name'], episode['season'], episode['episode'])
|
||||
seasonFormat = '%s/%s Season %s/'% (episode['name'], episode['name'], episode['season'])
|
||||
|
||||
# TODO All this is pretty ballsy to do this hard/stict.
|
||||
newMediaitems = newnameMediaitems(episode['media_items'])
|
||||
for item in newMediaitems:
|
||||
old_location = showDir + episode['original'] + '/' + item[0]
|
||||
@@ -103,6 +104,7 @@ def moveFiles(episode):
|
||||
for trash in json.loads(episode['trash']):
|
||||
os.remove(showDir + episode['original'] + '/'+ trash)
|
||||
|
||||
# TODO Maybe move to delete folder instead, than user can dump.
|
||||
os.rmdir(showDir + episode['original'])
|
||||
|
||||
updateMovedStatus(episode)
|
||||
|
||||
Reference in New Issue
Block a user