Skip to content

Commit

Permalink
adding updated files
Browse files Browse the repository at this point in the history
adding files to the branch
  • Loading branch information
gandhishripala authored Jul 14, 2019
1 parent 0bddaa2 commit ed51bed
Show file tree
Hide file tree
Showing 32 changed files with 6,477 additions and 0 deletions.
25 changes: 25 additions & 0 deletions CustomisedAudioRecording.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from SpecializeFileOperations import SpecializeFileOperations
from Settings import Settings
from Functions import Functions

class CustomisedAudioRecording():
def __init__(self, pathSuffix):
self.LocalMainPath = Settings.GetValue(pathSuffix + "LocalMainPath")
self.GDriveMainPath = Settings.GetValue(pathSuffix + "GDriveMainPath")
self.CurrentRecordings = "Current Recordings"
self.GDriveRemovePath = Settings.GetValue(pathSuffix + "GDriveRemovePath")
self.LocalBackupPath = Settings.GetValue(pathSuffix + "LocalBackupPath")
self.GDriveBackupPath = Settings.GetValue(pathSuffix + "GDriveBackupPath")
self.SourceFiles = Functions.ScanAllFiles(self.LocalMainPath, False)

def PerformOperations(self):
organiseData = SpecializeFileOperations()
organiseSpecializeFolder = Functions.GetFolderPath(self.GDriveMainPath, self.CurrentRecordings)
if organiseSpecializeFolder:
organiseData.PerformSpecializeOperations(self.SourceFiles, self.LocalMainPath, organiseSpecializeFolder, self.GDriveRemovePath)
organiseData.ModifyFilesDataToOrganise(self.SourceFiles)
organiseData.OrganiseFiles(self.LocalMainPath, self.LocalBackupPath)
organiseData.OrganiseFiles(self.LocalMainPath, self.GDriveBackupPath)

def GetFiles(self):
return self.SourceFiles
72 changes: 72 additions & 0 deletions CustomisedDatabase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import sqlite3

from Settings import Settings

class CustomisedDatabase():
def __init__(self):
connection = sqlite3.connect(Settings.GetValue("DatabasePath"))
self.connection = connection

def InsertVideos(self, insertionData):
if len(insertionData) > 0:
cursor = self.connection.cursor()
sortedData = sorted(insertionData, key = lambda current : current[1])
for (insertId, insertDateTime) in sortedData:
cursor.execute('''INSERT OR IGNORE INTO VideoRecordingData (VideoYoutubeId, VideoRecordingDate, VideoDataModified) VALUES (?,?,?)''', (str(insertId),str(insertDateTime),"Y"))
self.connection.commit()

def GetVideosToOrganise(self):
cursor = self.connection.cursor()
cursor.execute('''SELECT VideoYoutubeId, VideoRecordingDate FROM VideoRecordingData WHERE VideoShastraName is null OR VideoShastraName = '' ''')
returnDate = [(fetch_data[0], fetch_data[1]) for fetch_data in cursor]
sortedData = sorted(returnDate, key = lambda current : current[1])
return sortedData

def UpdateShastraName(self, videoId, videoShastraName):
cursor = self.connection.cursor()
cursor.execute('''UPDATE VideoRecordingData SET VideoShastraName = ?, VideoDataModified = ? WHERE VideoYoutubeId = ?''', (str(videoShastraName), "Y", str(videoId)))
self.connection.commit()

