Add feeds and items greader requests

This commit is contained in:
Shinokuni 2019-08-10 19:13:39 +02:00
parent 6a5feb2d63
commit 829e6e98fc
16 changed files with 473 additions and 12 deletions

View File

@ -7,9 +7,15 @@ import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.ParsingResult;
import com.readrops.readropslibrary.services.SyncType;
import com.readrops.readropslibrary.services.freshrss.FreshRSSAPI;
import com.readrops.readropslibrary.services.freshrss.FreshRSSCredentials;
import com.readrops.readropslibrary.services.freshrss.FreshRSSService;
import com.readrops.readropslibrary.services.freshrss.FreshRSSSyncData;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFeed;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSItem;
import org.joda.time.LocalDateTime;
import java.util.List;
@ -46,7 +52,25 @@ public class FreshRSSRepository extends ARepository {
@Override
public Observable<Feed> sync(List<Feed> feeds, Account account) {
return null;
FreshRSSAPI api = new FreshRSSAPI(new FreshRSSCredentials(account.getToken(), account.getUrl()));
FreshRSSSyncData syncData = new FreshRSSSyncData();
long lastModified = LocalDateTime.now().toDateTime().getMillis();
SyncType syncType;
if (account.getLastModified() != 0) {
syncType = SyncType.CLASSIC_SYNC;
syncData.setLastModified(lastModified / 1000L);
} else
syncType = SyncType.INITIAL_SYNC;
return api.sync(syncType, syncData)
.flatMapObservable(syncResult -> {
insertFeeds(syncResult.getFeeds(), account);
insertItems(syncResult.getItems(), account);
return Observable.empty();
});
}
@Override
@ -78,4 +102,12 @@ public class FreshRSSRepository extends ARepository {
public Completable deleteFolder(Folder folder, Account account) {
return null;
}
private void insertFeeds(List<FreshRSSFeed> feeds, Account account) {
}
private void insertItems(List<FreshRSSItem> items, Account account) {
}
}

View File

@ -12,6 +12,7 @@ import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.ItemMatcher;
import com.readrops.app.utils.ParsingResult;
import com.readrops.app.utils.Utils;
import com.readrops.readropslibrary.services.SyncType;
import com.readrops.readropslibrary.services.nextcloudnews.NextNewsAPI;
import com.readrops.readropslibrary.services.nextcloudnews.NextNewsSyncData;
import com.readrops.readropslibrary.services.nextcloudnews.NextNewsSyncResult;
@ -67,16 +68,16 @@ public class NextNewsRepository extends ARepository {
try {
NextNewsAPI newsAPI = new NextNewsAPI(account.toNextNewsCredentials());
long lastModified = LocalDateTime.now().toDateTime().getMillis();
NextNewsAPI.SyncType syncType;
SyncType syncType;
if (account.getLastModified() != 0)
syncType = NextNewsAPI.SyncType.CLASSIC_SYNC;
syncType = SyncType.CLASSIC_SYNC;
else
syncType = NextNewsAPI.SyncType.INITIAL_SYNC;
syncType = SyncType.INITIAL_SYNC;
NextNewsSyncData syncData = new NextNewsSyncData();
if (syncType == NextNewsAPI.SyncType.CLASSIC_SYNC) {
if (syncType == SyncType.CLASSIC_SYNC) {
syncData.setLastModified(account.getLastModified() / 1000L);
syncData.setReadItems(database.itemDao().getReadChanges());
syncData.setUnreadItems(database.itemDao().getUnreadChanges());
@ -94,7 +95,7 @@ public class NextNewsRepository extends ARepository {
insertFeeds(syncResult.getFeeds(), account);
timings.addSplit("insert feeds");
insertItems(syncResult.getItems(), account, syncType == NextNewsAPI.SyncType.INITIAL_SYNC);
insertItems(syncResult.getItems(), account, syncType == SyncType.INITIAL_SYNC);
timings.addSplit("insert items");
timings.dumpToLog();

View File

@ -0,0 +1,6 @@
package com.readrops.readropslibrary.services;
public enum SyncType {
INITIAL_SYNC,
CLASSIC_SYNC
}

View File

@ -1,6 +1,11 @@
package com.readrops.readropslibrary.services.freshrss;
import androidx.annotation.NonNull;
import com.readrops.readropslibrary.services.API;
import com.readrops.readropslibrary.services.SyncType;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFeeds;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSItems;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSUserInfo;
import java.io.StringReader;
@ -16,7 +21,14 @@ public class FreshRSSAPI extends API<FreshRSSService> {
super(credentials, FreshRSSService.class, FreshRSSService.END_POINT);
}
public Single<String> login(String login, String password) {
/**
* Call token API to generate a new token from account credentials
*
* @param login
* @param password
* @return the generated token
*/
public Single<String> login(@NonNull String login, @NonNull String password) {
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("Email", login)
@ -35,4 +47,45 @@ public class FreshRSSAPI extends API<FreshRSSService> {
public Single<FreshRSSUserInfo> getUserInfo() {
return api.getUserInfo();
}
public Single<FreshRSSSyncResult> sync(@NonNull SyncType syncType, @NonNull FreshRSSSyncData syncData) {
FreshRSSSyncResult syncResult = new FreshRSSSyncResult();
return getFeeds()
.flatMap(freshRSSFeeds -> {
syncResult.setFeeds(freshRSSFeeds.getSubscriptions());
switch (syncType) {
case INITIAL_SYNC:
return getItems(EXCLUDE_ITEMS.EXCLUDE_READ_ITEMS.value, 10000, null);
case CLASSIC_SYNC:
return getItems(EXCLUDE_ITEMS.EXCLUDE_READ_ITEMS.value, 10000, syncData.getLastModified());
}
return Single.error(new Exception("Unknown sync type"));
})
.flatMap(freshRSSItems -> {
syncResult.setItems(freshRSSItems.getItems());
return Single.just(syncResult);
});
}
public Single<FreshRSSFeeds> getFeeds() {
return api.getFeeds();
}
public Single<FreshRSSItems> getItems(String excludeTarget, Integer max, Long lastModified) {
return api.getItems(excludeTarget, max, lastModified);
}
public enum EXCLUDE_ITEMS {
EXCLUDE_READ_ITEMS("user/-/state/com.google/read");
String value;
EXCLUDE_ITEMS(String value) {
this.value = value;
}
}
}

View File

@ -1,5 +1,7 @@
package com.readrops.readropslibrary.services.freshrss;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFeeds;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSItems;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSUserInfo;
import io.reactivex.Single;
@ -8,6 +10,7 @@ import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
public interface FreshRSSService {
@ -19,4 +22,9 @@ public interface FreshRSSService {
@GET("reader/api/0/user-info")
Single<FreshRSSUserInfo> getUserInfo();
@GET("reader/api/0/subscription/list?output=json")
Single<FreshRSSFeeds> getFeeds();
@GET("reader/api/0/stream/contents/user/-/state/com.google/reading-list")
Single<FreshRSSItems> getItems(@Query("xt") String excludeTarget, @Query("n") Integer max, @Query("ot") Long lastModified);
}

View File

@ -0,0 +1,18 @@
package com.readrops.readropslibrary.services.freshrss;
public class FreshRSSSyncData {
private long lastModified;
public FreshRSSSyncData() {
}
public long getLastModified() {
return lastModified;
}
public void setLastModified(long lastModified) {
this.lastModified = lastModified;
}
}

View File

@ -0,0 +1,35 @@
package com.readrops.readropslibrary.services.freshrss;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSFeed;
import com.readrops.readropslibrary.services.freshrss.json.FreshRSSItem;
import java.util.ArrayList;
import java.util.List;
public class FreshRSSSyncResult {
private List<FreshRSSFeed> feeds;
private List<FreshRSSItem> items;
public FreshRSSSyncResult() {
feeds = new ArrayList<>();
items = new ArrayList<>();
}
public List<FreshRSSFeed> getFeeds() {
return feeds;
}
public void setFeeds(List<FreshRSSFeed> feeds) {
this.feeds = feeds;
}
public List<FreshRSSItem> getItems() {
return items;
}
public void setItems(List<FreshRSSItem> items) {
this.items = items;
}
}

View File

@ -0,0 +1,16 @@
package com.readrops.readropslibrary.services.freshrss.json;
public class FreshRSSAlternate {
private String href;
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
}

View File

@ -0,0 +1,26 @@
package com.readrops.readropslibrary.services.freshrss.json;
public class FreshRSSCategory {
private String id;
private String label;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}

View File

@ -0,0 +1,68 @@
package com.readrops.readropslibrary.services.freshrss.json;
import java.util.List;
public class FreshRSSFeed {
private List<FreshRSSCategory> categories;
private String htmlUrl;
private String iconUrl;
private String id;
private String title;
private String url;
public List<FreshRSSCategory> getCategories() {
return categories;
}
public void setCategories(List<FreshRSSCategory> categories) {
this.categories = categories;
}
public String getHtmlUrl() {
return htmlUrl;
}
public void setHtmlUrl(String htmlUrl) {
this.htmlUrl = htmlUrl;
}
public String getIconUrl() {
return iconUrl;
}
public void setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

View File

@ -0,0 +1,16 @@
package com.readrops.readropslibrary.services.freshrss.json;
import java.util.List;
public class FreshRSSFeeds {
private List<FreshRSSFeed> subscriptions;
public List<FreshRSSFeed> getSubscriptions() {
return subscriptions;
}
public void setSubscriptions(List<FreshRSSFeed> subscriptions) {
this.subscriptions = subscriptions;
}
}

View File

@ -0,0 +1,108 @@
package com.readrops.readropslibrary.services.freshrss.json;
import java.util.List;
public class FreshRSSItem {
private List<FreshRSSAlternate> alternate;
private String author;
private List<String> categories;
private String crawlTimeMsec;
private String id;
private FreshRSSOrigin origin;
private Long published;
private FreshRSSSummary summary;
private String timestampUsec;
private String title;
public List<FreshRSSAlternate> getAlternate() {
return alternate;
}
public void setAlternate(List<FreshRSSAlternate> alternate) {
this.alternate = alternate;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public List<String> getCategories() {
return categories;
}
public void setCategories(List<String> categories) {
this.categories = categories;
}
public String getCrawlTimeMsec() {
return crawlTimeMsec;
}
public void setCrawlTimeMsec(String crawlTimeMsec) {
this.crawlTimeMsec = crawlTimeMsec;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public FreshRSSOrigin getOrigin() {
return origin;
}
public void setOrigin(FreshRSSOrigin origin) {
this.origin = origin;
}
public Long getPublished() {
return published;
}
public void setPublished(Long published) {
this.published = published;
}
public FreshRSSSummary getSummary() {
return summary;
}
public void setSummary(FreshRSSSummary summary) {
this.summary = summary;
}
public String getTimestampUsec() {
return timestampUsec;
}
public void setTimestampUsec(String timestampUsec) {
this.timestampUsec = timestampUsec;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@ -0,0 +1,36 @@
package com.readrops.readropslibrary.services.freshrss.json;
import java.util.List;
public class FreshRSSItems {
private String id;
private Long updated;
private List<FreshRSSItem> items;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Long getUpdated() {
return updated;
}
public void setUpdated(Long updated) {
this.updated = updated;
}
public List<FreshRSSItem> getItems() {
return items;
}
public void setItems(List<FreshRSSItem> items) {
this.items = items;
}
}

View File

@ -0,0 +1,26 @@
package com.readrops.readropslibrary.services.freshrss.json;
public class FreshRSSOrigin {
private String streamId;
private String title;
public String getStreamId() {
return streamId;
}
public void setStreamId(String streamId) {
this.streamId = streamId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@ -0,0 +1,16 @@
package com.readrops.readropslibrary.services.freshrss.json;
public class FreshRSSSummary {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

View File

@ -6,6 +6,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.readropslibrary.services.API;
import com.readrops.readropslibrary.services.SyncType;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolder;
@ -218,11 +219,6 @@ public class NextNewsAPI extends API<NextNewsService> {
return false;
}
public enum SyncType {
INITIAL_SYNC,
CLASSIC_SYNC
}
public enum StateType {
READ,
UNREAD,