|
@@ -0,0 +1,61 @@
|
|
1
|
+package fr.forum_thalie.tsumugi.planning
|
|
2
|
+
|
|
3
|
+import android.content.Context
|
|
4
|
+import fr.forum_thalie.tsumugi.Async
|
|
5
|
+import org.json.JSONObject
|
|
6
|
+import java.io.IOException
|
|
7
|
+import java.net.URL
|
|
8
|
+
|
|
9
|
+class PlanningParser {
|
|
10
|
+
|
|
11
|
+ val programs: ArrayList<Program> = ArrayList()
|
|
12
|
+
|
|
13
|
+ fun parseUrl(url: String? = null, context: Context? = null)
|
|
14
|
+ {
|
|
15
|
+ val scrape : (Any?) -> String = {
|
|
16
|
+ if (url.isNullOrEmpty() && context != null)
|
|
17
|
+ {
|
|
18
|
+ val json: String
|
|
19
|
+ try {
|
|
20
|
+ val inputStream = context.assets.open("planning_example.json")
|
|
21
|
+ val size = inputStream.available()
|
|
22
|
+ val buffer = ByteArray(size)
|
|
23
|
+ inputStream.use { it.read(buffer) }
|
|
24
|
+ json = String(buffer)
|
|
25
|
+ json
|
|
26
|
+ } catch (ioException: IOException) {
|
|
27
|
+ ioException.printStackTrace()
|
|
28
|
+ ""
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+ else
|
|
32
|
+ URL(url).readText()
|
|
33
|
+
|
|
34
|
+ }
|
|
35
|
+ val post : (parameter: Any?) -> Unit = {
|
|
36
|
+ val result = JSONObject(it as String)
|
|
37
|
+ if (result.has("planning"))
|
|
38
|
+ {
|
|
39
|
+ val programList = result.getJSONArray("planning")
|
|
40
|
+ for (i in 0 until programList.length())
|
|
41
|
+ {
|
|
42
|
+ val item = programList[i] as JSONObject
|
|
43
|
+ val periodicity = item.getInt("periodicity")
|
|
44
|
+ val hourBeginS = item.getString("hour_begin").split(":")
|
|
45
|
+ val hourBegin = hourBeginS.first().toInt()*60 + hourBeginS.last().toInt()
|
|
46
|
+ val hourEndS = item.getString("hour_end").split(":")
|
|
47
|
+ val hourEnd = hourEndS.first().toInt()* 60 + hourEndS.last().toInt()
|
|
48
|
+ val title = item.getString("title")
|
|
49
|
+ programs.add(Program(title, periodicity, hourBegin, hourEnd))
|
|
50
|
+ }
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+ Async(scrape, post)
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ companion object {
|
|
57
|
+ val instance by lazy {
|
|
58
|
+ PlanningParser()
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+}
|