Moved all python related scripts to app directory

This commit is contained in:
2017-08-14 22:42:48 +02:00
parent d2bed84ecb
commit ea97a0b4af
17 changed files with 0 additions and 0 deletions

26
app/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()

16
app/modules/createPasteee.py Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-02-23 21:41:40
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-03-05 19:35:10
from pasteee import Paste
def createPasteee():
paste = Paste('Test pastee', views=10)
print(paste)
print(paste['raw'])
if __name__ == '__main__':
createPasteee()

23
app/modules/dirHash.py Executable file
View File

@@ -0,0 +1,23 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2017-04-05 15:24:17
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-04-05 18:22:13
import os, hashlib
from functools import reduce
hashDir = '/Volumes/media/tv'
def main():
dirList = os.listdir(hashDir)
concat = reduce(lambda x, y: x + y, dirList, "")
m = hashlib.md5()
m.update(bytes(concat, 'utf-16be'))
return m.digest()
if __name__ == '__main__':
print(main())
# TODO The hash value should be saved in a global manner

125
app/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
app/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
app/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
app/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
app/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()

12
app/modules/subLangfinder.py Executable file
View File

@@ -0,0 +1,12 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from langdetect import detect
def main():
f = open('/Volumes/media/movies/The Man from UNCLE (2015)/The.Man.from.U.N.C.L.E.2015.1080p.nl.srt', 'r', encoding = "ISO-8859-15")
print(detect(f.read()))
f.close()
print(f.close())
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
app/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()

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()