def UpdateInternalReferences(self):
cursor = self.connection.cursor()
cursor.execute('''SELECT VideoId FROM VideoRecordingData WHERE VideoDataModified = ? ''', ("Y",))
videoIdToProcess = [fetch_data[0] for fetch_data in cursor]
for currentVideoId in videoIdToProcess:
cursor = self.connection.cursor()
cursor.execute('''SELECT VideoShastraName, VideoPrevId, VideoNextId, VideoYoutubeId FROM VideoRecordingData WHERE VideoId = ?''', (str(currentVideoId),))
fetchRecord = cursor.fetchone()
VideoShastraName, VideoPrevId, VideoNextId, VideoYoutubeId = str(fetchRecord[0]), fetchRecord[1], fetchRecord[2], fetchRecord[3]
#VideoPrevId
cursor = self.connection.cursor()
cursor.execute('''SELECT VideoYoutubeId, VideoId FROM VideoRecordingData WHERE VideoId < ? AND VideoShastraName = ? ORDER BY VideoId DESC''', (currentVideoId, VideoShastraName))
fetchRecord = cursor.fetchone()
if fetchRecord:
PreviousVideoYoutubeId, PreviousVideoId = fetchRecord[0], fetchRecord[1]
cursor = self.connection.cursor()
cursor.execute('''UPDATE VideoRecordingData SET VideoPrevId = ?, VideoDataModified = ? WHERE VideoId = ?''', (str(PreviousVideoYoutubeId), "Y", str(currentVideoId)))
cursor = self.connection.cursor()
cursor.execute('''UPDATE VideoRecordingData SET VideoNextId = ?, VideoDataModified = ? WHERE VideoId = ?''', (str(VideoYoutubeId), "Y", str(PreviousVideoId)))
#VideoNextId:
cursor = self.connection.cursor()
cursor.execute('''SELECT VideoYoutubeId, VideoId FROM VideoRecordingData WHERE VideoId > ? AND VideoShastraName = ? ORDER BY VideoId''', (currentVideoId, VideoShastraName))
fetchRecord = cursor.fetchone()
if fetchRecord:
NextVideoYoutubeId, NextVideoId = fetchRecord[0], fetchRecord[1]
cursor = self.connection.cursor()
cursor.execute('''UPDATE VideoRecordingData SET VideoNextId = ?, VideoDataModified = ? WHERE VideoId = ?''', (str(NextVideoYoutubeId), "Y", str(currentVideoId)))
cursor = self.connection.cursor()
cursor.execute('''UPDATE VideoRecordingData SET VideoPrevId = ?, VideoDataModified = ? WHERE VideoId = ?''', (str(VideoYoutubeId), "Y", str(NextVideoId)))
self.connection.commit()

def FetchDataToUpdate(self):
cursor = self.connection.cursor()
cursor.execute('''SELECT VideoId, VideoYoutubeId, VideoRecordingDate, VideoShastraName, VideoPrevId, VideoNextId FROM VideoRecordingData WHERE VideoDataModified = "Y" ''')
for fetch_data in cursor:
self.updateVideoId = fetch_data[0]
yield (fetch_data[0], fetch_data[1], fetch_data[2], fetch_data[3], fetch_data[4], fetch_data[5])

def SetUpdateCompleted(self, updateVideoId = None):
updateVideoId = self.updateVideoId if updateVideoId is None else updateVideoId
cursor = self.connection.cursor()
cursor.execute('''UPDATE VideoRecordingData SET VideoDataModified = ? WHERE VideoId = ?''', ("", updateVideoId))
self.connection.commit()
22 changes: 22 additions & 0 deletions CustomisedFileOperations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from os import path, sep, makedirs
from shutil import copy2 as copy

from Functions import Functions

class CustomisedFileOperations():
def ModifyFilesDataToOrganise(self, allFiles):
allFilesToOrganise = []
[allFilesToOrganise.append((files, Functions.GenerateFileOrganisePath(files))) for files in allFiles]
self.fileData = allFilesToOrganise

def OrganiseFiles(self, sourcePath, destinationPath):
organiseFiles = [(sourceFile, destFile) for (sourceFile, destFile) in self.fileData if not path.exists(path.join(destinationPath, destFile))]
for (sourceFile, destFilePath) in organiseFiles:
completeSourceFilePath = path.join(sourcePath, sourceFile)
completeDestinationFilePath = path.join(destinationPath, destFilePath)
destFileData = destFilePath.split(sep)
destFileName = destFileData.pop()
destFolderPath = path.join(destinationPath, sep.join(destFileData))
if not path.exists(destFolderPath):
makedirs(destFolderPath)
copy(completeSourceFilePath, completeDestinationFilePath)
27 changes: 27 additions & 0 deletions CustomisedWebDriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait

class CustomisedWebDriver(webdriver.Chrome):
def SendKeys(self, inputData):
self.currentElement.clear()
self.currentElement.send_keys(inputData)
self.currentElement.send_keys(Keys.RETURN)

def Click(self):
self.currentElement.click()

def LocateByPath(self, locatorString):
webWait = WebDriverWait(self, 60)
element = webWait.until(expected_conditions.element_to_be_clickable((By.XPATH, locatorString)))
self.currentElement = element

def LaunchURL(self, urlToLaunch):
#self.set_window_position(-10000,0)
self.implicitly_wait(20)
self.get(urlToLaunch)

def GetAuthCode(self):
return self.find_element_by_tag_name("textarea").text
139 changes: 139 additions & 0 deletions CustomisedYoutubeService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#C:\Python27\Lib\site-packages\google_auth_oauthlib - Changes in flow.py
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

import datetime
import json

from CustomisedWebDriver import CustomisedWebDriver
from Settings import Settings

class CustomisedYoutubeService():
SCOPES = ['https://www.googleapis.com/auth/youtube']
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'
YOUTUBEVIDEOSUFFIX = "https://www.youtube.com/watch?v="

