package com.dar.nclientv2.async; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.util.JsonReader; import androidx.annotation.Nullable; import androidx.core.app.JobIntentService; import com.dar.nclientv2.api.components.Tag; import com.dar.nclientv2.api.enums.TagStatus; import com.dar.nclientv2.api.enums.TagType; import com.dar.nclientv2.async.database.Queries; import com.dar.nclientv2.settings.Global; import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.List; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; public class ScrapeTags extends JobIntentService { private static final String DATA_FOLDER = "https://raw.githubusercontent.com/Dar9586/NClientV2/master/data/"; private static final int DAYS_UNTIL_SCRAPE = 7; private static final String TAGS = "https://raw.githubusercontent.com/Dar9586/NClientV2/master/data/tags.json"; private static final String VERSION = "https://raw.githubusercontent.com/Dar9586/NClientV2/master/data/tagsVersion"; private boolean enoughDayPassed(Date date, Date date2) { if (date.getTime() == date2.getTime()) { return true; } Calendar instance = Calendar.getInstance(); Calendar instance2 = Calendar.getInstance(); instance.setTime(date); instance2.setTime(date2); int i = 0; while (instance2.before(instance)) { instance2.add(5, 1); i++; if (i > 7) { return true; } } return false; } private void fetchTags() { Response execute = Global.getClient(this).newCall(new Request.Builder().url(TAGS).build()).execute(); ResponseBody body = execute.body(); if (body == null) { execute.close(); return; } JsonReader jsonReader = new JsonReader(body.charStream()); jsonReader.beginArray(); while (jsonReader.hasNext()) { Queries.TagTable.insertScrape(readTag(jsonReader), true); } jsonReader.close(); execute.close(); } private int getNewVersionCode() { Response execute = Global.getClient(this).newCall(new Request.Builder().url(VERSION).build()).execute(); ResponseBody body = execute.body(); if (body == null) { execute.close(); return -1; } try { int parseInt = Integer.parseInt(body.string().trim()); execute.close(); return parseInt; } catch (NumberFormatException unused) { return -1; } } private Tag readTag(JsonReader jsonReader) { jsonReader.beginArray(); int nextInt = jsonReader.nextInt(); String nextString = jsonReader.nextString(); int nextInt2 = jsonReader.nextInt(); TagType tagType = TagType.values[jsonReader.nextInt()]; jsonReader.endArray(); return new Tag(nextString, nextInt2, nextInt, tagType, TagStatus.DEFAULT); } public static void startWork(Context context) { JobIntentService.enqueueWork(context, ScrapeTags.class, 2000, new Intent()); } @Override // androidx.core.app.JobIntentService public final void c(@Nullable Intent intent) { int i; IOException e2; SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Settings", 0); Date date = new Date(); Date date2 = new Date(sharedPreferences.getLong("lastSync", date.getTime())); int i2 = sharedPreferences.getInt("lastTagsVersion", -1); if (enoughDayPassed(date, date2)) { try { i = getNewVersionCode(); if (i2 <= -1 || i2 < i) { try { List<Tag> allFiltered = Queries.TagTable.getAllFiltered(); fetchTags(); for (Tag tag : allFiltered) { Queries.TagTable.updateStatus(tag.getId(), tag.getStatus()); } } catch (IOException e3) { e2 = e3; e2.printStackTrace(); sharedPreferences.edit().putLong("lastSync", date.getTime()).putInt("lastTagsVersion", i).apply(); } sharedPreferences.edit().putLong("lastSync", date.getTime()).putInt("lastTagsVersion", i).apply(); } } catch (IOException e4) { e2 = e4; i = -1; e2.printStackTrace(); sharedPreferences.edit().putLong("lastSync", date.getTime()).putInt("lastTagsVersion", i).apply(); } } } }