Skip to content

Commit

Permalink
Multimedia section #2
Browse files Browse the repository at this point in the history
  • Loading branch information
darioragusa committed May 15, 2022
1 parent 9d94c90 commit 82c0aa4
Show file tree
Hide file tree
Showing 12 changed files with 423 additions and 88 deletions.
74 changes: 63 additions & 11 deletions JWLibrary-VideosAdder/AddVideos.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ Imports Devart.Data.SQLite
Imports System.IO

Module AddVideos
Sub AddImg(ByVal dbDir As String, ByVal vidList As List(Of String))
Sub AddVideo(ByVal dbDirWT As String, ByVal docID As Integer, ByVal dbDir As String, ByVal vidList As List(Of String))
If Not System.IO.File.Exists(dbDir) Then Return
Using SQLCon As New SQLiteConnection(String.Format("Data Source = {0}", dbDir))
Dim insertMediaKeyQuery As String = "INSERT INTO MediaKey(KeySymbol, MediaType, DocumentId, MepsLanguageIndex, IssueTagNumber, Track, BookNumber) VALUES(@KeySymbol, @MediaType, @DocumentId, @MepsLanguageIndex, @IssueTagNumber, @Track, @BookNumber)"
Dim trackList As List(Of Integer) = New List(Of Integer)
Dim insertMediaKeyQuery As String = "INSERT INTO MediaKey(MediaKeyId, KeySymbol, MediaType, DocumentId, MepsLanguageIndex, IssueTagNumber, Track, BookNumber) VALUES(@MediaKeyId, @KeySymbol, @MediaType, @DocumentId, @MepsLanguageIndex, @IssueTagNumber, @Track, @BookNumber)"
Dim insertVideoQuery As String = "INSERT INTO Video(MediaKeyId, Title, Version, MimeType, BitRate, FrameRate, Duration, Checksum, FileSize, FrameHeight, FrameWidth, Label, Subtitled, DownloadUrl, FilePath, Source) VALUES(@MediaKeyId, @Title, @Version, @MimeType, @BitRate, @FrameRate, @Duration, @Checksum, @FileSize, @FrameHeight, @FrameWidth, @Label, @Subtitled, @DownloadUrl, @FilePath, @Source)"
SQLCon.Open()
For Each Video In vidList
Dim NewID = GetLastMediaKeyID(dbDir) + 1
Dim mediaKeyCMD As New SQLiteCommand(insertMediaKeyQuery, SQLCon)
Dim videoCMD As New SQLiteCommand(insertVideoQuery, SQLCon)
mediaKeyCMD.Parameters.AddWithValue("@KeySymbol", "custom")
mediaKeyCMD.Parameters.AddWithValue("@MediaKeyId", NewID)
mediaKeyCMD.Parameters.AddWithValue("@KeySymbol", "sjjm") ' Fake song
mediaKeyCMD.Parameters.AddWithValue("@MediaType", "1")
mediaKeyCMD.Parameters.AddWithValue("@DocumentId", "0")
mediaKeyCMD.Parameters.AddWithValue("@MepsLanguageIndex", "4")
mediaKeyCMD.Parameters.AddWithValue("@IssueTagNumber", Format(Now, "yyyyMMdd"))
mediaKeyCMD.Parameters.AddWithValue("@Track", Format(Now, "hhmmss"))
mediaKeyCMD.Parameters.AddWithValue("@IssueTagNumber", 0)
mediaKeyCMD.Parameters.AddWithValue("@Track", NewID + 1000)
mediaKeyCMD.Parameters.AddWithValue("@BookNumber", "0")
mediaKeyCMD.ExecuteNonQuery()
videoCMD.Parameters.AddWithValue("@MediaKeyId", CStr(GetLastMediaKeyID(dbDir)))
trackList.Add(NewID + 1000)
videoCMD.Parameters.AddWithValue("@MediaKeyId", NewID)
videoCMD.Parameters.AddWithValue("@Title", Video.Split("\").Last.Replace(".mp4", "").Replace(".MP4", ""))
videoCMD.Parameters.AddWithValue("@Version", "1")
videoCMD.Parameters.AddWithValue("@MimeType", "video/mp4")
Expand All @@ -33,21 +37,21 @@ Module AddVideos
videoCMD.Parameters.AddWithValue("@FrameWidth", "1280") 'No matter
videoCMD.Parameters.AddWithValue("@Label", "720p") 'No matter
videoCMD.Parameters.AddWithValue("@Subtitled", "0")
videoCMD.Parameters.AddWithValue("@DownloadUrl", "https://github.com/Miaosi001") 'No matter
videoCMD.Parameters.AddWithValue("@DownloadUrl", "https://github.com/darioragusa") 'No matter
videoCMD.Parameters.AddWithValue("@FilePath", Environment.GetFolderPath(Environment.SpecialFolder.MyVideos) & "\JWLibrary\" & Video.ToString.Split("\").Last)
videoCMD.Parameters.AddWithValue("@Source", "0")
videoCMD.ExecuteNonQuery()
Next
_End:
SQLCon.Dispose() : SQLCon.Close() : GC.Collect() : GC.WaitForPendingFinalizers()
AddVideoToWT(dbDirWT, docID, trackList)
End Using
End Sub

Function GetLastMediaKeyID(ByVal dbDir As String) As Integer
If Not System.IO.File.Exists(dbDir) Then Return -1
Dim lastID As Integer = 0
Using sqlCon As New SQLiteConnection(String.Format("Data Source = {0}", dbDir))
Dim readIdQuery As String = "SELECT MediaKeyId FROM MediaKey"
Dim readIdQuery As String = "SELECT MAX(MediaKeyId) FROM MediaKey"
sqlCon.Open()
Using command = sqlCon.CreateCommand()
command.CommandText = readIdQuery
Expand All @@ -64,8 +68,7 @@ _End:
Function GetDuration(ByVal MovieFullPath As String) As String
If File.Exists(MovieFullPath) Then
Dim objShell As Object = CreateObject("Shell.Application")
Dim objFolder As Object = _
objShell.Namespace(Path.GetDirectoryName(MovieFullPath))
Dim objFolder As Object = objShell.Namespace(Path.GetDirectoryName(MovieFullPath))
For Each strFileName In objFolder.Items
If strFileName.Name = Path.GetFileName(MovieFullPath) Then
Dim duration = objFolder.GetDetailsOf(strFileName, 27).ToString
Expand All @@ -83,4 +86,53 @@ _End:
Return ""
End If
End Function

Sub AddVideoToWT(ByVal dbDir As String, ByVal docID As Integer, ByVal trackList As List(Of Integer))
If Not System.IO.File.Exists(dbDir) Then Return
Using SQLCon As New SQLiteConnection(String.Format("Data Source = {0}", dbDir))
Dim insertMultimediaQuery As String = "INSERT INTO Multimedia(MultimediaId, DataType, MajorType, MinorType, MimeType, KeySymbol, Track, CategoryType, MepsLanguageIndex) VALUES(@MultimediaId, @Data, @Major, @Minor, @Mime, @KeySymbol, @Track, @Category, @Meps)"
Dim insertDocumentMultimediaQuery As String = "INSERT INTO DocumentMultimedia(DocumentId, MultimediaId) VALUES(@Document, @Multimedia)"
Dim newID As Integer = GetLastMultimediaID(dbDir) + 1
If newID <> -1 Then
SQLCon.Open()
For Each track In trackList
Dim multimediaCMD As New SQLiteCommand(insertMultimediaQuery, SQLCon)
Dim documentMultimediaCMD As New SQLiteCommand(insertDocumentMultimediaQuery, SQLCon)
multimediaCMD.Parameters.AddWithValue("@MultimediaId", newID)
multimediaCMD.Parameters.AddWithValue("@Data", 2)
multimediaCMD.Parameters.AddWithValue("@Major", 2)
multimediaCMD.Parameters.AddWithValue("@Minor", 3)
multimediaCMD.Parameters.AddWithValue("@Mime", "video/mp4")
multimediaCMD.Parameters.AddWithValue("@KeySymbol", "sjjm")
multimediaCMD.Parameters.AddWithValue("@Track", track)
multimediaCMD.Parameters.AddWithValue("@Category", -1)
multimediaCMD.Parameters.AddWithValue("@Meps", 4)
multimediaCMD.ExecuteNonQuery()
documentMultimediaCMD.Parameters.AddWithValue("@Document", CStr(docID))
documentMultimediaCMD.Parameters.AddWithValue("@Multimedia", newID)
documentMultimediaCMD.ExecuteNonQuery()
newID += 1
Next
End If
SQLCon.Dispose() : SQLCon.Close() : GC.Collect() : GC.WaitForPendingFinalizers()
End Using
End Sub

Function GetLastMultimediaID(ByVal dbDir As String) As Integer
If Not System.IO.File.Exists(dbDir) Then Return -1
Dim lastID As Integer = -1
Using sqlCon As New SQLiteConnection(String.Format("Data Source = {0}", dbDir))
Dim readWeekQuery As String = "SELECT Max(MultimediaId) FROM Multimedia"
sqlCon.Open()
Using command = sqlCon.CreateCommand()
command.CommandText = readWeekQuery
Using reader = command.ExecuteReader()
While reader.Read()
lastID = reader.GetInt32(0)
End While
End Using
End Using
sqlCon.Dispose() : sqlCon.Close() : GC.Collect() : GC.WaitForPendingFinalizers()
End Using : Return lastID
End Function
End Module
83 changes: 83 additions & 0 deletions JWLibrary-VideosAdder/FindWatchtower.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Imports System.IO
Imports Devart.Data
Imports Devart.Data.SQLite

Module FindWatchtower
Function findWatchtowerDirectory() As String
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\Packages"
If IO.Directory.Exists(path) Then
For Each Dir As String In Directory.GetDirectories(path)
If LCase(Dir).Contains("watchtower") Then
path = Dir & "\LocalState\Publications"
Exit For
End If
Next
End If
If IO.Directory.Exists(path) Then Return path Else Return ""
End Function

Function existWhatchtowerDirectory() As Boolean
If findWatchtowerDirectory() <> "" Then Return True Else Return False
End Function

Function findCurrentWatchtowers() As List(Of String) 'I used an array in case there were multiple languages
Dim wtPath As String = findWatchtowerDirectory()
Dim collectionPath As String = wtPath.Replace("\Publications", "\Data\pub_collections.db")
Dim pubArray As New List(Of publication)
If System.IO.File.Exists(collectionPath) Then
Using sqlCon As New SQLiteConnection(String.Format("Data Source = {0}", collectionPath))
Dim query As String = "SELECT IssueTagNumber, FirstDatedTextDateOffset, LastDatedTextDateOffset, KeySymbol FROM Publication"
sqlCon.Open()
Using command = sqlCon.CreateCommand()
command.CommandText = query
Using reader = command.ExecuteReader()
While reader.Read()
Dim pub As publication = New publication
pub.tagNumber = reader.GetString(0)
pub.firstDate = reader.GetString(1)
pub.lastDate = reader.GetString(2)
pub.keySymbol = reader.GetString(3)
pubArray.Add(pub) 'Getting all publications
End While
End Using
End Using
sqlCon.Dispose() : sqlCon.Close() : GC.Collect() : GC.WaitForPendingFinalizers()
End Using
End If

Dim wtArray As New List(Of publication)
For Each pub In pubArray
If pub.keySymbol = "w" Then wtArray.Add(pub) 'Keeping only watchtowers
Next

Dim currentTagN As String = ""
For Each pub In wtArray
Dim firstDate As Date = DateTime.ParseExact(pub.firstDate, "yyyyMMdd", Nothing)
Dim lastDate As Date = DateTime.ParseExact(pub.lastDate, "yyyyMMdd", Nothing)
Dim todayDate As Date = Now.Date
If firstDate <= todayDate And todayDate <= lastDate Then 'Period checking
currentTagN = pub.tagNumber
Exit For
End If
Next

Dim wtDirArray As New List(Of String)
For Each pubDirectory In IO.Directory.EnumerateDirectories(wtPath)
Dim pubName As String = pubDirectory.ToString.Split("\").Last
If Not pubName.Contains("w_") Or currentTagN.Length < 6 Then GoTo _endFor
If pubName.StartsWith("w_") And pubName.EndsWith(currentTagN.Substring(0, 6)) Then 'Getting watchtower directories
wtDirArray.Add(pubDirectory)
End If
_endFor:
Next

Return wtDirArray
End Function
End Module

Structure publication
Dim keySymbol As String
Dim tagNumber As String
Dim firstDate As String
Dim lastDate As String
End Structure
55 changes: 55 additions & 0 deletions JWLibrary-VideosAdder/FindWeekWT.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Imports Devart.Data
Imports Devart.Data.SQLite

Module FindWeekWT
Function GetWeekN(ByVal dbDir As String) As Integer
If Not System.IO.File.Exists(dbDir) Then Return -1
Dim returnID As Integer = -1
Using sqlCon As New SQLiteConnection(String.Format("Data Source = {0}", dbDir))
Dim readWeekQuery As String = "SELECT DatedTextId, FirstDateOffset, LastDateOffset FROM DatedText"
sqlCon.Open()
Using command = sqlCon.CreateCommand()
command.CommandText = readWeekQuery
Using reader = command.ExecuteReader()
While reader.Read()
Dim id = reader.GetInt32(0)
Dim firstDate = reader.GetString(1)
Dim lastDate = reader.GetString(2)
If CheckWTWeek(firstDate, lastDate) Then returnID = id
End While
End Using
End Using
sqlCon.Dispose() : sqlCon.Close() : GC.Collect() : GC.WaitForPendingFinalizers()
End Using : Return returnID
End Function

Function CheckWTWeek(ByVal firstDate As String, ByVal lastDate As String) As Boolean
Dim startDate As Date = DateTime.ParseExact(firstDate, "yyyyMMdd", Nothing)
Dim endDate As Date = DateTime.ParseExact(lastDate, "yyyyMMdd", Nothing)
If Now.Date >= startDate And Now.Date <= endDate Then Return True Else Return False
End Function

Function GetDocumentID(ByVal dbDir As String, ByVal weekN As Integer) As Integer
If Not System.IO.File.Exists(dbDir) Then Return -1
Dim returnID As Integer = -1
Using sqlCon As New SQLiteConnection(String.Format("Data Source = {0}", dbDir))
Dim readWeekQuery As String = "SELECT DocumentId, Class FROM Document"
sqlCon.Open()
Using command = sqlCon.CreateCommand()
command.CommandText = readWeekQuery
Dim actWeekN As Integer = 1
Using reader = command.ExecuteReader()
While reader.Read()
Dim id = reader.GetInt32(0)
Dim class_ = reader.GetInt32(1)
If class_ = 40 Then
If weekN = actWeekN Then returnID = id
actWeekN += 1
End If
End While
End Using
End Using
sqlCon.Dispose() : sqlCon.Close() : GC.Collect() : GC.WaitForPendingFinalizers()
End Using : Return returnID
End Function
End Module
23 changes: 21 additions & 2 deletions JWLibrary-VideosAdder/Form1.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 82c0aa4

Please sign in to comment.