Skip to content

Commit

Permalink
URL Helpers for top filters
Browse files Browse the repository at this point in the history
  • Loading branch information
adriens committed Apr 11, 2021
1 parent 8933a86 commit 30191fa
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.adriens.imgflip</groupId>
<artifactId>imgflip4j</artifactId>
<version>1.4</version>
<version>1.5</version>
<packaging>jar</packaging>
<description>A Java SDK to interact with Imgflip API.</description>
<properties>
Expand Down Expand Up @@ -46,5 +46,10 @@
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
*/
package com.github.adriens.imgflip.sdk.imgflip.sdk;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;

/**
* A set of stativ helpers around Imgflip Urls
* @author 3004SAL
Expand All @@ -29,12 +37,12 @@ public static String getPagePath(String inStream, int page) {
return out;
}
public static String getPagePath(String inStream) {
return getPagePath(inStream, null);
return getPagePath(inStream, 1);
}
public static String getPagePath() {
return getPagePath("fun");
}
public static String getPagePath(String inStream, String sortOption) {
public static String getPagePath(String inStream, String sortOption, int page) {
String out = "";
if (inStream == null || inStream.isEmpty()) {
out += "/m/" + HOTTEST_STREAMS.FUN.url();
Expand All @@ -46,6 +54,85 @@ public static String getPagePath(String inStream, String sortOption) {
} else {
out += "?sort=" + sortOption;
}
out += "&page=" + page;
return out;
}

//////////////////////////////////////////////////////////////////////
// Top like queries
public static String getPathOfTop(String inStream, String sortOption, int page){
String out = IMGFLIP_ROOT_URL;
//ex: https://imgflip.com/m/fun?sort=top-1d
if (inStream == null || inStream.isEmpty()) {
out += "/m/" + HOTTEST_STREAMS.FUN.url();
} else {
out += "/m/" + inStream;
}
// now put the sort option
out += "?sort=" + sortOption;
out += "&page=" + page;
return out;
}
public static String getPathOfTop(String inStream, String sortOption){
return getPathOfTop(inStream, sortOption, 1);
}
public static String getPathOfTopOneDay(String stream){
return getPathOfTop(stream, "top-1d");
}
public static String getPathOfTopSevendays(String stream){
return getPathOfTop(stream, "top-7d");
}
public static String getPathOfTopThirtyDays(String stream){
return getPathOfTop(stream, "top-30d");
}
public static String getPathOfTopLastMonth(String stream, int page){
//top-2021-03
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);
Date lDate = calendar.getTime();

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
String sortOpt = "top-" + dateFormat.format(lDate);
return getPathOfTop(stream, sortOpt,page);
}
public static String getPathOfTopLastMonth(String stream){
return getPathOfTopLastMonth(stream, 1);
}
public static String getPathOfTopLastYear(String stream, int page){
//top-2021-03
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
Date lDate = calendar.getTime();

DateFormat dateFormat = new SimpleDateFormat("yyyy");
String sortOpt = "top-" + dateFormat.format(lDate);
return getPathOfTop(stream, sortOpt, page);
}
public static String getPathOfTopLastYear(String stream){
return getPathOfTopLastYear(stream, 1);
}
public static String getPathOfTopOfMonthOfYear(String stream, int year, int month, int page){
String sortOpt = "top-" + year + "-" + StringUtils.leftPad(month+"", 2, "0");
return getPathOfTop(stream, sortOpt, page);
}
public static String getPathOfTopOfMonthOfYear(String stream, int year, int month){
return getPathOfTopOfMonthOfYear(stream, year, month, 1);
}
public static String getPathOfTopOfMonthOfYearWithPage(String stream, int year, int page){
String sortOpt = "top-" + year;
return getPathOfTop(stream, sortOpt, page);
}
public static String getPathOfTopOfMonthOfYear(String stream, int year){
return getPathOfTopOfMonthOfYearWithPage(stream, year, 1);
}

public static void main(String[] arg){
System.out.println("Path Fun for Last Month: <" + ImgFlipURLHelper.getPathOfTopLastMonth("fun")+ ">");
System.out.println("Path Fun for Last Year: <" + ImgFlipURLHelper.getPathOfTopLastYear("fun")+ ">");
System.out.println("Path Fun for Februray 2017: <" + ImgFlipURLHelper.getPathOfTopOfMonthOfYear("politics", 2017, 2) + ">");
System.exit(0);
}

}

0 comments on commit 30191fa

Please sign in to comment.