private String generatePageLink(ChannelHistory.Message message) { String link = message.getPageLink(); if (link.contains("youtube")) { return link; } if (link.contains("?")) { int startPositionOfParams = link.indexOf('?'); link = link.substring(0, startPositionOfParams); } return link; }
private String generateText(ChannelHistory.Message message) { String text = message.getText() .replaceAll("<h", "h") .replaceAll("/>", "") .replaceAll(">", "") .replaceAll("\\?(.*)", "") .replaceAll("(http|https)://(.*)", ""); if (text.isEmpty()) { return "I was so excited by this #enews, I didn't have time to write anything. Check it out!"; } return text; }
private static ChannelHistory.Message convert(ApiPagedChannelHistory.ApiMessage apiMessage) throws ConvertException { List<ApiPagedChannelHistory.ApiAttachment> attachments = apiMessage.attachments; String imageUrl = null; if (attachments != null) { imageUrl = attachments.get(0).imageUrl; } String text = apiMessage.text; // Bots send messages with attachments but no text if (text == null) { throw new ConvertException("Cannot convert " + apiMessage, new NullPointerException("text was null")); } String pageLink = ""; Matcher matcher = urlPattern.matcher(text); while (matcher.find()) { int matchStart = matcher.start(1); int matchEnd = matcher.end(); // now you have the offsets of a URL match pageLink = text.substring(matchStart, matchEnd); } return new ChannelHistory.Message(text, imageUrl, pageLink); }
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 String generateImageUrl(ChannelHistory.Message message) { if (message.hasImage()) { return message.getImageUrl(); } else { return missingImageFetcher.getMissingImageUrl(); } }
private String generateTitle(ChannelHistory.Message message) { try { String pageLink = message.getPageLink(); if (pageLink.startsWith("www")) { pageLink = "http:// " + pageLink; } return linkUnfurler.unfurl(pageLink).getTitle(); } catch (IOException | UnsupportedUrlTypeException e) { return "#eNews link"; } }