Moved old files to modules from root folder.

This commit is contained in:
Kevin Midboe
2017-04-07 11:33:19 +02:00
parent b7f894b407
commit 43e1e88865
14 changed files with 0 additions and 484 deletions

26
modules/addSubLanguage.py Executable file
View File

@@ -0,0 +1,26 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-03-04 13:46:28
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-03-04 14:03:57
from langdetect import detect
from removeUploader import removeUploader
testFiles = ['subs/The.Man.from.U.N.C.L.E.2015.1080p-[eztv].srt',
'subs/The.Man.from.U.N.C.L.E.2015.1080p-[eztv]ENGLUISH.srt']
def detectLanguage(file):
f = open(file, 'r', encoding= 'ISO-8859-15')
language = detect(f.read())
f.close()
return removeUploader(file)[:-3] + language + '.srt'
def addLangExtension():
for file in testFiles:
print(detectLanguage(file))
if __name__ == '__main__':
addLangExtension()

View File

@@ -1,211 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-04-05 18:40:11
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-04-06 15:58:48
import os.path, hashlib, time, glob, sqlite3, re, json, tweepy
from functools import reduce
from fuzzywuzzy import process
from langdetect import detect
from time import sleep
import env
dirHash = None
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):
def __init__(self, parent, childrenList):
self.parent = parent
self.children = childrenList
self._id = hashlib.md5("b'{}'".format(self.parent).encode()).hexdigest()[:6]
self.showName = self.findSeriesName()
self.season = self.getSeasonNumber()
self.episode = self.getEpisodeNumber()
self.videoFiles = []
self.subtitles = []
self.trash = []
self.getMediaItems()
self.saveToDB()
self.notifyInsert()
def findSeriesName(self):
find = re.compile("^[a-zA-Z. ]*")
m = re.match(find, self.parent)
if m:
name, hit = process.extractOne(m.group(0), getShowNames().keys())
if hit >= 90:
return name
else:
# This should be logged or handled somehow
pass
def getSeasonNumber(self):
m = re.search('[sS][0-9]{1,2}', self.parent)
if m:
return re.sub('[sS]', '', m.group(0))
def getEpisodeNumber(self):
m = re.search('[eE][0-9]{1,2}', self.parent)
if m:
return re.sub('[eE]', '', m.group(0))
def uploaderSignature(self, file):
match = re.search('-[a-zA-Z\[\]\-]*.[a-z]{3}', file)
if match:
uploader = match.group(0)[:-4]
return re.sub(uploader, '', file)
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):
for child in self.children:
if child[-3:] in env.mediaExt and child[:-4] not in env.mediaExcluders:
self.videoFiles.append([child, self.uploaderSignature(child)])
elif child[-3:] in env.subExt:
self.subtitles.append([child, self.uploaderSignature(child), self.getSubtitlesLang(child)])
else:
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):
try:
return [d for d in os.listdir(dir) if d[0] != '.']
except FileNotFoundError:
print('Error: "' + dir + '" is not a directory.')
exit(0)
# Hashes the contents of media folder to easily check for changes.
def directoryChecksum():
dirList = getDirContent()
# Creates a string of all the list items.
dirConcat = reduce(lambda x, y: x + y, dirList, "")
m = hashlib.md5()
m.update(bytes(dirConcat, 'utf-16be')) # String to byte conversion.
global dirHash
if dirHash != m.digest():
dirHash = m.digest()
return True
return False
def getShowNames():
conn = sqlite3.connect(env.db_path)
c = conn.cursor()
c.execute('SELECT show_names, date_added, date_modified FROM shows')
returnList = {}
for name, added, modified in c.fetchall():
returnList[name] = [added, modified]
conn.close()
return returnList
def XOR(list1, list2):
return set(list1) ^ set(list2)
def filterChildItems(parent):
try:
children = getDirContent('/'.join([env.show_dir, parent]))
if children:
childList.append(mediaItem(parent, children))
except FileNotFoundError:
# Log to error file
print('Error: "' + '/'.join([env.show_dir, parent]) + '" is not a valid directory.')
def getNewItems():
newItems = XOR(getDirContent(), getShowNames())
for item in newItems:
filterChildItems(item)
def main():
# TODO Verify env variables (showDir)
start_time = time.time()
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()
print("--- %s seconds ---" % '{0:.4f}'.format((time.time() - start_time)))
if __name__ == '__main__':
while True:
main()
sleep(2)

