added planning parser from simple JSON format (with local example)
parent
9ea24e2e63
commit
5920c8cd21
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"planning": [
|
||||||
|
{
|
||||||
|
"title": "Anime Matin",
|
||||||
|
"periodicity": "1111100",
|
||||||
|
"hour_begin": "9:00",
|
||||||
|
"hour_end": "10:30"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Jeux Vidéos Matin",
|
||||||
|
"periodicity": "1111100",
|
||||||
|
"hour_begin": "09:00",
|
||||||
|
"hour_end": "10:30"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "Programmation nocturne",
|
||||||
|
"periodicity": "1111111",
|
||||||
|
"hour_begin": "23:00",
|
||||||
|
"hour_end": "5:30"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -16,6 +16,7 @@ import fr.forum_thalie.tsumugi.playerstore.PlayerStore
|
||||||
import java.util.Timer
|
import java.util.Timer
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import fr.forum_thalie.tsumugi.alarm.RadioAlarm
|
import fr.forum_thalie.tsumugi.alarm.RadioAlarm
|
||||||
|
import fr.forum_thalie.tsumugi.planning.PlanningParser
|
||||||
|
|
||||||
|
|
||||||
/* Log to file import
|
/* Log to file import
|
||||||
|
|
@ -155,6 +156,9 @@ class MainActivity : BaseActivity() {
|
||||||
isTimerStarted = true
|
isTimerStarted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fetch program
|
||||||
|
PlanningParser.instance.parseUrl(/* getString(R.string.planning_url) */ context = this)
|
||||||
|
|
||||||
// initialize the UI
|
// initialize the UI
|
||||||
setTheme(R.style.AppTheme)
|
setTheme(R.style.AppTheme)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
package fr.forum_thalie.tsumugi.planning
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import fr.forum_thalie.tsumugi.Async
|
||||||
|
import org.json.JSONObject
|
||||||
|
import java.io.IOException
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
|
class PlanningParser {
|
||||||
|
|
||||||
|
val programs: ArrayList<Program> = ArrayList()
|
||||||
|
|
||||||
|
fun parseUrl(url: String? = null, context: Context? = null)
|
||||||
|
{
|
||||||
|
val scrape : (Any?) -> String = {
|
||||||
|
if (url.isNullOrEmpty() && context != null)
|
||||||
|
{
|
||||||
|
val json: String
|
||||||
|
try {
|
||||||
|
val inputStream = context.assets.open("planning_example.json")
|
||||||
|
val size = inputStream.available()
|
||||||
|
val buffer = ByteArray(size)
|
||||||
|
inputStream.use { it.read(buffer) }
|
||||||
|
json = String(buffer)
|
||||||
|
json
|
||||||
|
} catch (ioException: IOException) {
|
||||||
|
ioException.printStackTrace()
|
||||||
|
""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
URL(url).readText()
|
||||||
|
|
||||||
|
}
|
||||||
|
val post : (parameter: Any?) -> Unit = {
|
||||||
|
val result = JSONObject(it as String)
|
||||||
|
if (result.has("planning"))
|
||||||
|
{
|
||||||
|
val programList = result.getJSONArray("planning")
|
||||||
|
for (i in 0 until programList.length())
|
||||||
|
{
|
||||||
|
val item = programList[i] as JSONObject
|
||||||
|
val periodicity = item.getInt("periodicity")
|
||||||
|
val hourBeginS = item.getString("hour_begin").split(":")
|
||||||
|
val hourBegin = hourBeginS.first().toInt()*60 + hourBeginS.last().toInt()
|
||||||
|
val hourEndS = item.getString("hour_end").split(":")
|
||||||
|
val hourEnd = hourEndS.first().toInt()* 60 + hourEndS.last().toInt()
|
||||||
|
val title = item.getString("title")
|
||||||
|
programs.add(Program(title, periodicity, hourBegin, hourEnd))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Async(scrape, post)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val instance by lazy {
|
||||||
|
PlanningParser()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
package fr.forum_thalie.tsumugi.planning
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import fr.forum_thalie.tsumugi.tag
|
||||||
|
import org.json.JSONObject
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class Program (val title: String, private val periodicity: Int, private val hourBegin: Int, private val hourEnd: Int) {
|
||||||
|
fun isCurrent(): Boolean
|
||||||
|
{
|
||||||
|
val now = Calendar.getInstance()
|
||||||
|
val currentDay = if (now.get(Calendar.DAY_OF_WEEK) - 1 == 0) 6 else now.get(Calendar.DAY_OF_WEEK) - 2
|
||||||
|
// 0 (Monday) to 5 (Saturday) + 6 (Sunday)
|
||||||
|
|
||||||
|
// this translates to "true" when:
|
||||||
|
// - the currentDay is flagged in the "periodicity" bit array
|
||||||
|
// OR
|
||||||
|
// - Yesterday is flagged in the "periodicity" bit array AND the program does span over 2 days (night programs typically).
|
||||||
|
// We'll check a after this whether the current hour is within the span.
|
||||||
|
val isNow: Boolean = ((((1000000 shr currentDay) and (periodicity)) == 1)) || (((1000000 shr ((currentDay-1)%7) and (periodicity)) == 1) && hourEnd < hourBegin)
|
||||||
|
|
||||||
|
// shr = shift-right. It's a binary mask.
|
||||||
|
if (isNow)
|
||||||
|
{
|
||||||
|
val hasBegun = (now.get(Calendar.HOUR_OF_DAY)*60 + now.get(Calendar.MINUTE) >= hourBegin )
|
||||||
|
val hasNotEnded = (now.get(Calendar.HOUR_OF_DAY)*60 + now.get(Calendar.MINUTE) <= hourEnd )
|
||||||
|
if (hasBegun && hasNotEnded)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "Title: $title, time info (periodicity, begin, end): $periodicity, $hourBegin, $hourEnd"
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
Log.d(tag, this.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -43,5 +43,6 @@
|
||||||
<string name="disable">Disable</string>
|
<string name="disable">Disable</string>
|
||||||
<string name="website_url">https://tsumugi.forum-thalie.fr/</string>
|
<string name="website_url">https://tsumugi.forum-thalie.fr/</string>
|
||||||
<string name="rss_url">https://tsumugi.forum-thalie.fr/?feed=rss2</string>
|
<string name="rss_url">https://tsumugi.forum-thalie.fr/?feed=rss2</string>
|
||||||
|
<string name="planning_url">ADD SOME URL HERE...</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue