Skip to content

Commit

Permalink
experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
antony committed Nov 4, 2023
1 parent 2d17034 commit bc10841
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,35 @@ class BookmarksHelper(_userId: String) {
.create(PixivAuthFeedJsonService::class.java)
private val userId: String = _userId

fun getNewIllusts(maxBookmarkId: String): Illusts {
val call = service.getBookmarkOffsetJson(userId, maxBookmarkId)
fun getNewPublicBookmarks(maxBookmarkId: String): Illusts {
val call = service.getPublicBookmarkOffsetJson(userId, maxBookmarkId)
illusts = call.execute().body()!!
return illusts
}

fun getNewIllusts(): Illusts {
val call = service.getBookmarkJson(userId)
fun getNewPrivateIllusts(maxBookmarkId: String): Illusts {
val call = service.getPrivateBookmarkOffsetJson(userId, maxBookmarkId)
illusts = call.execute().body()!!
return illusts
}

fun getNewPublicBookmarks(): Illusts {
val call = service.getPublicBookmarkJson(userId)
illusts = call.execute().body()!!
return illusts
}

fun getNewPrivateIllusts(): Illusts {
val call = service.getPrivateBookmarkJson(userId)
illusts = call.execute().body()!!
return illusts
}

fun getNextBookmarks(): Illusts {
val call = service.getNextUrl(illusts.next_url)
illusts = call.execute().body()!!
return illusts
}

fun getBookmarks() = illusts
}
56 changes: 32 additions & 24 deletions app/src/main/java/com/antony/muzei/pixiv/provider/PixivArtWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,40 +107,42 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
*
* Example:
* Say that the initial value is 100, and the target value is 67.5
* The inital search at 100 is too high, so we go down by half the search space to 50
* We perform an initial search at value 100
* We find the initial value is too high, so we go down by half the search space to 50
* 50 is too low, go up by half the previous step (25 = 50 / 2) to 75
* 75 is too high, so we go down by half the previous step (12.5 = 25 / 2) to 67.5
* We have a match
*/
private fun findBookmarkStartTime(userId: String) {
Log.d(LOG_TAG, "Looking for oldest bookmark id")
val service: PixivAuthFeedJsonService = RestClient.getRetrofitAuthInstance()
.create(PixivAuthFeedJsonService::class.java)
var call: Call<Illusts?> = service.getBookmarkJson(userId)
val bookmarks = BookmarksHelper(userId)
bookmarks.getNewPublicBookmarks()

// setting initial values
var counter = 0
var callingId = 0L
// setting initial value
var illusts = call.execute().body()!!
var step = illusts.next_url!!.substringAfter("max_bookmark_id=").toLong()
var step = bookmarks.getBookmarks().next_url!!.substringAfter("max_bookmark_id=").toLong()

while (true) {
counter++
step /= 2 //Halving the step size
Log.d(LOG_TAG, "Iteration $counter")
if (illusts.next_url != null) {
// we are too high
val currentId = illusts.next_url!!.substringAfter("max_bookmark_id=").toLong()
step /= 2
if (bookmarks.getBookmarks().next_url != null) {
// we are not looking far enough in the past
// we need to reduce the maxBookmarkId parameter to
val currentId =
bookmarks.getBookmarks().next_url!!.substringAfter("max_bookmark_id=").toLong()
callingId = currentId - step
} else if (illusts.artworks.isEmpty()) {
// we are too low
step /= 2
} else if (bookmarks.getBookmarks().artworks.isEmpty()) {
// we are looking too far in the past
// we need to increase the maxBookmarkId parameter to look closer to current time
callingId += step
} else {
// we have found the stop criteria
Log.d(LOG_TAG, "Found at $callingId")
break
}
call = service.getBookmarkOffsetJson(userId, callingId.toString())
illusts = call.execute().body()!!
bookmarks.getNewPublicBookmarks(callingId.toString())
}
with(PreferenceManager.getDefaultSharedPreferences(applicationContext).edit()) {
putLong("oldestMaxBookmarkId", callingId)
Expand Down Expand Up @@ -744,11 +746,13 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
val oldestBookmarkId = sharedPrefs.getLong("oldestMaxBookmarkId", 0)
// Find the upper bound
val currentBookmarkId: Long =
(bookmarksHelper.getNewIllusts().next_url?.substringAfter("max_bookmark_id=")!!
(bookmarksHelper.getNewPublicBookmarks().next_url?.substringAfter("max_bookmark_id=")!!
.toLong() * 1.01).toLong()

var bookmarkArtworks =
bookmarksHelper.getNewIllusts((oldestBookmarkId..currentBookmarkId).random().toString()).artworks
bookmarksHelper.getNewPublicBookmarks(
(oldestBookmarkId..currentBookmarkId).random().toString()
).artworks
val artworkList = mutableListOf<Artwork>()
var counter = 0
while (counter < sharedPrefs.getInt("prefSlider_numToDownload", 2)) {
Expand All @@ -758,7 +762,9 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
} catch (e: FilterMatchNotFoundException) {
Log.i(LOG_TAG, "Fetching new bookmarks")
bookmarkArtworks =
bookmarksHelper.getNewIllusts((oldestBookmarkId..currentBookmarkId).random().toString()).artworks
bookmarksHelper.getNewPublicBookmarks(
(oldestBookmarkId..currentBookmarkId).random().toString()
).artworks
continue
} catch (e: CorruptFileException) {
Log.i(LOG_TAG, "Corrupt artwork found")
Expand All @@ -770,11 +776,12 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
return artworkList
}

// Bookmarks artworks are handled in a separate function
// Part of the reason is that Pixiv itself has different API surface for bookmarks
// And must be handled accordingly
private fun getArtworksAuth(updateMode: String): List<Artwork> {
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(applicationContext)
// Determines if any extra information is needed, and passes it along

// {"follow", "bookmark", "tag_search", "artist", "recommended"};
val illustsHelper = when (updateMode) {
"follow" -> IllustsHelper(updateMode)
"recommended" -> IllustsHelper(updateMode)
Expand All @@ -784,6 +791,7 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
sharedPrefs.getString("pref_tagSearch", "") ?: "",
sharedPrefs.getString("pref_tagLanguage", "") ?: ""
)

else -> IllustsHelper("follow")
}
var authArtworkList = illustsHelper.getNewIllusts().artworks
Expand Down Expand Up @@ -847,9 +855,6 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
}
}

// App has functionality to temporarily or permanently change the update mode if authentication fails
// i.e. update mode can change between the previous if block and this if block
// Thus two identical if statements are required
Log.i(LOG_TAG, "Feed mode: $updateMode")
val artworkList: List<Artwork> = when (updateMode) {
"bookmark" -> getArtworksBookmark()
Expand All @@ -875,6 +880,7 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
})
return "daily"
}

"doNotChange_downDaily" -> {
Log.i(LOG_TAG, "Downloading a single daily")
PixivMuzeiSupervisor.post(Runnable {
Expand All @@ -886,6 +892,7 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
})
return "daily"
}

"doNotChange_doNotDown" -> {
Log.i(LOG_TAG, "Retrying with no changes")
PixivMuzeiSupervisor.post(Runnable {
Expand All @@ -897,6 +904,7 @@ class PixivArtWorker(context: Context, workerParams: WorkerParameters) :
})
return null
}

else -> return null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ public interface PixivAuthFeedJsonService {
Call<Illusts> getFollowJson();

@GET("v1/user/bookmarks/illust?restrict=public")
Call<Illusts> getBookmarkJson(@Query("user_id") String userId);
Call<Illusts> getPublicBookmarkJson(@Query("user_id") String userId);

@GET("v1/user/bookmarks/illust?restrict=private")
Call<Illusts> getPrivateBookmarkJson(@Query("user_id") String userId);

@GET("v1/user/bookmarks/illust?restrict=public")
Call<Illusts> getBookmarkOffsetJson(@Query("user_id") String userId, @Query("max_bookmark_id") String maxBookmarkId);
Call<Illusts> getPublicBookmarkOffsetJson(@Query("user_id") String userId, @Query("max_bookmark_id") String maxBookmarkId);

@GET("v1/user/bookmarks/illust?restrict=private")
Call<Illusts> getPrivateBookmarkOffsetJson(@Query("user_id") String userId, @Query("max_bookmark_id") String maxBookmarkId);

@GET("v1/search/illust?search_target=partial_match_for_tags&sort=date_desc")
Call<Illusts> getTagSearchJson(@Header("Accept-Language") String language, @Query("word") String tag);
Expand Down
70 changes: 35 additions & 35 deletions app/src/main/java/com/antony/muzei/pixiv/settings/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,41 @@ class MainActivity : PixivMuzeiActivity(), AdvOptionsPreferenceFragment.NightMod
}.attach()

// If Muzei is not installed, this will redirect the user to Muzei's Play Store listing
if (!isMuzeiInstalled) {
AlertDialog.Builder(this)
.setTitle(getString(R.string.dialogTitle_muzeiNotInstalled))
.setMessage(getString(R.string.dialog_installMuzei))
.setPositiveButton(R.string.dialog_yes) { dialog: DialogInterface?, which: Int ->
if (!IntentUtils.launchActivity(
this,
Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=net.nurik.roman.muzei"))
)
) {
val fallback = Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=net.nurik.roman.muzei")
)
IntentUtils.launchActivity(this, fallback)
}
}
.setNegativeButton(R.string.dialog_no) { dialog: DialogInterface, which: Int ->
// Do nothing
dialog.dismiss()
}
.show()
} else if (!isProviderSelected(this, BuildConfig.APPLICATION_ID + ".provider")) {
// If Pixiv for Muzei 3 is not the selected provider
AlertDialog.Builder(this)
.setTitle(applicationContext.getString(R.string.dialogTitle_muzeiNotActiveSource))
.setMessage(applicationContext.getString(R.string.dialog_selectSource))
.setNeutralButton(android.R.string.ok) { dialog: DialogInterface?, which: Int ->
val intent = createChooseProviderIntent(BuildConfig.APPLICATION_ID + ".provider")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
finishAffinity()
IntentUtils.launchActivity(this, intent)
}
.show()
}
// if (!isMuzeiInstalled) {
// AlertDialog.Builder(this)
// .setTitle(getString(R.string.dialogTitle_muzeiNotInstalled))
// .setMessage(getString(R.string.dialog_installMuzei))
// .setPositiveButton(R.string.dialog_yes) { dialog: DialogInterface?, which: Int ->
// if (!IntentUtils.launchActivity(
// this,
// Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=net.nurik.roman.muzei"))
// )
// ) {
// val fallback = Intent(
// Intent.ACTION_VIEW,
// Uri.parse("https://play.google.com/store/apps/details?id=net.nurik.roman.muzei")
// )
// IntentUtils.launchActivity(this, fallback)
// }
// }
// .setNegativeButton(R.string.dialog_no) { dialog: DialogInterface, which: Int ->
// // Do nothing
// dialog.dismiss()
// }
// .show()
// } else if (!isProviderSelected(this, BuildConfig.APPLICATION_ID + ".provider")) {
// // If Pixiv for Muzei 3 is not the selected provider
// AlertDialog.Builder(this)
// .setTitle(applicationContext.getString(R.string.dialogTitle_muzeiNotActiveSource))
// .setMessage(applicationContext.getString(R.string.dialog_selectSource))
// .setNeutralButton(android.R.string.ok) { dialog: DialogInterface?, which: Int ->
// val intent = createChooseProviderIntent(BuildConfig.APPLICATION_ID + ".provider")
// intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
// finishAffinity()
// IntentUtils.launchActivity(this, intent)
// }
// .show()
// }
}

// Checks if Muzei is installed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.antony.muzei.pixiv.settings.deleteArtwork.ArtworkDeletionFragment;
import com.antony.muzei.pixiv.settings.fragments.AdvOptionsPreferenceFragment;
import com.antony.muzei.pixiv.settings.fragments.BookmarkExportFragment;
import com.antony.muzei.pixiv.settings.fragments.CreditsPreferenceFragment;
import com.antony.muzei.pixiv.settings.fragments.MainPreferenceFragment;
import com.antony.muzei.pixiv.settings.fragments.RoadmapPreferenceFragment;
Expand All @@ -50,7 +51,7 @@ public Fragment createFragment(int position) {
default:
return new CreditsPreferenceFragment();
case 4:
return new RoadmapPreferenceFragment();
return new BookmarkExportFragment();
}
}

Expand Down
Loading

0 comments on commit bc10841

Please sign in to comment.