125
modules/folderCreator.py Executable file
View File

@@ -0,0 +1,125 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-03-05 13:52:45
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-03-05 17:14:25
import sqlite3, json, os, tweepy
from re import sub
dbPath = 'shows.db'
consumer_key, consumer_secret = 'yvVTrxNtVsLkoHxKWxh4xvgjg', '39OW6Q8fIKDXvTPPCaEJDULcYaHC5XZ3fe7HHCGtdepBKui2jK'
access_token, access_token_secret = '3214835117-OXVVLYeqUScRAPMqfVw5hS8NI63zPnWOVK63C5I', 'ClcGnF8vW6DbvgRgjwU6YjDc9f2oxMzOvUAN8kzpsmbcL'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
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)
returnList = []
for item in media_items:
returnList.append([item[0], item[0].replace(item[1], '')])
return returnList
def newnameSubtitles(subtitles):
subtitles = json.loads(subtitles)
returnList = []
for item in subtitles:
returnList.append([item[0], item[0].replace(item[1], '.' + item[2])])
return returnList
def updateMovedStatus(episodeDict):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('UPDATE stray_episodes SET moved = 1 WHERE original is "' + episodeDict['original'] + '"')
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]
new_location = showDir + seasonFormat + episodeFormat + item[1]
os.rename(old_location, new_location)
if episode['subtitles']:
newSubtitles = newnameSubtitles(episode['subtitles'])
for item in newSubtitles:
old_location = showDir + episode['original'] + '/' + item[0]
new_location = showDir + seasonFormat + episodeFormat + item[1]
os.rename(old_location, new_location)
# shutil.rmtree(showDir + episode['original'])
if episode['trash']:
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)
api.create_favorite(episode['response_id'])
def findVerified():
episodes = unpackEpisodes()
if episodes:
for episode in episodes:
createFolders(episode)
moveFiles(episode)
if __name__ == '__main__':
findVerified()

57
modules/pasteee.py Normal file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
pasteee module
Allows pasting to https://paste.ee
https://github.com/i-ghost/pasteee
"""
# 2 <-> 3
from urllib.request import urlopen
from urllib.request import Request as urlrequest
from urllib.parse import urlencode
from urllib import error as urlerror
import json
class PasteError(Exception):
"""Exception class for this module"""
pass
class Paste(object):
def __new__(cls, paste,
private=True, lang="plain",
key="public", desc="",
expire=0, views=0, encrypted=False):
if not paste:
raise PasteError("No paste provided")
if expire and views:
# API incorrectly returns success so we raise error locally
raise PasteError("Options 'expire' and 'views' are mutually exclusive")
request = urlrequest(
"http://paste.ee/api",
data=urlencode(
{
'paste': paste,
'private': bool(private),
'language': lang,
'key': key,
'description': desc,
'expire': expire,
'views': views,
'encrypted': bool(encrypted),
'format': "json"
}
).encode("utf-8"),
headers={'User-Agent': 'Mozilla/5.0'}
)
try:
result = json.loads(urlopen(request).read().decode("utf-8"))
return result["paste"]
except urlerror.HTTPError:
print("Couldn't send paste")
raise
except KeyError:
raise PasteError("Invalid paste option: %s" % (result["error"]))

30
modules/readDB.py Executable file
View File

@@ -0,0 +1,30 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-03-03 22:35:38
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-03-04 11:09:09
import sqlite3
from fuzzywuzzy import process
path = "/Users/KevinMidboe/Dropbox/python/seasonedShows/shows.db"
def main():
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('SELECT show_names, date_added, date_modified FROM shows')
returnList = {}
for name, added, modified in c.fetchall():
returnList[name] = [added, modified]
while True:
query = input('Query: ')
print(process.extractOne(query, returnList.keys()))
conn.close()
main()

24
modules/removeUploader.py Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-03-04 13:47:32
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-03-04 13:53:12
import re
testFile = '/Volumes/media/tv/New Girl/New Girl Season 06/New Girl S06E18/New.Girl.S06E18.Young.Adult.1080p.WEB-DL.DD5.1.H264-[eztv]-horse.mkv'
def removeUploader(file=testFile):
match = re.search('-[a-zA-Z\[\]\-]*.[a-z]{3}', file)
if match and input('Remove uploader:\t' + match.group(0)[:-4] + ' [Y/n] ') != 'n':
uploader, ext = match.group(0).split('.')
# if ext not in subExtensions:
# file = file.replace(uploader, '')
# else:
# file = file.replace(uploader, '.eng')
file = file.replace(uploader, '')
return file
if __name__ == '__main__':
print(removeUploader())

22
modules/seasonedFolders.py Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-03-05 15:55:16
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-03-05 17:25:50
from time import sleep
from findStray import findStray
from tweetNewEpisodes import tweetNewEpisodes
from folderCreator import findVerified
def main():
while True:
findStray()
tweetNewEpisodes()
findVerified()
sleep(10)
if __name__ == '__main__':
main()

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

137
modules/tweetNewEpisodes.py Executable file
View File

@@ -0,0 +1,137 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-03-04 16:50:09
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-03-05 16:51:35
import tweepy, sqlite3
from pasteee import Paste
from pprint import pprint
dbPath = "shows.db"
consumer_key, consumer_secret = 'yvVTrxNtVsLkoHxKWxh4xvgjg', '39OW6Q8fIKDXvTPPCaEJDULcYaHC5XZ3fe7HHCGtdepBKui2jK'
access_token, access_token_secret = '3214835117-OXVVLYeqUScRAPMqfVw5hS8NI63zPnWOVK63C5I', 'ClcGnF8vW6DbvgRgjwU6YjDc9f2oxMzOvUAN8kzpsmbcL'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def unpackEpisode(episode):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
cursor = c.execute('SELECT * FROM stray_episodes')
columnNames = [description[0] for description in cursor.description]
conn.close()
episodeDict = dict.fromkeys(columnNames)
for i, key in enumerate(episodeDict.keys()):
episodeDict[key] = episode[i]
return episodeDict
def prettifyEpisode(episode):
returnString = ''
for key, value in episode.items():
returnString += key + ': ' + str(value) + '\n'
return returnString
def createPasteee(episode):
return Paste(prettifyEpisode(episode), private=False, desc="My first paste", views=10)
def tweetString(episode):
print(type(episode['episode']), episode)
tweetString = '@KevinMidboe\nAdded episode:\n' + episode['name'] + ' S' + str(episode['season'])\
+ 'E' + str(episode['episode']) + '\nDetails: '
return tweetString
# TODO What if error when tweeting, no id_str
def tweet(tweetString):
response = api.update_status(status=tweetString)
return response.id_str
def updateTweetID(episodeDict, id):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('UPDATE stray_episodes SET tweet_id = ' + id + ' WHERE original is "' + episodeDict['original'] + '"')
conn.commit()
conn.close()
def tweetEpisode(episode):
pasteee = createPasteee(episode)
tweet_id = tweet(tweetString(episode) + pasteee['raw'])
updateTweetID(episode, tweet_id)
def getLastTweets(user, count=1):
return api.user_timeline(screen_name=user,count=count)
def verifyByID(id, reponse_id):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('UPDATE stray_episodes SET verified = 1, response_id = ' + reponse_id + ' WHERE tweet_id is "' + id + '"')
conn.commit()
conn.close()
# TODO Add more parsing than confirm
def parseReply(tweet):
if b'\xf0\x9f\x91\x8d' in tweet.text.encode('utf-8'):
print('Verified!')
verifyByID(tweet.in_reply_to_status_id_str, tweet.id_str)
def getReply(tweet):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
tweetID = tweet.in_reply_to_status_id_str
c.execute('SELECT * FROM stray_episodes WHERE tweet_id is ' + tweetID + ' AND verified is 0')
row = c.fetchone()
if row:
episode = unpackEpisode(row)
conn.close()
parseReply(tweet)
conn.close()
def lookForNewEpisodes():
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('SELECT * FROM stray_episodes WHERE tweet_id is NULL')
for row in c.fetchall():
episode = unpackEpisode(row)
tweetEpisode(episode)
conn.close()
def checkForReply():
for tweet in getLastTweets('KevinMidboe', 10):
if tweet.in_reply_to_status_id_str != None:
getReply(tweet)
def tweetNewEpisodes():
lookForNewEpisodes()
checkForReply()
if __name__ == '__main__':
tweetNewEpisodes()

104
modules/twitterConversation.py Executable file
View File

@@ -0,0 +1,104 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-03-04 14:06:53
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-03-05 11:48:47
import tweepy, sqlite3
from pasteee import Paste
from pprint import pprint
dbPath = "shows.db"
consumer_key, consumer_secret = 'yvVTrxNtVsLkoHxKWxh4xvgjg', '39OW6Q8fIKDXvTPPCaEJDULcYaHC5XZ3fe7HHCGtdepBKui2jK'
access_token, access_token_secret = '3214835117-OXVVLYeqUScRAPMqfVw5hS8NI63zPnWOVK63C5I', 'ClcGnF8vW6DbvgRgjwU6YjDc9f2oxMzOvUAN8kzpsmbcL'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
apiUser = api.me()
apiUsername, apiUserID = apiUser.screen_name, apiUser.id_str
def tweetNewEpisode(episode):
createPasteee()
def unpackEpisode(episode):
episodeDict = dict.fromkeys(['original', 'full_path', 'name', 'season', 'episode',\
'media_items', 'subtitles', 'trash', 'tweet_id', 'verified'])
for i, key in enumerate(episodeDict.keys()):
episodeDict[key] = episode[i]
return episodeDict
def
def updateTweetID(episodeDict, id):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('UPDATE stray_episodes SET tweet_id = ' + id + ' WHERE original is ' + episodeDict['original'])
conn.commit()
conn.close()
def getUntweetedEpisodes():
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('SELECT * FROM stray_episodes WHERE tweet_id is NULL')
for episode in c.fetchall():
tweetNewEpisode(episode)
conn.close()
exit(0)
return episode
def lastTweet(user, count=1):
return api.user_timeline(screen_name=user,count=count)
def checkReply():
originalTweet = lastTweet('pi_midboe')[0]
originalID, lastText = originalTweet.id_str, originalTweet.text
tweets = lastTweet('KevinMidboe', 10)
for tweet in tweets:
tweetID = tweet.in_reply_to_status_id_str
if tweetID == originalID:
print(tweet.text)
def unpackEpisodes():
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('SELECT * FROM stray_episodes WHERE tweet_id IS NULL and verified IS 0')
content = c.fetchall()
conn.close()
return content
def tweet(tweetString):
if not lastTweet('pi_midboe')[0].text.startswith(tweetString):
paste = Paste(unpackEpisodes(), private=False, desc="My first paste", views=2)
tweetString += paste['raw']
response = api.update_status(status=tweetString)
print('\n', response.id_str)
def main():
episode = getUntweetedEpisodes()
print(episode)
tweet('@KevinMidboe\nAdded episode: \nNew Girl S06E16\n\nDetails: ')
# unpackEpisodes()
checkReply()
if __name__ == '__main__':
main()