/
Utils.vb
1255 lines (1140 loc) · 58.3 KB
/
Utils.vb
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Option Strict On
Option Explicit On
Imports System.Text.RegularExpressions
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.Text.Json
Imports MWBot.net.My.Resources
Namespace Utility
Public NotInheritable Class Utils
#Region "Properties"
Public Shared Codename As String = "Utils"
Public Shared DirSeparator As String = IO.Path.DirectorySeparatorChar
''' <summary>
''' El separador de decimales varia segun SO y configuracion regional, eso puede afectar los calculos.
''' </summary>
Public Shared DecimalSeparator As String = String.Format(CType(1.1, String)).Substring(1, 1)
Public Shared OS As String = GetOsString()
Public Shared Exepath As String = ReplaceLast(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName, Process.GetCurrentProcess().MainModule.ModuleName, "")
#End Region
#Region "Prevent init"
Private Sub New()
End Sub
#End Region
#Region "Text Functions"
Public Shared signpattern As String = "([0-9]{2}):([0-9]{2}) ([0-9]{2}|[0-9]) ([A-z]{3})([\.,])* [0-9]{4}( \([A-z]{3,4}\))*"
Public Shared Function GetJsonDocument(ByVal jsonString As String) As JsonDocument
Return JsonDocument.Parse(jsonString)
End Function
Public Shared Function GetJsonElement(ByVal jsonDocument As JsonDocument, ByVal propertyName As String) As JsonElement
Dim element As JsonElement
Dim text As String = jsonDocument.RootElement.ToString
element = jsonDocument.RootElement.GetProperty(propertyName)
Return element
End Function
Public Shared Function GetJsonElement(ByVal jsonElement As JsonElement, ByVal propertyName As String) As JsonElement
Dim text As String = jsonElement.ToString
jsonElement = jsonElement.GetProperty(propertyName)
Return jsonElement
End Function
Public Shared Function IsJsonPropertyPresent(ByVal jsonElement As JsonElement, ByVal properyName As String) As Boolean
For Each elementProperty As JsonProperty In jsonElement.EnumerateObject
If elementProperty.Name = properyName Then Return True
Next
Return False
End Function
''' <summary>
''' Crea una cadena de texto concatenado según los parámetros entregados
''' </summary>
''' <param name="array">Array de origen</param>
''' <param name="separator">Separador</param>
''' <returns></returns>
Public Shared Function ConcatenateTextArrayWithChar(ByVal array As String(), ByVal separator As Char) As String
Dim result As String = ""
For Each s As String In array
result &= s & separator
Next
Return result.Trim(separator)
End Function
''' <summary>
''' Crea una cadena de texto concatenado según los parámetros entregados y la codifica según urlencode
''' </summary>
''' <param name="array">Array de origen</param>
''' <param name="separator">Separador</param>
''' <param name="urlEncode">Codificar el texto en urlencode, se codificará aunque se establesca en 'false'</param>
''' <returns></returns>
<CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification:="Overload")>
Public Shared Function ConcatenateTextArrayWithChar(ByVal array As String(), ByVal separator As Char, ByVal urlEncode As Boolean) As String
Dim result As String = ""
For Each s As String In array
result &= s & separator
Next
Return UrlWebEncode(ReplaceLast(result, separator, ""))
End Function
''' <summary>
''' Elimina los tags html de una cadena de texto dada.
''' </summary>
''' <param name="html">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function StripTags(ByVal html As String) As String
Return Regex.Replace(html, "<.*?>", String.Empty)
End Function
''' <summary>
''' Elimina las líneas en blanco.
''' </summary>
''' <param name="Str">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function Removewhitelines(ByVal Str As String) As String
Return Regex.Replace(Str, "^\s+$[\r\n]*", "", RegexOptions.Multiline)
End Function
''' <summary>
''' Elimina los excesos de espacios (consecutivos) en una cadena de texto.
''' </summary>
''' <param name="text">Texto a evaluar</param>
''' <returns></returns>
Public Shared Function RemoveExcessOfSpaces(ByVal text As String) As String
Return Regex.Replace(text, "\s{2,}", " ")
End Function
''' <summary>
''' Cuenta las veces que un carácter aparece en una cadena de texto dada.
''' </summary>
''' <param name="value">Texto a evaluar.</param>
''' <param name="ch">Carácter a buscar.</param>
''' <returns></returns>
Public Shared Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
Return value.Count(Function(c As Char) c = ch)
End Function
''' <summary>
''' Regresa la misma cadena de texto, pero con la primera letra mayúscula.
''' </summary>
''' <param name="val">Texto a evaluar</param>
''' <returns></returns>
Public Shared Function UppercaseFirstCharacter(ByVal val As String) As String
If String.IsNullOrEmpty(val) Then
Return val
End If
Dim array() As Char = val.ToCharArray
array(0) = Char.ToUpper(array(0))
Return New String(array)
End Function
''' <summary>
''' Codifica texto para ser guardado en el LOG.
''' </summary>
''' <param name="text">Texto a codificar</param>
''' <returns></returns>
Public Shared Function PsvSafeEncode(ByVal text As String) As String
Return text.Replace("|"c, "%CHAR:U+007C%")
End Function
''' <summary>
''' Decodifica texto guardado en el LOG.
''' </summary>
''' <param name="text">Texto a decodificar.</param>
''' <returns></returns>
Public Shared Function PsvSafeDecode(ByVal text As String) As String
Return text.Replace("%CHAR:U+007C%", "|")
End Function
''' <summary>
''' Convierte el texto completo a minúsculas y luego coloca en mayúsculas la primera letra.
''' </summary>
''' <param name="text">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function NormalizeText(ByVal text As String) As String
Dim s As String = text.ToLower
Return UppercaseFirstCharacter(s)
End Function
''' <summary>
''' Verifica si una cadena de texto es numérica.
''' </summary>
''' <param name="number">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function IsNumeric(ByVal number As String) As Boolean
If Regex.IsMatch(number, "^[0-9 ]+$") Then
Return True
Else
Return False
End If
End Function
''' <summary>
''' Obtiene el último hilo que coincida con el título entregado.
''' </summary>
''' <param name="threads"></param>
''' <returns></returns>
Public Shared Function GetLastThreadByTitle(ByVal threads As String(), title As String) As String
Dim matchingthread As String = String.Empty
For Each t As String In threads
If GetTitleFromThread(t) = title Then
matchingthread = t
End If
Next
Return matchingthread
End Function
''' <summary>
''' Entrega el primer título del hilo en formato wikitexto que se le pase como parámetro. Si no tiene título entregará una cadena vacía.
''' </summary>
''' <param name="thread"></param>
''' <returns></returns>
Public Shared Function GetTitleFromThread(ByVal thread As String) As String
Dim TitlesList As New List(Of String)
Dim temptext As String = thread
Dim commentMatch As MatchCollection = Regex.Matches(temptext, "(<!--)[\s\S]*?(-->)")
Dim CommentsList As New List(Of String)
For i As Integer = 0 To commentMatch.Count - 1
CommentsList.Add(commentMatch(i).Value)
temptext = temptext.Replace(commentMatch(i).Value, ColoredText("PERIODIBOT::::COMMENTREPLACE::::" & i, 4))
Next
Dim mc As MatchCollection = Regex.Matches(temptext, "([\n\r]|^)((==(?!=)).+?(==(?!=)))")
For Each m As Match In mc
TitlesList.Add(m.Value)
Next
If TitlesList.Count > 0 Then
Return TitlesList.First
Else
Return String.Empty
End If
End Function
''' <summary>
''' Entrega únicamente los títulos de los hilos en formato wikitexto que se les pase como parámetro. Si uno de los parámetros no tiene título entregará una cadena vacía en su lugar.
''' </summary>
''' <param name="threads"></param>
''' <returns></returns>
Public Shared Function GetTitlesFromThreads(ByVal threads As String()) As String()
Dim TitlesList As New List(Of String)
For Each threadtext As String In threads
TitlesList.Add(GetTitleFromThread(threadtext))
Next
Return TitlesList.ToArray
End Function
''' <summary>
''' Entrega los elementos en el segundo array que no estén presentes en el primero
''' </summary>
''' <param name="Arr1">Array base</param>
''' <param name="arr2">Array a comparar</param>
''' <returns></returns>
Public Shared Function GetSecondArrayAddedDiff(ByVal arr1 As String(), arr2 As String()) As String()
Dim Difflist As New List(Of String)
For i As Integer = 0 To arr2.Count - 1
If Not arr1.Contains(arr2(i)) Then
Difflist.Add(arr2(i))
End If
Next
Return Difflist.ToArray
End Function
''' <summary>
''' Entrega los titulos de los hilos en el segundo array que sean distintos al primero
''' </summary>
''' <param name="threadlist1">Array base</param>
''' <param name="threadlist2">Array a comparar</param>
''' <returns></returns>
Public Shared Function GetChangedThreadsTitle(ByVal threadlist1 As String(), threadlist2 As String()) As String()
Dim Difflist As New List(Of String)
Dim thread1 As List(Of String) = threadlist1.ToList
Dim thread2 As List(Of String) = threadlist2.ToList
thread1.Sort()
thread2.Sort()
If thread1.Count = thread2.Count Then
For i As Integer = 0 To thread1.Count - 1
If Not thread1(i) = thread2(i) Then
Difflist.Add(GetTitleFromThread(thread1(i)))
End If
Next
ElseIf thread2.Count > thread1.Count - 1 Then
Difflist.AddRange(GetSecondArrayAddedDiff(GetTitlesFromThreads(thread1.ToArray), GetTitlesFromThreads(thread2.ToArray)))
End If
Return Difflist.ToArray
End Function
''' <summary>
''' Entrega los titulos de los hilos en el segundo array que sean distintos al primero. Los array deben tener los mismos elementos o se retornara un array vacio.
''' </summary>
''' <param name="threadlist1">Array base</param>
''' <param name="threadlist2">Array a comparar</param>
''' <returns></returns>
Public Shared Function GetChangedThreads(ByVal threadlist1 As String(), threadlist2 As String()) As String()
Dim Difflist As New List(Of String)
Dim thread1 As List(Of String) = threadlist1.ToList
Dim thread2 As List(Of String) = threadlist2.ToList
thread1.Sort()
thread2.Sort()
If thread1.Count = thread2.Count Then
For i As Integer = 0 To thread1.Count - 1
If Not thread1(i) = thread2(i) Then
Difflist.Add(thread2(i))
End If
Next
End If
Return Difflist.ToArray
End Function
''' <summary>
''' Entrega el último hilo en los hilos entregados que coincida con el nombre
''' </summary>
''' <param name="threads"></param>
''' <param name="title"></param>
''' <returns></returns>
Public Shared Function GetThreadByTitle(ByVal threads As String(), title As String) As String
Dim thread As String = String.Empty
For Each threadtext As String In threads
Dim currentthreadtitle As String = GetTitleFromThread(threadtext)
If title = currentthreadtitle Then
thread = currentthreadtitle
End If
Next
Return thread
End Function
''' <summary>
''' Convierte un array de tipo string numérico a integer. Si uno de los elementos no es numérico retorna 0 en su lugar.
''' </summary>
''' <param name="arr">Array a evaluar</param>
''' <returns></returns>
Public Shared Function StringArrayToInt(ByVal arr As String()) As Integer()
Dim intlist As New List(Of Integer)
For i As Integer = 0 To arr.Count - 1
If IsNumeric(arr(i)) Then
intlist.Add(Integer.Parse(arr(i)))
Else
intlist.Add(0)
End If
Next
Return intlist.ToArray
End Function
''' <summary>
''' Realiza los escapes para usar una cadena de texto dentro de una expresión regular.
''' </summary>
''' <param name="s">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function RegexParser(ByVal s As String) As String
Return Regex.Escape(s)
End Function
''' <summary>
''' Realiza los escapes para usar una cadena de texto dentro de una expresión regular, exceptuando el "pipe" (|).
''' </summary>
''' <param name="s">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function SpamListParser(ByVal s As String) As String
s = s.Replace("\", "\\")
Return s.Replace("[", "\[").Replace("/", "\/").Replace("^", "\^").Replace("$", "\$").Replace(".", "\.") _
.Replace("?", "\?").Replace("*", "\*").Replace("+", "\+").Replace("(", "\(").Replace(")", "\)") _
.Replace("{", "\{").Replace("}", "\}")
End Function
''' <summary>
''' Entrega el título de la página en la pseudoplantilla de resúmenes de página.
''' </summary>
''' <param name="SourceString">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function GetTitlesOfTemplate(ByVal SourceString As String) As String()
Return TextInBetween(SourceString, "|" & Environment.NewLine & "|", "=")
End Function
''' <summary>
''' Reeplaza la primera ocurrencia de una cadena dada en la cadena de entrada.
''' </summary>
''' <param name="text">Cadena de texto a modificar.</param>
''' <param name="search">Cadena texto a buscar.</param>
''' <param name="replace">Cadena texto de reemplazo.</param>
''' <returns></returns>
Public Shared Function ReplaceFirst(ByVal text As String, ByVal search As String, ByVal replace As String) As String
Dim pos As Integer = text.IndexOf(search)
If pos < 0 Then
Return text
End If
Return text.Substring(0, pos) & replace + text.Substring(pos + search.Length)
End Function
''' <summary>
''' Reeplaza la todas las ocurrencia de una cadena dada en la cadena de entrada menos la primera.
''' </summary>
''' <param name="text">Cadena de texto a modificar.</param>
''' <param name="search">Cadena texto a buscar.</param>
''' <param name="replace">Cadena texto de reemplazo.</param>
''' <returns></returns>
Public Shared Function ReplaceEveryOneButFirst(ByVal text As String, ByVal search As String, ByVal replace As String) As String
Dim placeholder As String = ColoredText("#$^&&*!?.::++!+_@(!@PLACEHOLDER:::PLACEHOLDER:$^&&*!?.::++!+:PLACEHOLDER::PLACEHOLDER:$^&&*!?.::++!+:PLACEHOLDER::PLACEHOLDER$^&&*!?.::++!+", 1, 1)
text = ReplaceFirst(text, search, placeholder)
text = text.Replace(search, replace)
text = text.Replace(placeholder, search)
Return text
End Function
''' <summary>
''' Reeplaza la todas las ocurrencia de una cadena dada en la cadena de entrada menos la última.
''' </summary>
''' <param name="text">Cadena de texto a modificar.</param>
''' <param name="search">Cadena texto a buscar.</param>
''' <param name="replace">Cadena texto de reemplazo.</param>
''' <returns></returns>
Public Shared Function ReplaceEveryOneButLast(ByVal text As String, ByVal search As String, ByVal replace As String) As String
Dim placeholder As String = ColoredText("#$^&&*!?.::++!+_@(!@PLACEHOLDER:::PLACEHOLDER:$^&&*!?.::++!+:PLACEHOLDER::PLACEHOLDER:$^&&*!?.::++!+:PLACEHOLDER::PLACEHOLDER$^&&*!?.::++!+", 1, 1)
text = ReplaceLast(text, search, placeholder)
text = text.Replace(search, replace)
text = text.Replace(placeholder, search)
Return text
End Function
''' <summary>
''' Reeplaza la última ocurrencia de una cadena dada en la cadena de entrada.
''' </summary>
''' <param name="text">Cadena de texto a modificar.</param>
''' <param name="search">Cadena texto a buscar.</param>
''' <param name="replace">Cadena texto de reemplazo.</param>
''' <returns></returns>
Public Shared Function ReplaceLast(ByVal text As String, ByVal search As String, ByVal replace As String) As String
Dim pos As Integer = text.LastIndexOf(search)
If pos < 0 Then
Return text
End If
Return text.Substring(0, pos) & replace & text.Substring(pos + search.Length)
End Function
''' <summary>
''' Escribe una línea en la salida del programa a modo de registro siguiendo un formato estándar
''' (en realidad es completamente arbitrario pero está ordenado y bonito :) ).
''' </summary>
''' <param name="type">Tipo de registro</param>
''' <param name="source">Origen del registro</param>
''' <param name="message">Mensaje de salida</param>
Public Shared Sub WriteLine(ByVal type As String, ByVal source As String, message As String)
Dim msgstr As String = "[" & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & "]" & " [" & source & " " & type & "] " & message
Console.WriteLine(msgstr)
End Sub
''' <summary>
''' Retorna las líneas contenidas en una cadena de texto.
''' </summary>
''' <param name="text">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function GetLines(ByVal text As String) As String()
Return GetLines(text, False)
End Function
''' <summary>
''' Retorna las líneas contenidas en una cadena de texto.
''' </summary>
''' <param name="text">Texto a evaluar.</param>
''' <param name="removeemptylines">Eliminar las líneas vacías</param>
''' <returns></returns>
Public Shared Function GetLines(ByVal text As String, ByVal removeEmptyLines As Boolean) As String()
Dim thelines As List(Of String) = text.Split({vbCrLf, vbCr, vbLf, Environment.NewLine}, StringSplitOptions.None).ToList
If removeEmptyLines Then
thelines.RemoveAll(Function(x) String.IsNullOrWhiteSpace(x))
End If
Return thelines.ToArray
End Function
''' <summary>
''' Soluciona un error de la api en los resúmenes, donde cuertas plantillas los números los entrega repetidos con varios símbolos en medio.
''' </summary>
''' <param name="text"></param>
''' <returns></returns>
Public Shared Function FixResumeNumericExp(ByVal text As String) As String
Dim newtext As String = text
For Each m As Match In Regex.Matches(text, "&+[0-9]+\.&+[0-9 ]+")
Dim num As Integer = Integer.Parse(TextInBetween(m.Value, "&&&&&&&&", ".&&&&&")(0))
Dim numsrt As String = num.ToString & " "
newtext = newtext.Replace(m.Value, numsrt)
Next
Return newtext
End Function
''' <summary>
''' Normaliza los títulos de la plantilla de resúmenes.
''' </summary>
''' <param name="SourceString">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function GetNormalizedTitlesOfTemplate(ByVal SourceString As String) As String()
Dim mlist As New List(Of String)
For Each m As Match In Regex.Matches(SourceString, "(\n\|)[\s\S]*?(=\n)")
mlist.Add(NormalizeText(m.Value.Replace("|", String.Empty).Replace("=", String.Empty).Replace(vbLf, String.Empty).Replace("_", " ")))
Next
Return mlist.ToArray
End Function
''' <summary>
''' Entrega un array con todas las coincidencias donde se encuentre un texto en medio de dos cadenas de texto
''' </summary>
''' <param name="SourceString">Texto a evaluar.</param>
''' <param name="string1">Texto a la izquierda.</param>
''' <param name="string2">Texto a la derecha.</param>
''' <returns></returns>
Public Shared Function TextInBetween(ByVal SourceString As String, string1 As String, string2 As String) As String()
Dim mlist As New List(Of String)
For Each m As Match In Regex.Matches(SourceString, "(" & RegexParser(string1) & ")[\s\S]*?(" & RegexParser(string2) & ")")
mlist.Add(m.Value.Replace(string1, String.Empty).Replace(string2, String.Empty))
Next
Return mlist.ToArray
End Function
''' <summary>
''' Similar a TextInBetween pero incluye además las las cadenas a la izquierda y derecha del texto.
''' </summary>
''' <param name="SourceString">Texto a evaluar.</param>
''' <param name="string1">Texto a la izquierda.</param>
''' <param name="string2">Texto a la derecha</param>
''' <returns></returns>
Public Shared Function TextInBetweenInclusive(ByVal SourceString As String, string1 As String, string2 As String) As String()
Dim mlist As New List(Of String)
For Each m As Match In Regex.Matches(SourceString, "(" & RegexParser(string1) & ")[\s\S]*?(" & RegexParser(string2) & ")")
mlist.Add(m.Value)
Next
Return mlist.ToArray
End Function
''' <summary>
''' Entrega un array con todos los números (integer) que estén entre comillas.
''' </summary>
''' <param name="SourceString">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function MatchQuotedIntegers(ByVal SourceString As String) As String()
Dim mlist As New List(Of String)
For Each m As Match In Regex.Matches(SourceString, "(""[0-9]+"")")
mlist.Add(m.Value.Replace("""", ""))
Next
Return mlist.ToArray
End Function
''' <summary>
''' Entrega los títulos en una cadena de texto con el formato de respuesta de la Api de wikipedia.
''' </summary>
''' <param name="sourcestring">Texto a evaluar.</param>
''' <returns></returns>
Public Shared Function GetTitlesFromQueryText(ByVal sourcestring As String) As String()
Return TextInBetween(sourcestring, ",""title"":""", """,")
End Function
''' <summary>
''' Normaliza el texto ASCII con códigos unicodes escapados con el formato \\u(número)
''' </summary>
''' <param name="stringToNormalize"></param>
''' <returns></returns>
Public Shared Function NormalizeUnicodetext(ByVal stringToNormalize As String) As String
Dim temptext As String = Regex.Replace(stringToNormalize, "\\u([\dA-Fa-f]{4})", Function(v) ChrW(Convert.ToInt32(v.Groups(1).Value, 16)))
temptext = Regex.Replace(temptext, "(?<!\\)\\n", Environment.NewLine)
temptext = Regex.Replace(temptext, "(?<!\\)\\t", ControlChars.Tab)
temptext = temptext.Replace("\""", """")
temptext = temptext.Replace("\\", "\")
temptext = temptext.Replace("\\n", "\n")
temptext = temptext.Replace("\\t", "\t")
Return temptext
End Function
''' <summary>
''' Normaliza el texto ASCII con códigos unicodes escapados con el formato \\u(número)
''' </summary>
''' <param name="stringToNormalize"></param>
''' <returns></returns>
Public Shared Function NormalizeHexScapedString(ByVal stringToNormalize As String) As String
Dim temptext As String = Regex.Replace(stringToNormalize, "\\u([\dA-Fa-f]{4})", Function(v) ChrW(Convert.ToInt32(v.Groups(1).Value, 16)))
Return temptext
End Function
''' <summary>
''' Codifica una cadena de texto en URLENCODE.
''' </summary>
''' <param name="textToEncode">Texto a codificar.</param>
''' <returns></returns>
Public Shared Function UrlWebEncode(ByVal textToEncode As String) As String
Dim PreTreatedText As String = WebUtility.UrlEncode(textToEncode)
Dim TreatedText As String = Regex.Replace(PreTreatedText, "%\w{2}", Function(x) x.Value.ToUpper)
Return TreatedText
End Function
''' <summary>
''' Decodifica una cadena de texto en URLENCODE.
''' </summary>
''' <param name="text">Texto a decodificar.</param>
''' <returns></returns>
Public Shared Function UrlWebDecode(ByVal text As String) As String
Return WebUtility.UrlDecode(text)
End Function
''' <summary>
''' Evalua una línea de texto (formato IRC según la RFC) y entrega el usuario que emitió el mensaje.
''' </summary>
''' <param name="response">Mensaje a evaluar.</param>
''' <returns></returns>
Public Shared Function GetUserFromChatresponse(ByVal response As String) As String
Return response.Split("!"c)(0).Replace(":", "")
End Function
''' <summary>
''' Elimina todas las letras dejando únicamente números
''' </summary>
''' <param name="text">Texto a evaluar</param>
''' <returns></returns>
Public Shared Function RemoveAllAlphas(ByVal text As String) As String
Return Regex.Replace(text, "[^0-9]", "")
End Function
''' <summary>
''' Retorna una cadena de texto formateada de tal forma que muestra colores, para mas info ver https://github.com/myano/jenni/wiki/IRC-String-Formatting
''' </summary>
''' <param name="ForegroundColor">Color del texto</param>
''' <param name="BackgroundColor">Color del fondo, si se omite se usa el color por defecto del cliente irc</param>
Public Shared Function ColoredText(ByVal text As String, ForegroundColor As Integer, Optional BackgroundColor As Integer = 99) As String
Dim _foregroundColor As String = ForegroundColor.ToString("00")
Dim _backgroundColor As String = BackgroundColor.ToString("00")
If _backgroundColor = "99" Then
Return Chr(3) & _foregroundColor & text & Chr(3) & Chr(15)
Else
Return Chr(3) & _foregroundColor & "," & _backgroundColor & text & Chr(3) & Chr(15)
End If
End Function
''' <summary>
''' Cuenta las veces que se repite una cadena de texto en otra cadena de texto.
''' </summary>
''' <param name="StToSearch"></param>
''' <param name="StToLookFor"></param>
''' <returns></returns>
Public Shared Function CountOccurrences(ByVal StToSearch As String, StToLookFor As String) As Integer
Dim txtlen As Integer = StToSearch.Length
Dim strlen As Integer = StToLookFor.Length
Dim newstring As String = StToSearch.Replace(StToLookFor, String.Empty)
Dim newtxtlen As Integer = newstring.Length
Dim lenghtdiff As Integer = txtlen - newtxtlen
Dim occurences As Integer = CInt(lenghtdiff / strlen)
Return occurences
End Function
''' <summary>
''' Entrega un valor que simboliza el nivel de aparición de las palabras indicadas
''' </summary>
''' <param name="Phrase">Frase a evaluar</param>
''' <param name="words">Palabras a buscar</param>
''' <returns></returns>
Public Shared Function LvlOfAppereance(ByVal phrase As String, words As String()) As Double
If (phrase Is Nothing) Or (words Is Nothing) Then
Return 0
End If
Dim PhraseString As String() = phrase.Split(Chr(32))
Dim NOWords As Integer = PhraseString.Count
Dim NOAppeareances As Integer = 0
For a As Integer = 0 To NOWords - 1
For Each s As String In words
If PhraseString(a).ToLower.Contains(s.ToLower) Then
NOAppeareances += 1
End If
Next
Next
Return ((CType(NOAppeareances, Double) * 100) / CType(NOWords, Double))
End Function
''' <summary>
''' Separa un array de string segun lo especificado. Retorna una lista con listas de texto.
''' </summary>
''' <param name="StrArray">Lista a partir</param>
''' <param name="chunkSize">En cuantos items se parte</param>
''' <returns></returns>
Public Shared Function SplitStringArrayIntoChunks(strArray As String(), chunkSize As Integer) As List(Of List(Of String))
Return strArray.
Select(Function(x, i) New With {Key .Index = i, Key .Value = x}).
GroupBy(Function(x) (x.Index \ chunkSize)).
Select(Function(x) x.Select(Function(v) v.Value).ToList()).
ToList()
End Function
''' <summary>
''' Retorna una cadena de texto que interpreta los bytes entregados como Byte, Kbyte o Mbyte respectivamente.
''' Importante: El valor debe ser en Bytes, no Bits. La función redondea en la segunda posición decimal.
''' </summary>
''' <param name="bytes"></param>
''' <returns></returns>
Public Shared Function GetSizeAsString(ByVal bytes As Integer) As String
If bytes < 999 Then
Return bytes.ToString & " Bytes"
ElseIf bytes < 999999 Then
Return System.Math.Round((bytes / 1000), 2).ToString & " KB"
Else
Return System.Math.Round((bytes / 1000000), 2).ToString & " MB"
End If
End Function
''' <summary>
''' Retorna el destino del enlace interno y el texto del mismo. Debe pasarse como parámetro una cadena de texto con formato de enlace wiki. Debe cumplir con la expresión regular "(\[\[)(.+?)(\]\]))".
''' </summary>
''' <param name="tlink"></param>
''' <returns></returns>
Public Shared Function GetLinkText(ByVal tlink As String) As Tuple(Of String, String)
Dim tstr As String = tlink.Replace("[[", "").Replace("]]", "")
If tstr.Contains("|") Then
Dim tval As Tuple(Of String, String) = New Tuple(Of String, String)(tstr.Split("|"c)(0).Trim(), tstr.Split("|"c)(1).Trim())
Return tval
End If
Return New Tuple(Of String, String)(tstr, tstr)
End Function
#End Region
#Region "Math Functions"
''' <summary>
''' Retorna true si un numero es par.
''' </summary>
''' <param name="Number"></param>
''' <returns></returns>
Public Shared Function IsODD(ByVal number As Integer) As Boolean
Return (number Mod 2 = 0)
End Function
''' <summary>
''' Separa un array de integer segun lo especificado. Retorna una lista con listas de integer.
''' </summary>
''' <param name="IntArray">Lista a partir</param>
''' <param name="chunkSize">En cuantos items se parte</param>
''' <returns></returns>
Public Shared Function SplitIntegerArrayIntoChunks(intArray As Integer(), chunkSize As Integer) As List(Of List(Of Integer))
Return intArray.
Select(Function(x, i) New With {Key .Index = i, Key .Value = x}).
GroupBy(Function(x) (x.Index \ chunkSize)).
Select(Function(x) x.Select(Function(v) v.Value).ToList()).
ToList()
End Function
''' <summary>
''' Convierte una cadena de texto a un array de integer donde cada valor equivale a la representación numérica del cada caracter en la cadena.
''' </summary>
''' <param name="tstring"></param>
''' <returns></returns>
Public Shared Function StringToIntArray(ByVal tstring As String) As Integer()
Return tstring.ToCharArray.[Select](Function(c) Convert.ToInt32(c.ToString())).ToArray()
End Function
#End Region
#Region "Program Subs"
''' <summary>
''' Finaliza el programa correctamente.
''' </summary>
Public Shared Sub ExitProgram()
Environment.Exit(0)
End Sub
#End Region
#Region "Program Functions"
''' <summary>
''' Entrega la plataforma de ejecución.
''' </summary>
''' <returns></returns>
Public Shared Function GetPlatform() As String
Dim isFreebsd As Boolean = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD)
Dim isLinux As Boolean = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
Dim isWindows As Boolean = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
Dim isOSx As Boolean = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
Return If(isFreebsd, "FreeBDS", "") & If(isLinux, "Linux", "") & If(isWindows, "Windows", "") & If(isOSx, "OSX", "")
End Function
Public Shared Function GetOSDescription() As String
Return RuntimeInformation.OSDescription
End Function
Public Shared Function GetExecMode() As String
Dim processname As String = Process.GetCurrentProcess().ProcessName
Dim dotnet As Boolean = processname.Contains("dotnet")
Return If(dotnet, "dotnet (JIT)", "native")
End Function
Public Shared Function GetOsString() As String
Return GetOSDescription() & " (" & GetPlatform() & ", " & GetExecMode() & ")"
End Function
''' <summary>
''' Intercambia dos objetos del mismo tipo
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="arg1"></param>
''' <param name="arg2"></param>;
Sub Swap(Of T)(ByRef arg1 As T, ByRef arg2 As T)
Dim temp As T = arg1
arg1 = arg2
arg2 = temp
End Sub
''' <summary>
''' Establece un tiempo de espera (en segundos)
''' </summary>
''' <param name="seconds"></param>
''' <returns></returns>
Public Shared Function WaitSeconds(ByVal seconds As Integer) As Boolean
System.Threading.Thread.Sleep(seconds * 1000)
Return True
End Function
''' <summary>
''' Convierte una cadena de texto con una hora en formato unix a DateTime
''' </summary>
''' <param name="strUnixTime"></param>
''' <returns></returns>
Public Shared Function UnixToTime(ByVal strUnixTime As String) As Date
UnixToTime = DateAdd(DateInterval.Second, Val(strUnixTime), #1/1/1970#)
If UnixToTime.IsDaylightSavingTime = True Then
UnixToTime = DateAdd(DateInterval.Hour, 1, UnixToTime)
End If
End Function
''' <summary>
''' Convierte una fecha a un numero entero que representa la hora en formato unix
''' </summary>
''' <param name="dteDate"></param>
''' <returns></returns>
Public Shared Function TimeToUnix(ByVal dteDate As Date) As Integer
If dteDate.IsDaylightSavingTime = True Then
dteDate = DateAdd(DateInterval.Hour, -1, dteDate)
End If
TimeToUnix = CInt(DateDiff(DateInterval.Second, #1/1/1970#, dteDate))
End Function
''' <summary>
''' Convierte una cadena de texto con formato especial a segundos
''' </summary>
''' <param name="time"></param>
''' <returns></returns>
Public Shared Function TimeStringToSeconds(ByVal time As String) As Integer
Try
Dim Str1 As String() = time.Split(CType(":", Char))
Dim Str2 As String() = Str1(0).Split(CType(".", Char))
Dim Str_Days As String = Str2(0)
Dim Str_Hours As String = Str2(1)
Dim Str_Minutes As String = Str1(1)
Dim Days As Integer = Convert.ToInt32(Str_Days) * 86400
Dim Hours As Integer = Convert.ToInt32(Str_Hours) * 3600
Dim Minutes As Integer = Convert.ToInt32(Str_Minutes) * 60
Dim total As Integer = (Days + Hours + Minutes)
Return total
Catch ex As IndexOutOfRangeException
Return 0
End Try
End Function
''' <summary>
''' Retorna los hilos hijos del programa, puede variar según el framework. No es fiable como indicador de tareas en ejecución.
''' </summary>
''' <returns></returns>
Public Shared Function GetCurrentThreads() As Integer
Return Process.GetCurrentProcess().Threads.Count
End Function
''' <summary>
''' Entrega la cantidad de memoria reservada para el proceso, depende del framework y no tiene relación directa con el rendimiento real del programa.
''' </summary>
''' <returns></returns>
Public Shared Function PrivateMemory() As Long
Return CLng(Process.GetCurrentProcess().PrivateMemorySize64 / 1024)
End Function
''' <summary>
''' Entrega la cantidad de memoria que el programa está usando efectivamente.
''' </summary>
''' <returns></returns>
Public Shared Function UsedMemory() As Long
Return CLng(Process.GetCurrentProcess().WorkingSet64 / 1024)
End Function
''' <summary>
''' Entrega Verdadero si el usuario presiona cualquier tecla dentro del tiempo indicado
''' </summary>
''' <returns></returns>
Public Shared Function PressKeyTimeout(ByVal Timeout As Integer) As Boolean
Dim exitloop As Boolean = False
Console.Write(SStrings.PressKey)
For ttime As Integer = 0 To Timeout
Console.Write(".")
If Console.KeyAvailable Then
exitloop = True
Exit For
End If
System.Threading.Thread.Sleep(1000)
Next
Console.Write(Environment.NewLine)
Return exitloop
End Function
#End Region
#Region "Wikipedia exclusive"
''' <summary>
''' Entrega como array de DateTime todas las fechas (Formato de firma Wikipedia) en el texto dado.
''' </summary>
''' <param name="text">Texto a evaluar</param>
''' <returns></returns>
Public Shared Function AllDateTimes(ByVal text As String) As DateTime()
Dim Datelist As New List(Of DateTime)
For Each m As Match In Regex.Matches(text, signpattern)
Dim TheDate As DateTime = ESWikiDatetime(m.Value)
Datelist.Add(TheDate)
Next
Return Datelist.ToArray
End Function
''' <summary>
''' Evalua texto (wikicódigo) y regresa un array de string con cada uno de los hilos del mismo (los que comienzan con = ejemplo = y terminan en otro comienzo o el final de la página).
''' </summary>
''' <param name="pagetext">Texto a evaluar</param>
''' <returns></returns>
Public Shared Function GetPageMainThreads(ByVal pagetext As String) As String()
Dim tutil As Utils = New Utils
Return tutil.GetLvlThread(pagetext, 1)
End Function
''' <summary>
''' Evalua texto (wikicódigo) y regresa un array de string con cada uno de los hilos del mismo (los que comienzan con == ejemplo == y terminan en otro comienzo o el final de la página).
''' </summary>
''' <param name="pagetext">Texto a evaluar</param>
''' <returns></returns>
Public Shared Function GetPageThreads(ByVal pagetext As String) As String()
Dim tutil As Utils = New Utils
Return tutil.GetLvlThread(pagetext, 2)
End Function
''' <summary>
''' Evalua texto (wikicódigo) y regresa un array de string con cada uno de los subhilos del mismo (los que comienzan con === ejemplo === y terminan en otro subhilo o el final de la página).
''' </summary>
''' <param name="pagetext">Texto a evaluar</param>
''' <returns></returns>
Public Shared Function GetPageSubThreads(ByVal pagetext As String) As String()
Dim tutil As Utils = New Utils
Return tutil.GetLvlThread(pagetext, 3)
End Function
''' <summary>
''' Función base que obtiene los secciones de una página según la expresión regular que defina las secciones.
''' </summary>
''' <param name="pagetext">Texto</param>
''' <param name="lvl">Nivel (= 1 = | == 2 == | === 3 === | ==== 4 ==== ), cualquier otro valor será ignorado y se trabajará con Nivel 2</param>
''' <returns></returns>
Private Function GetLvlThread(ByVal pagetext As String, ByVal lvl As Integer) As String()
Dim ThreadList As New List(Of String)
Dim temptext As String = pagetext
Dim commentMatch As MatchCollection = Regex.Matches(temptext, "(<!--)[\s\S]*?(-->)")
Dim NowikiMatch As MatchCollection = Regex.Matches(temptext, "(<[nN]owiki>)([\s\S]+?)(<\/[nN]owiki>)")
Dim CodeMatch As MatchCollection = Regex.Matches(temptext, "(<[cC]ode>)([\s\S]+?)(<\/[cC]ode>)")
Dim PreMatch As MatchCollection = Regex.Matches(temptext, "(<pre>)([\s\S]+?)(<\/pre>)", RegexOptions.IgnoreCase)
Dim SrcMatch As MatchCollection = Regex.Matches(temptext, "(<source( [^>]*)?>)([\s\S]+?)(<\/source>)", RegexOptions.IgnoreCase)
Dim SyntaxHlMatch As MatchCollection = Regex.Matches(temptext, "(<syntaxhighlight( [^>]*)?>)([\s\S]+?)(<\/syntaxhighlight>)", RegexOptions.IgnoreCase)
'Comentarios
Dim CommentsList As New List(Of String)
'Nowiki
Dim NowikiList As New List(Of String)
'Code
Dim CodeList As New List(Of String)
'Pre
Dim Prelist As New List(Of String)
'Src
Dim SrcList As New List(Of String)
'SynHL
Dim SyntaxHlList As New List(Of String)
'Comentarios
For i As Integer = 0 To commentMatch.Count - 1
CommentsList.Add(commentMatch(i).Value)
temptext = temptext.Replace(commentMatch(i).Value, ColoredText("PERIODIBOT::::COMMENTREPLACE::::" & i, 4))
Next
'Nowiki
For i As Integer = 0 To NowikiMatch.Count - 1
NowikiList.Add(NowikiMatch(i).Value)
temptext = temptext.Replace(NowikiMatch(i).Value, ColoredText("PERIODIBOT::::NOWIKIREPLACE::::" & i, 4))
Next
'Code
For i As Integer = 0 To CodeMatch.Count - 1
CodeList.Add(CodeMatch(i).Value)
temptext = temptext.Replace(CodeMatch(i).Value, ColoredText("PERIODIBOT::::CODEREPLACE::::" & i, 4))
Next
'Pre
For i As Integer = 0 To PreMatch.Count - 1
Prelist.Add(PreMatch(i).Value)
temptext = temptext.Replace(PreMatch(i).Value, ColoredText("PERIODIBOT::::PREREPLACE::::" & i, 4))
Next
'Src
For i As Integer = 0 To SrcMatch.Count - 1
SrcList.Add(SrcMatch(i).Value)
temptext = temptext.Replace(SrcMatch(i).Value, ColoredText("PERIODIBOT::::SRCREPLACE::::" & i, 4))
Next
'SynHL
For i As Integer = 0 To SyntaxHlMatch.Count - 1
SyntaxHlList.Add(SyntaxHlMatch(i).Value)
temptext = temptext.Replace(SyntaxHlMatch(i).Value, ColoredText("PERIODIBOT::::SYNREPLACE::::" & i, 4))
Next
Dim RegexExpression As String
Select Case lvl
Case 1
RegexExpression = "(((\n|\r|^)(=(?!=))(.+)(=(?!=))[ \t]*(\n|\r))([\s\S]*?))(?=(((\n|\r)(=(?!=))(.+)(=(?!=))[ \t]*(\n|\r))([\s\S]*?))|$)"
Case 2
RegexExpression = "(((\n|\r|^)(==(?!=))(.+)(==(?!=))[ \t]*(\n|\r))([\s\S]*?))(?=(((\n|\r)(==(?!=))(.+)(==(?!=))[ \t]*(\n|\r))([\s\S]*?))|$)"
Case 3
RegexExpression = "(((\n|\r|^)(===(?!=))(.+)(===(?!=))[ \t]*(\n|\r))[\s\S]*?)(?=(((\n|\r)(=){1,4}(.+)(=){1,4}[ \t]*(\n|\r))[\s\S]*?)|$)"
Case 4
RegexExpression = "(((\n|\r|^)(====(?!=))(.+)(====(?!=))[ \t]*(\n|\r))[\s\S]*?)(?=(((\n|\r)(=){1,4}(.+)(=){1,4}[ \t]*(\n|\r))[\s\S]*?)|$)"