def __init__(self):
client_config = json.loads(Settings.GetValue("YoutubeSecret"))
appFlow = InstalledAppFlow.from_client_config(client_config, CustomisedYoutubeService.SCOPES)
urlForAuth = appFlow.run_console()
authCode = CustomisedYoutubeService.GetAuthenticationCode(urlForAuth)
credentialsForAuth = appFlow.run_console_rest(authCode)
self.youtubeService = build(CustomisedYoutubeService.API_SERVICE_NAME, CustomisedYoutubeService.API_VERSION, credentials = credentialsForAuth)

@staticmethod
def GetAuthenticationCode(authUrl):
webDriver = CustomisedWebDriver()
webDriver.LaunchURL(authUrl)
webDriver.LocateByPath("//input[@type='email']")
webDriver.SendKeys(Settings.GetValue("GUsername"))
webDriver.LocateByPath("//input[@type='password']")
webDriver.SendKeys(Settings.GetValue("GPassword"))
webDriver.LocateByPath("//*[contains(text(), '" + Settings.GetValue("YTChannelName") + "')]")
webDriver.Click()
webDriver.LocateByPath("//a[contains(text(), 'Advanced')]")
webDriver.Click()
webDriver.LocateByPath("//a[contains(text(), 'Go to python test project (unsafe)')]")
webDriver.Click()
webDriver.LocateByPath("//span[contains(text(), 'Allow')]")
webDriver.Click()
code = webDriver.GetAuthCode()
webDriver.quit()
return code

@staticmethod
def ConvertToISTTime(dateTimeString):
parsedDateTime = datetime.datetime.strptime(dateTimeString, "%Y-%m-%dT%H:%M:%S.%fZ")
parsedDateTime = parsedDateTime + datetime.timedelta(hours=5,minutes=30)
return parsedDateTime.strftime("%Y%m%d %H%M%S")

def UpdateVideoInformation(self, videoId = None, videoTitle = None, videoDescription = None, videoRecordingDate = None):
videoId = self.videoYoutubeId if videoId is None else videoId
videoTitle = self.videoTitle if videoTitle is None else videoTitle
videoDescription = self.videoDescription if videoDescription is None else videoDescription
videoRecordingDate = self.videoRecordingDate if videoRecordingDate is None else videoRecordingDate
queryReturnParts = "snippet,recordingDetails"
videoToUpdate = self.youtubeService.videos().list(
id = videoId,
part = queryReturnParts
).execute()
if not videoToUpdate[u"items"]:
return False
videoSnippet = videoToUpdate[u"items"][0][u"snippet"]
videoRecordingDetails = dict()
if videoTitle:
videoSnippet[u"title"] = videoTitle
if videoDescription:
videoSnippet[u"description"] = videoDescription
if videoRecordingDate:
videoRecordingDetails[u"recordingDate"] = videoRecordingDate
if u"tags" not in videoSnippet:
videoSnippet[u"tags"] = []
videos_update_response = self.youtubeService.videos().update(
part = queryReturnParts,
body = dict(
snippet = videoSnippet,
recordingDetails = videoRecordingDetails,
id = videoId)
).execute()
print(videoId)
print(videoTitle)
print(videoDescription)
print("----------------")
return True

def GetVideoIDs(self, searchString):
queryReturnParts = "id,snippet"
orderString = "date"
queryString = searchString
nextPageToken = ""
responseData = []
while True:
response = self.youtubeService.search().list(
part = queryReturnParts,
channelId = Settings.GetValue("YTChannelID"),
order = orderString,
q = queryString,
pageToken = nextPageToken
).execute()
for currentResponseItems in response["items"]:
if u"videoId" in currentResponseItems[u"id"].keys():
currentVideoId = currentResponseItems[u"id"][u"videoId"]
responseData.append(currentVideoId)
if u"nextPageToken" in response.keys():
nextPageToken = response[u"nextPageToken"]
else:
break
return responseData

def GetVideoStartTimeDetails(self, inputList):
queryReturnParts = "id,liveStreamingDetails"
idToFetch = ",".join(inputList)
responseData = []
nextPageToken = ""
while True:
response = self.youtubeService.videos().list(
part = queryReturnParts,
id = idToFetch,
pageToken = nextPageToken
).execute()
for currentResponseItems in response["items"]:
responseData.append((str(currentResponseItems[u"id"]), (CustomisedYoutubeService.ConvertToISTTime(currentResponseItems[u"liveStreamingDetails"][u"actualStartTime"]))))
if u"nextPageToken" in response.keys():
nextPageToken = response[u"nextPageToken"]
else:
break
return responseData

def SetVideoDetails(self, dataTuple, videoType):
VideoId = videoType + str(dataTuple[0])
VideoRecordingDate = datetime.datetime.strptime(dataTuple[2], "%Y%m%d %H%M%S").strftime("%d %B %Y")
VideoShastraNameTitle = dataTuple[3] if dataTuple[3] is not None else "Vanchan"
VideoShastraNameDesc = dataTuple[3] if dataTuple[3] is not None else "TBD"
VideoPrevId = "TBD" if not dataTuple[4] else str(CustomisedYoutubeService.YOUTUBEVIDEOSUFFIX + dataTuple[4])
VideoNextId = "TBD" if not dataTuple[5] else str(CustomisedYoutubeService.YOUTUBEVIDEOSUFFIX + dataTuple[5])
self.videoYoutubeId = dataTuple[1]
self.videoTitle = VideoShastraNameTitle + " || " + VideoRecordingDate + " || " + VideoId + " || Live Stream"
self.videoDescription = "Shree Adinath Digambar Jain Mandir, Rajkot\nLive Stream\nPrevious Video Link: " + VideoPrevId + "\nShastra: " + VideoShastraNameDesc + "\nRecording Date: " + VideoRecordingDate + "\nRecording Number: " + VideoId + "\nNext Video Link: " + VideoNextId
self.videoRecordingDate = datetime.datetime.strptime(dataTuple[2], "%Y%m%d %H%M%S").strftime("%Y-%m-%dT%H:%M:%S.%fZ")
3 changes: 3 additions & 0 deletions Execute.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
python Program.py
timeout 15
71 changes: 71 additions & 0 deletions Functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from os import walk, path, sep
import datetime

class Functions():
@staticmethod
def GetFolderPath(directoryPath, specializeString):
specializeFolderPath = None
for (dirpath, dirnames, filenames) in walk(directoryPath):
for dir in dirnames:
if dir.startswith(specializeString):
specializeFolderPath = path.join(dirpath, dir)
return specializeFolderPath

@staticmethod
def DeleteEmptyFolders(destinationPath):
for (dirpath, dirnames, filenames) in walk(destinationPath):
if dirpath == destinationPath:
continue
if len(filenames) == 1 and filenames[0] == "desktop.ini":
print("DeleteEmptyFolders1")
try:
remove(path.join(dirpath, filenames[0]))
rmdir(dirpath)
except OSError:
continue
if len(filenames) == 0:
print("DeleteEmptyFolders0")
try:
rmdir(dirpath)
except OSError:
continue

@staticmethod
def ScanAllFiles(directoryPath, includeFolder = True):
files = []
for (dirpath, dirnames, filenames) in walk(directoryPath):
directory_path_relative = path.relpath(dirpath, directoryPath)
if includeFolder:
[files.append(path.join(directory_path_relative, dir)) for dir in dirnames]
[files.append(path.join(directory_path_relative, file)) for file in filenames if file != "desktop.ini"]
return files

@staticmethod
def GenerateFileOrganisePath(fileRelativePath):
monthFolder = { 1 : '01 JAN', 2 : '02 FEB', 3 : '03 MAR', 4 : '04 APR', 5 : '05 MAY', 6 : '06 JUN', 7 : '07 JUL', 8 : '08 AUG', 9 : '09 SEP', 10 : '10 OCT', 11 : '11 NOV', 12 : '12 DEC' }
fileData = fileRelativePath.split(sep)
folderShastraName, fileName = fileData[0], fileData[1]
fileRecDate = datetime.datetime.strptime(fileName[4:12],'%Y%m%d').date()
folderYear = str(fileRecDate.year) + '-01 To ' + str(fileRecDate.year) + '-12'
folderMon = monthFolder.get(fileRecDate.month, '00 MON') + '-' + str(fileRecDate.year)
return path.join(folderYear, folderMon, folderShastraName, fileName)

@staticmethod
def GetShastraName(videoStartDate, filesData):
videoDate = videoStartDate.split(' ')
organise_file_date = videoDate[0]
organise_file_time = int(videoDate[1])
probable_source_file = []
for source_file in filesData:
if '.mp3' not in source_file:
continue
source_file_name = source_file.split(sep)[-1]
source_file_date = source_file_name[4:12]
source_file_time = int(source_file_name[13:19])
if((organise_file_date == source_file_date) and (abs(source_file_time - organise_file_time) < (5 * 60))):
probable_source_file.append(source_file)
if len(probable_source_file) == 1:
source_file_path = probable_source_file[0]
if not source_file_path.find(sep) == -1:
return source_file_path[:source_file_path.index(sep)]
return None
Loading

0 comments on commit ed51bed

Please sign in to comment.