Skip to content

Commit

Permalink
Lazy-load tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
wb9688 committed Mar 19, 2020
1 parent c914021 commit a657d97
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ public static ChannelInfo getInfo(ChannelExtractor extractor) throws ExtractionE
}

List<ChannelTabInfo> tabs = new ArrayList<>();
for (ChannelTabExtractor tab : extractor.getTabs()) {
for (int i = 0; i < extractor.getTabs().size(); i++) {
try {
tab.fetchPage();
ChannelTabInfo tabInfo = ChannelTabInfo.getInfo(tab);
ChannelTabInfo tabInfo = ChannelTabInfo.getInfo(extractor.getTabs().get(i), i == 0);
tabs.add(tabInfo);
} catch (Exception e) {
info.addError(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ChannelTabInfo(int serviceId, String id, String url, String originalUrl,
super(serviceId, id, url, originalUrl, name, listLinkHandler.getContentFilters(), listLinkHandler.getSortFilter());
}

public static ListExtractor.InfoItemsPage<InfoItem> getMoreItems(ChannelTabInfo tabInfo, String pageUrl) throws IOException, ExtractionException {
public static void assureChannelTabExtractor(ChannelTabInfo tabInfo) throws ExtractionException, IOException {
if (tabInfo.getChannelTabExtractor() == null) {
ChannelExtractor channelExtractor = NewPipe.getServiceByUrl(tabInfo.getUrl()).getChannelExtractor(tabInfo.getUrl());
channelExtractor.fetchPage();
Expand All @@ -28,6 +28,10 @@ public static ListExtractor.InfoItemsPage<InfoItem> getMoreItems(ChannelTabInfo
}
}
}
}

public static ListExtractor.InfoItemsPage<InfoItem> getMoreItems(ChannelTabInfo tabInfo, String pageUrl) throws IOException, ExtractionException {
assureChannelTabExtractor(tabInfo);

return tabInfo.getChannelTabExtractor().getPage(pageUrl);
}
Expand All @@ -43,13 +47,32 @@ public static ChannelTabInfo getInfo(ChannelTabExtractor extractor) throws Extra

info.setChannelTabExtractor(extractor);

final ListExtractor.InfoItemsPage<InfoItem> itemsPage = ExtractorHelper.getItemsPageOrLogError(info, extractor);
info.setRelatedItems(itemsPage.getItems());
info.setNextPageUrl(itemsPage.getNextPageUrl());

return info;
}

public static ChannelTabInfo getInfo(ChannelTabExtractor extractor, boolean usedForFeed) throws ExtractionException {
ChannelTabInfo channelTabInfo = getInfo(extractor);
channelTabInfo.setUsedForFeed(usedForFeed);
return channelTabInfo;
}

private boolean isLoaded = false;

public ChannelTabInfo loadTab() throws IOException, ExtractionException {
if (isLoaded) return this;

assureChannelTabExtractor(this);
this.getChannelTabExtractor().fetchPage();

final ListExtractor.InfoItemsPage<InfoItem> itemsPage = ExtractorHelper.getItemsPageOrLogError(this, this.getChannelTabExtractor());
this.setRelatedItems(itemsPage.getItems());
this.setNextPageUrl(itemsPage.getNextPageUrl());

isLoaded = true;

return this;
}

private transient ChannelTabExtractor channelTabExtractor;

public ChannelTabExtractor getChannelTabExtractor() {
Expand All @@ -59,4 +82,14 @@ public ChannelTabExtractor getChannelTabExtractor() {
public void setChannelTabExtractor(ChannelTabExtractor channelTabExtractor) {
this.channelTabExtractor = channelTabExtractor;
}

private boolean usedForFeed = false;

public boolean isUsedForFeed() {
return usedForFeed;
}

public void setUsedForFeed(boolean usedForFeed) {
this.usedForFeed = usedForFeed;
}
}

0 comments on commit a657d97

Please sign in to comment.