Skip to content

Instantly share code, notes, and snippets.

@badalsarkar

badalsarkar/lab6.diff

Last active Nov 6, 2020
Embed
What would you like to do?
OSD600- Lab6: Using Blink to check URLs Telescope's latest posts
diff --git a/src/main/java/com/badalsarkar/App.java b/src/main/java/com/badalsarkar/App.java
index 4b3dca8..ba5c239 100644
--- a/src/main/java/com/badalsarkar/App.java
+++ b/src/main/java/com/badalsarkar/App.java
@@ -1,7 +1,10 @@
package com.badalsarkar;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.util.List;
+import java.util.regex.Pattern;
/**
* Main class.
@@ -33,6 +36,21 @@ public class App {
Cli.printVersion(appVersion);
}
+ if(Cli.isSet(Cli.in)) {
+ configureUrlPrinter();
+ try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
+ while(true) {
+ String text = null;
+ if((text = reader.readLine())!= null) {
+ Checker.check(Extractor.extractUrlFromText(text, Pattern.compile(pattern)), urlPrinter);
+ }
+ else {
+ break;
+ }
+ }
+ }
+ System.exit(0);
+ }
if(Cli.isSet(Cli.source)) {
configureUrlPrinter();
processFile(Cli.getCliOptionArgValue(Cli.source), Cli.getCliOptionArgValue(Cli.destination), urlPrinter);
diff --git a/src/main/java/com/badalsarkar/Cli.java b/src/main/java/com/badalsarkar/Cli.java
index fdf0c47..ae53d09 100644
--- a/src/main/java/com/badalsarkar/Cli.java
+++ b/src/main/java/com/badalsarkar/Cli.java
@@ -18,6 +18,7 @@ public final class Cli {
public static final CliOption all;
public static final CliOption good;
public static final CliOption bad;
+ public static final CliOption in;
static {
help = new CliOption("h", "help", "Show app usage help.");
@@ -28,6 +29,7 @@ public final class Cli {
all = new CliOption("a", "all", "Print all URLS.");
good = new CliOption("g", "good", "Print only good URL.");
bad = new CliOption("b", "bad", "Print only bad URLs.");
+ in = new CliOption("i", "in", "Accept text input from standard input");
}
/**
@@ -46,6 +48,7 @@ public final class Cli {
cliParser.registerCliOption(help);
cliParser.registerCliOption(version);
+ cliParser.registerCliOption(in);
cliParser.defineCliOptionWithArgument(source);
cliParser.defineCliOptionWithArgument(destination);
/*
diff --git a/src/main/java/com/badalsarkar/Extractor.java b/src/main/java/com/badalsarkar/Extractor.java
new file mode 100644
index 0000000..6073dc8
--- /dev/null
+++ b/src/main/java/com/badalsarkar/Extractor.java
@@ -0,0 +1,22 @@
+package com.badalsarkar;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class Extractor {
+
+ public static List<String> extractUrlFromText(String text, Pattern pattern){
+ int startPos = 0;
+ int endPos = 0;
+ List<String> url = new ArrayList<String>();
+ Matcher matcher = pattern.matcher(text);
+ while (matcher.find()) {
+ startPos = matcher.start();
+ endPos = matcher.end();
+ url.add(text.substring(startPos, endPos));
+ }
+ return url;
+ }
+}
#!/usr/bin/env bash
if ! command -v jq &> /dev/null
then
echo "JQ is not installed."
echo "Please install jq by sudo apt install jq."
exit
fi
if [ ! -f ./Blink.jar ]; then
echo "Could not find Blink. Download from https://github.com/badalsarkar/Blink"
exit
fi
telescopeLatestPostApi=http://localhost:3000/posts
data=$(curl -s $telescopeLatestPostApi | jq '.[] | .id')
for i in $data;
do
i="${i%\"}"
i="${i#\"}"
echo
echo ====================================================
echo Checking $telescopeLatestPostApi/$i
echo ====================================================
echo
curl -s $telescopeLatestPostApi/$i | java -jar ./Blink.jar -i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment