Big update, now looks to work. V0.1
This commit is contained in:
@@ -3,21 +3,43 @@
|
|||||||
# @Author: KevinMidboe
|
# @Author: KevinMidboe
|
||||||
# @Date: 2017-04-05 18:40:11
|
# @Date: 2017-04-05 18:40:11
|
||||||
# @Last Modified by: KevinMidboe
|
# @Last Modified by: KevinMidboe
|
||||||
# @Last Modified time: 2017-04-05 23:49:40
|
# @Last Modified time: 2017-04-06 15:58:48
|
||||||
import os.path, hashlib, time, glob, sqlite3, re
|
import os.path, hashlib, time, glob, sqlite3, re, json, tweepy
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
from fuzzywuzzy import process
|
from fuzzywuzzy import process
|
||||||
|
from langdetect import detect
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import env
|
import env
|
||||||
|
|
||||||
dirHash = None
|
dirHash = None
|
||||||
childList = []
|
childList = []
|
||||||
|
|
||||||
|
class twitter(object):
|
||||||
|
def __init__(self):
|
||||||
|
if '' in [env.consumer_key, env.consumer_secret, env.access_token, env.access_token_secret]:
|
||||||
|
print('not set')
|
||||||
|
|
||||||
|
self.consumer_key = env.consumer_key
|
||||||
|
self.consumer_secret = env.consumer_secret
|
||||||
|
self.access_token = env.access_token
|
||||||
|
self.access_token_secret = env.access_token_secret
|
||||||
|
|
||||||
|
self.authenticate()
|
||||||
|
|
||||||
|
def authenticate(self):
|
||||||
|
auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
|
||||||
|
auth.set_access_token(self.access_token, self.access_token_secret)
|
||||||
|
self.api_token = tweepy.API(auth)
|
||||||
|
|
||||||
|
def api(self):
|
||||||
|
return self.api_token
|
||||||
|
|
||||||
class mediaItem(object):
|
class mediaItem(object):
|
||||||
def __init__(self, parent, childrenList):
|
def __init__(self, parent, childrenList):
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.children = childrenList
|
self.children = childrenList
|
||||||
self.seriesName = self.findSeriesName()
|
self._id = hashlib.md5("b'{}'".format(self.parent).encode()).hexdigest()[:6]
|
||||||
|
self.showName = self.findSeriesName()
|
||||||
self.season = self.getSeasonNumber()
|
self.season = self.getSeasonNumber()
|
||||||
self.episode = self.getEpisodeNumber()
|
self.episode = self.getEpisodeNumber()
|
||||||
self.videoFiles = []
|
self.videoFiles = []
|
||||||
@@ -25,6 +47,9 @@ class mediaItem(object):
|
|||||||
self.trash = []
|
self.trash = []
|
||||||
self.getMediaItems()
|
self.getMediaItems()
|
||||||
|
|
||||||
|
self.saveToDB()
|
||||||
|
self.notifyInsert()
|
||||||
|
|
||||||
|
|
||||||
def findSeriesName(self):
|
def findSeriesName(self):
|
||||||
find = re.compile("^[a-zA-Z. ]*")
|
find = re.compile("^[a-zA-Z. ]*")
|
||||||
@@ -33,6 +58,9 @@ class mediaItem(object):
|
|||||||
name, hit = process.extractOne(m.group(0), getShowNames().keys())
|
name, hit = process.extractOne(m.group(0), getShowNames().keys())
|
||||||
if hit >= 90:
|
if hit >= 90:
|
||||||
return name
|
return name
|
||||||
|
else:
|
||||||
|
# This should be logged or handled somehow
|
||||||
|
pass
|
||||||
|
|
||||||
def getSeasonNumber(self):
|
def getSeasonNumber(self):
|
||||||
m = re.search('[sS][0-9]{1,2}', self.parent)
|
m = re.search('[sS][0-9]{1,2}', self.parent)
|
||||||
@@ -44,34 +72,67 @@ class mediaItem(object):
|
|||||||
if m:
|
if m:
|
||||||
return re.sub('[eE]', '', m.group(0))
|
return re.sub('[eE]', '', m.group(0))
|
||||||
|
|
||||||
def removeSignature(self, file):
|
def uploaderSignature(self, file):
|
||||||
match = re.search('-[a-zA-Z\[\]\-]*.[a-z]{3}', file)
|
match = re.search('-[a-zA-Z\[\]\-]*.[a-z]{3}', file)
|
||||||
if match:
|
if match:
|
||||||
uploader = match.group(0)[:-4]
|
uploader = match.group(0)[:-4]
|
||||||
if 'sdh' in uploader.lower():
|
|
||||||
return re.sub(uploader, '.sdh', file)
|
|
||||||
|
|
||||||
return re.sub(uploader, '', file)
|
return re.sub(uploader, '', file)
|
||||||
|
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
def getSubtitlesLang(self, subFile):
|
||||||
|
f = open('/'.join([env.show_dir, self.parent, subFile]), 'r', encoding='ISO-8859-15')
|
||||||
|
language = detect(f.read())
|
||||||
|
f.close()
|
||||||
|
if 'sdh' in subFile.lower():
|
||||||
|
return 'sdh.' + language
|
||||||
|
return language
|
||||||
|
|
||||||
def getMediaItems(self):
|
def getMediaItems(self):
|
||||||
for child in self.children:
|
for child in self.children:
|
||||||
if child[-3:] in env.mediaExt and child[:-4] not in env.mediaExcluders:
|
if child[-3:] in env.mediaExt and child[:-4] not in env.mediaExcluders:
|
||||||
self.videoFiles.append([child, self.removeSignature(child)])
|
self.videoFiles.append([child, self.uploaderSignature(child)])
|
||||||
elif child[-3:] in env.subExt:
|
elif child[-3:] in env.subExt:
|
||||||
# print([child, removeSignature(child), getLanguage(showDir + folder + '/', child)])
|
self.subtitles.append([child, self.uploaderSignature(child), self.getSubtitlesLang(child)])
|
||||||
self.subtitles.append([child, self.removeSignature(child)])
|
|
||||||
print(self.subtitles)
|
|
||||||
else:
|
else:
|
||||||
self.trash.append(child)
|
self.trash.append(child)
|
||||||
|
|
||||||
|
def notifyInsert(self):
|
||||||
|
# Send unique id. (time)
|
||||||
|
tweetObj = twitter()
|
||||||
|
api = tweetObj.api()
|
||||||
|
tweetString = 'Added episode:\n' + self.showName + ' S' + self.season\
|
||||||
|
+ 'E' + self.episode + '\nDetails: \n https://kevinmidboe.com/seasoned/verified.html?id=' + self._id
|
||||||
|
response = api.send_direct_message('kevinmidboe', text=tweetString)
|
||||||
|
|
||||||
|
|
||||||
|
def saveToDB(self):
|
||||||
|
# TODO Setup script
|
||||||
|
conn = sqlite3.connect(env.db_path)
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
path = '/'.join([env.show_dir, self.parent])
|
||||||
|
video_files = json.dumps(self.videoFiles)
|
||||||
|
subtitles = json.dumps(self.subtitles)
|
||||||
|
trash = json.dumps(self.trash)
|
||||||
|
|
||||||
|
try:
|
||||||
|
c.execute("INSERT INTO stray_eps ('id', 'parent', 'path', 'name', 'season', 'episode', 'video_files', 'subtitles', 'trash') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", \
|
||||||
|
[self._id, self.parent, path, self.showName, self.season, self.episode, video_files, subtitles, trash])
|
||||||
|
except sqlite3.IntegrityError:
|
||||||
|
print('Episode already registered')
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getDirContent(dir=env.show_dir):
|
def getDirContent(dir=env.show_dir):
|
||||||
try:
|
try:
|
||||||
return [d for d in os.listdir(dir) if d[0] != '.']
|
return [d for d in os.listdir(dir) if d[0] != '.']
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('Error: "' + dir + '" is not a directory.')
|
print('Error: "' + dir + '" is not a directory.')
|
||||||
|
exit(0)
|
||||||
|
|
||||||
# Hashes the contents of media folder to easily check for changes.
|
# Hashes the contents of media folder to easily check for changes.
|
||||||
def directoryChecksum():
|
def directoryChecksum():
|
||||||
@@ -105,12 +166,12 @@ def XOR(list1, list2):
|
|||||||
|
|
||||||
def filterChildItems(parent):
|
def filterChildItems(parent):
|
||||||
try:
|
try:
|
||||||
children = getDirContent(env.show_dir + parent)
|
children = getDirContent('/'.join([env.show_dir, parent]))
|
||||||
if children:
|
if children:
|
||||||
childList.append(mediaItem(parent, children))
|
childList.append(mediaItem(parent, children))
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
# Log to error file
|
# Log to error file
|
||||||
print('"' + env.show_dir + parent + '" is not a valid directory.')
|
print('Error: "' + '/'.join([env.show_dir, parent]) + '" is not a valid directory.')
|
||||||
|
|
||||||
def getNewItems():
|
def getNewItems():
|
||||||
newItems = XOR(getDirContent(), getShowNames())
|
newItems = XOR(getDirContent(), getShowNames())
|
||||||
@@ -122,9 +183,26 @@ def main():
|
|||||||
# TODO Verify env variables (showDir)
|
# TODO Verify env variables (showDir)
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
if directoryChecksum():
|
if directoryChecksum():
|
||||||
|
conn = sqlite3.connect(env.db_path)
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
cursor = c.execute('SELECT * FROM stray_eps WHERE id = "9110df"')
|
||||||
|
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()
|
||||||
|
print(json.dumps(episodeList))
|
||||||
getNewItems()
|
getNewItems()
|
||||||
|
|
||||||
print("--- %s seconds ---" % (time.time() - start_time))
|
print("--- %s seconds ---" % '{0:.4f}'.format((time.time() - start_time)))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user