Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim fileQueue As LinkedList
Type fileEntry(link As String, fileName As String, title As String)
Dim hj As HttpJob
Dim gitHubLink As String = "https://gist.github.com/bishboria/8326b17bbd652f34566a"
Dim htmlString As String
Dim count As Int = 0
Dim LS As String = GetSystemProperty("line.separator", CRLF)
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
File.MakeDir(File.DirApp, "books")
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
'MainForm.Show
hj.Initialize("hj", Me)
hj.Tag = "htmlStringDL"
hj.Download(gitHubLink)
' ParseAndFillFileQueueFromProblemList
' hj.Tag = "files"
' Dim tfe As fileEntry = fileQueue.Get(0)
' hj.Download(tfe.link)
End Sub
Sub JobDone(Job As HttpJob)
If Job.Success Then
If Job.Tag=="htmlStringDL" Then
htmlString = Job.GetString
Log("Parsing github link")
ParseHTMLAndFillFileQueue
Else
Dim thisFE As fileEntry = fileQueue.Get(0)
Try
Dim newFileName As String = thisFE.title.Replace("""", "").Replace("?", "").Replace("*", "_star").Replace("+", "_plus") _
.Replace(" ", "_").Replace(",", "").Replace(":","") & "_" & thisFE.fileName
Dim inpstr As InputStream = Job.GetInputStream
Dim outstr As OutputStream = File.OpenOutput(File.DirApp & "/books", newFileName , False)
File.Copy2(inpstr, outstr)
outstr.Flush
outstr.Close
Catch
Log(LastException.Message)
Dim tw As TextWriter
tw.Initialize(File.OpenOutput(File.DirApp, "ProblemList.txt", True))
tw.Write(thisFE.link & ";" & thisFE.fileName & ";" & thisFE.title & LS)
tw.Flush
tw.Close
End Try
fileQueue.RemoveFirst
count = count + 1
Log("count: " & count & " fileQueue.Size: " & fileQueue.Size)
End If
Else
Log("Failed Download")
fileQueue.RemoveFirst
End If
hj.Tag = "files"
If fileQueue.Size>0 Then
Dim nextFile As fileEntry = fileQueue.Get(0)
hj.Download(nextFile.link)
Else
Log("Done")
End If
Job.Release
End Sub
Sub ParseHTMLAndFillFileQueue
fileQueue.Initialize
fileQueue.Clear
Dim ahrefOpen As String = "<a href="""
Dim ahrefClose As String = """>"
Dim aclose As String = "</a>"
Dim index As Int = htmlString.IndexOf2(ahrefOpen, 0)
Dim tw As TextWriter
tw.Initialize(File.OpenOutput(File.DirApp, "FEList.txt", True))
Do While index>-1
Dim thisFE As fileEntry
thisFE.Initialize
index = index + ahrefOpen.Length
thisFE.link = htmlString.SubString2(index, htmlString.IndexOf2(ahrefClose, index))
' Log("link: " & thisFE.link)
If thisFE.link.EndsWith(".pdf") Then
thisFE.fileName = thisFE.link.SubString(thisFE.link.LastIndexOf("/")+1)
' Log("fileName: " & thisFE.fileName)
index = htmlString.IndexOf2(ahrefClose, index) + ahrefClose.Length
thisFE.title = htmlString.SubString2(index, htmlString.IndexOf2(aclose, index))
' Log("title: " & thisFE.title)
index = htmlString.IndexOf2(aclose, index)
' If thisFE.title.Contains(":") Then
fileQueue.Add(thisFE)
tw.Write(thisFE.link & ";" & thisFE.fileName & ";" & thisFE.title & LS)
tw.Flush
' End If
' Log(fileQueue.Size)
End If
index = htmlString.IndexOf2(ahrefOpen, index)
Loop
tw.Close
Log("Done filling fileQueue, size: " & fileQueue.Size)
End Sub
Sub ParseAndFillFileQueueFromProblemList
Dim strList As List = File.ReadList(File.DirApp, "ProblemList.txt")
fileQueue.Initialize
fileQueue.Clear
For Each str As String In strList
Dim thisFE As fileEntry
thisFE.Initialize
thisFE.link = Regex.Split(";", str)(0)
thisFE.fileName = Regex.Split(";", str)(1)
thisFE.title = Regex.Split(";", str)(2)
fileQueue.Add(thisFE)
Next
Log("Done filling fileQueue, size: " & fileQueue.Size)
End Sub