forked from Smoothtalk/ani-script-deprecrated-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synckkk.py
157 lines (131 loc) · 4.56 KB
/
synckkk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
import sys
import os
import linecache
import re
import json
import codecs
import glob
import xml.etree.ElementTree as ET
import multiprocessing
import traceback
import subprocess
from trakt.users import User
from fuzzywuzzy import fuzz
from multiprocessing import Process
from collections import OrderedDict
FUZZ_RATIO = 70
class TvShow:
def __init__(self, title, lwd, lwe):
self.title = title
self.last_watched_date = lwd
self.last_watched_episode = lwe
def getTitle(self):
return self.title
def getLast_watched_date(self):
return self.last_watched_date
def getlast_watched_episode(self):
return self.last_watched_episode
class SyncUser:
def __init__(self, user, userSettings):
self.userName = user
self.remote_port = userSettings['remote_port']
self.remote_host = userSettings['remote_host']
self.remote_download_dir = userSettings['remote_download_dir']
self.discord_ID = userSettings['discord_ID']
self.traktUserName = userSettings['traktUserName']
self.custom_titles = userSettings['custom_titles']
self.shows = [] #titles of show
def getUserName(self):
return self.userName
def getRemote_Port(self):
return self.remote_port
def getRemote_Host(self):
return self.remote_host
def getRemote_Download_Dir(self):
return self.remote_download_dir
def getDiscord_ID(self):
return self.discord_ID
def getTraktUserName(self):
return self.traktUserName
def getCustom_Titles(self):
return self.custom_titles
def addShow(self, show):
self.shows.append(show)
def getShows(self):
return self.shows
class Release:
def __init__(self, inputVar):
#title from torrent
regex = r"^.*.S\d\d"
preTitle = re.findall(regex, inputVar, re.IGNORECASE)
preTitle = str(preTitle)
preTitle = preTitle[2:-6]
regex = r"S\d\dE\d\d"
tempSandE = str(re.findall(regex, inputVar, re.IGNORECASE))
regex = r"\d\d"
seasonAndEpisodeNumbers = re.findall(regex, tempSandE)
self.title = preTitle.replace('.',' ')
self.season = seasonAndEpisodeNumbers[:1]
self.episode = seasonAndEpisodeNumbers[1:]
def getTitle(self):
return self.title
def getSeason(self):
return self.season[0]
def getEpisode(self):
return self.episode[0]
def readJson():
json_data=open("vars.json").read()
data = json.loads(json_data, object_pairs_hook=OrderedDict)
return data #an OrderedDict
def getTraktShows(syncUser):
my = User(syncUser.getTraktUserName())
for y in range(len(my.watched_shows)):
dict = my.watched_shows[y].seasons[-1]
# gets the episodes (change to -1 to get current season value
# -2 gets all teh episodes you've watched
fKey = dict.keys()[-2]
values = dict[fKey]
last_episode_watched = values[-1]
episode_Number = last_episode_watched['number']
date_Watched_At = last_episode_watched['last_watched_at']
traktShow = TvShow(my.watched_shows[y].title, date_Watched_At, episode_Number);
syncUser.addShow(traktShow)
def match(release, traktShows):
match = None
for show in traktShows:
# print release.getTitle()
if (fuzz.ratio(show.getTitle(), release.getTitle()) > FUZZ_RATIO):
match = show
return match
def sync(settings, syncingUser, match, glob, filePath):
innerFileName = ""
try:
os.chdir(filePath)
for file in glob.glob("*.mkv"):
innerFileName = file
filename = match.getTitle() + " - " + 'S' + match.getSeason() + 'E' + match.getEpisode() +".mkv"
print filename
command = "rsync --progress -v -z -e 'ssh -p" + syncingUser.getRemote_Port() + "'" + " \"" + filePath + "/" + innerFileName + "\"" + ' ' + "\"" + syncingUser.getRemote_Host() + ":" + syncingUser.getRemote_Download_Dir() + "\""
os.system(command)
command = "ssh -p" + syncingUser.getRemote_Port() + ' ' + syncingUser.getRemote_Host() + " \"mv '" + syncingUser.getRemote_Download_Dir() + innerFileName + "' '" + syncingUser.getRemote_Download_Dir() + filename + "'\""
os.system(command)
os.chdir(settings['System Settings']['script_location'])
command = "python3.5 discordAnnounce.py \'" + filename + '\' ' + syncingUser.getUserName()
process = subprocess.call(command, shell=True)
except Exception as e:
print e
sys.exit(1)
traceback.print_stack()
settings = readJson()
filePath = sys.argv[1]
syncRelease = Release(sys.argv[2])
os.chdir(settings['System Settings']['script_location'])
for user in settings['Users']:
if (settings['Users'][user]['traktUserName'] != ""):
syncingUser = SyncUser(user, settings['Users'][user])
getTraktShows(syncingUser)
match = match(syncRelease, syncingUser.getShows())
if (match is not None):
print "Syncing: " + str(match.getTitle())
sync(settings, syncingUser, syncRelease, glob, filePath)