Last active
November 26, 2020 03:28
-
-
Save HenryZerocool/e015248e5d18218e5beaf3e13755afa8 to your computer and use it in GitHub Desktop.
Changes needed for lab6 - working with telescope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/henzcli/__main__.py b/henzcli/__main__.py | |
index ad9ecc3..b4623d2 100644 | |
--- a/henzcli/__main__.py | |
+++ b/henzcli/__main__.py | |
@@ -1,7 +1,7 @@ | |
import requests | |
import sys | |
import os | |
from bs4 import BeautifulSoup | |
@@ -35,18 +35,35 @@ def main(): | |
if (args[0] == "--good"): | |
onlyGood = True | |
allIncluded = False | |
- else if (args[0] == "--bad"): | |
+ elif (args[0] == "--bad"): | |
onlyBad = True | |
allIncluded = False | |
- else if (args[0] == "--all"): | |
+ elif (args[0] == "--all"): | |
allIncluded = True | |
if (args[0] == "-v" or args[0] == "--version"): | |
print("HenZCLI version 0.1") | |
print('passed argument :: {}'.format(args)) | |
for arg in args: | |
if (arg[0] != "-"): | |
+ if (arg == "telescope"): | |
+ # Get request http://localhost:3000/posts/ | |
+ r = requests.get('http://localhost:3000/posts') | |
+ # parse result as JSON object | |
+ jsonTele = r.json() | |
+ # reading through each url in JSON objects | |
+ text = "" | |
+ for link in jsonTele: | |
+ # GET request to each post | |
+ r = requests.get('http://localhost:3000' + link["url"]) | |
+ text += r.json()["html"] | |
+ # parse/ammend all of them into 1 file | |
+ f = open("telescope.html", 'a') | |
+ f.write(text) | |
try: | |
- f = open(arg, "r") | |
+ if (arg == "telescope"): | |
+ f = open("telescope.html", "r") | |
+ else: | |
+ f = open(arg, "r") | |
if (CLICOLOR): | |
print(purpleColor + "In file " + arg + noColor) | |
else: | |
@@ -56,33 +73,36 @@ def main(): | |
# look for all link in a tags | |
for link in html.find_all('a'): | |
URL = link.get('href').strip() | |
- try: | |
- # test links | |
- requestObj = requests.get(URL) | |
- if ((onlyBad or allIncluded) and requestObj.status_code == 404 or requestObj.status_code == 401): | |
- if (CLICOLOR): | |
- print(redColor + "Bad link " + URL + noColor) | |
- else: | |
- print("Bad link " + URL) | |
- elif ((onlyGood or allIncluded) and requestObj.status_code == 200): | |
- if (CLICOLOR): | |
- print(greenColor + "Good link " + URL + noColor) | |
+ if (URL.find("http") == 0): | |
+ try: | |
+ # test links | |
+ requestObj = requests.get(URL) | |
+ if ((onlyBad or allIncluded) and requestObj.status_code == 404 or requestObj.status_code == 401): | |
+ if (CLICOLOR): | |
+ print(redColor + "Bad link " + URL + noColor) | |
+ else: | |
+ print("Bad link " + URL) | |
+ elif ((onlyGood or allIncluded) and requestObj.status_code == 200): | |
+ if (CLICOLOR): | |
+ print(greenColor + "Good link " + URL + noColor) | |
+ else: | |
+ print("Good link " + URL) | |
else: | |
- print("Good link " + URL) | |
- else: | |
+ if (allIncluded): | |
+ if (CLICOLOR): | |
+ print(dgrayColor + "Unknown "+ URL + noColor) | |
+ else: | |
+ print("Unknown "+ URL) | |
+ except: | |
if (allIncluded): | |
if (CLICOLOR): | |
print(dgrayColor + "Unknown "+ URL + noColor) | |
else: | |
print("Unknown "+ URL) | |
- except: | |
- if (allIncluded): | |
- if (CLICOLOR): | |
- print(dgrayColor + "Unknown "+ URL + noColor) | |
- else: | |
- print("Unknown "+ URL) | |
except: | |
print(dgrayColor + "Unable to open file " + arg) | |
- | |
+ # clean up created file | |
+ if os.path.exists("telescope.html"): | |
+ os.remove("telescope.html") | |
if __name__ == '__main__': | |
main() | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment