public static SlackHistoryFetcher from(SlackWebService slackWebService, String slackToken) { HistoryConverter historyConverter = new HistoryConverter(); return new SlackHistoryFetcher(slackWebService, slackToken, CHANNEL_GENERAL, historyConverter); }
@NotNull private Scraper newLiveServiceInstance(String slackToken) { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://slack.com/api/") .addConverterFactory(GsonConverterFactory.create()) .build(); SlackWebService slackWebService = retrofit.create(SlackWebService.class); SlackHistoryFetcher slackHistoryFetcher = SlackHistoryFetcher.from(slackWebService, slackToken); return new Scraper(slackHistoryFetcher); }
public ChannelHistory getChannelHistory(LocalDateTime start, LocalDateTime end) { return historyConverter.convert(getApiChannelHistory(start, end)); }
private void appendChannelHistory(ApiPagedResponse apiPagedResponse) { Response<ApiPagedChannelHistory> response = fetchApiChannelHistoryResponse(apiPagedResponse.getLastResponseEpochTime()); if (response.isSuccessful()) { ApiPagedChannelHistory apiPagedChannelHistory = response.body(); apiPagedResponse.addResponse(apiPagedChannelHistory); } else { throw new IllegalStateException("No internet or server down or something." + " code: " + response.code() + " error: " + response.body()); } }
public Stream<ChannelHistory.Message> scrape(LocalDateTime start, LocalDateTime end) { ChannelHistory channelHistory = slackHistoryFetcher.getChannelHistory(start, end); System.out.println(channelHistory.getHistoryFrom() + " / " + channelHistory.getHistoryTo()); return channelHistory .getMessages() .parallelStream() .map(message -> new ChannelHistory.Message(message.getText() .replace("<#C0YNBKANM|enews>", "#eNews") .replace("<#C1V389HGB|enews>", "#eNews"), message.getImageUrl(), message.getPageLink())) .filter(message -> { String messageText = message.getText().toLowerCase(); return messageText.contains("#enews") && messageText.contains("http"); }); }
private List<ApiPagedChannelHistory> getApiChannelHistory(LocalDateTime start, LocalDateTime end) { ApiPagedResponse apiPagedResponse = new ApiPagedResponse(start); do { appendChannelHistory(apiPagedResponse); } while (apiPagedResponse.responsesAreAfter(end) && apiPagedResponse.hasMoreResponses()); return apiPagedResponse.getResponses(); }
@NotNull private Scraper newMockServiceInsstance(String slackToken) { SlackWebService slackWebService = new MockSlackWebService(); SlackHistoryFetcher slackHistoryFetcher = SlackHistoryFetcher.from(slackWebService, slackToken); return new Scraper(slackHistoryFetcher); }