Skip to content

Commit

Permalink
Merge pull request #1 from Abhinav-ark/main
Browse files Browse the repository at this point in the history
Initial Usable Version
  • Loading branch information
Ashrockzzz2003 authored Oct 26, 2024
2 parents 01b2243 + a490285 commit 0b8264e
Show file tree
Hide file tree
Showing 14 changed files with 528 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cmd

const (
BASE_URL = "http://dspace.amritanet.edu:8080"
COURSE_URL = BASE_URL + "/xmlui/handle/123456789/"
COURSE_LIST_URL = COURSE_URL + "16"
)

38 changes: 38 additions & 0 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"fmt"
"log"
"github.com/anaskhan96/soup"
"os/exec"
"runtime"
)

// Fetches and parses HTML from the given URL.
func fetchHTML(url string) (string, error) {
doc, err := soup.Get(url)

if err != nil {
fmt.Println("Error fetching the URL. Make sure you're connected to Amrita WiFi or VPN.")
return "", err
}

return doc, nil
}


// Opens a URL in the default web browser.
func openBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
}
if err != nil {
log.Fatal(err)
}
}
12 changes: 12 additions & 0 deletions cmd/logo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmd

const LOGO_ASCII string = `
__ __ _____ _____ _______ _______ ______
/\ | \/ | __ \|_ _|__ __|/\ | __ \ \ / / __ \
/ \ | \ / | |__) | | | | | / \ | |__) \ \_/ / | | |
/ /\ \ | |\/| | _ / | | | | / /\ \ | ___/ \ /| | | |
/ ____ \| | | | | \ \ _| |_ | |/ ____ \ | | | | | |__| |
/_/ \_\_| |_|_| \_\_____| |_/_/ \_\ |_| |_| \___\_\
`

7 changes: 7 additions & 0 deletions cmd/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cmd

type resource struct {
name string
path string
}

151 changes: 151 additions & 0 deletions cmd/requestClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package cmd

import (
"errors"
"github.com/anaskhan96/soup"
)

var htmlFetchErr error = errors.New("failed to fetch the HTML content")

func getCoursesReq(url string) ([]resource, error) {

res, err := fetchHTML(url)

if err != nil {
return nil, htmlFetchErr
}

doc := soup.HTMLParse(res)
div := doc.Find("div", "id", "aspect_artifactbrowser_CommunityViewer_div_community-view")

subs := div.FindAll("div","class","artifact-title")

var subjects []resource

for _, item := range subs {
sub := item.Find("span")
a := item.Find("a")
path := a.Attrs()["href"]
subject := resource{sub.Text(), path}
subjects = append(subjects, subject)
}

return subjects, nil
}


func semChooseReq(url string) ([]resource ,error) {

res, err := fetchHTML(url)

if err != nil {
return nil, htmlFetchErr
}

doc := soup.HTMLParse(res)
div := doc.Find("div", "id", "aspect_artifactbrowser_CommunityViewer_div_community-view")

if div.Error != nil {
return nil, errors.New("No assesments found on the page.")
}

ul := div.FindAll("ul")
li := ul[0].FindAll("li")

if len(ul)>1 {
li = ul[1].FindAll("li")
} else {
li = ul[0].FindAll("li")
}

var assesments []resource

for _, link := range li {
a := link.Find("a")
span := a.Find("span")
path := link.Find("a").Attrs()["href"]
assesment := resource{span.Text(), path}
assesments = append(assesments, assesment)
}

return assesments, nil
}

func semTableReq(url string) ([]resource, error) {

res, err := fetchHTML(url)

if err != nil {
return nil, htmlFetchErr
}

doc := soup.HTMLParse(res)
div := doc.Find("div", "id", "aspect_artifactbrowser_CommunityViewer_div_community-view")

if div.Error != nil {
return nil, errors.New("No semesters found on the page.")
}

ul := div.Find("ul")
li := ul.FindAll("li")

if len(li) == 0 {
return nil, errors.New("No semesters found on the page.")
}

var semesters []resource

for _, link := range li {
a := link.Find("a")
span := a.Find("span")
path := link.Find("a").Attrs()["href"]
semester := resource{span.Text(), path}
semesters = append(semesters, semester)
}

return semesters, nil

}

func yearReq(url string) ([]resource, error) {

res, err := fetchHTML(url)

if err != nil {
return nil, htmlFetchErr
}

doc := soup.HTMLParse(res)
div := doc.Find("div", "xmlns","http://di.tamu.edu/DRI/1.0/")

ul := div.Find("ul")
li := ul.Find("li")
hyper := li.Find("a").Attrs()["href"]

url = BASE_URL + hyper
page,err := fetchHTML(url)

if err != nil {
return nil, htmlFetchErr
}

doc = soup.HTMLParse(page)
div = doc.Find("div", "class","file-list")

subdiv := div.FindAll("div","class","file-wrapper")

var files []resource

for _, item := range subdiv {
title := item.FindAll("div")
indiv := title[1].Find("div")
span := indiv.FindAll("span")
fileName := span[1].Attrs()["title"]
path := title[0].Find("a").Attrs()["href"]
file := resource{fileName, path}
files = append(files, file)
}

return files, nil

}
64 changes: 64 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "ampyq",
Short: "Amrita PYQ CLI",
Long: `A CLI application to access Amrita Repository for previous year question papers.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(LOGO_ASCII)
start()
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

// start function - equivalent to start() in Python
func start() {
fmt.Println("Fetching Courses...")

subjects, err := getCoursesReq(COURSE_LIST_URL)

if err != nil {
fmt.Errorf(err.Error())
return
}

fmt.Println("Available Courses:")

for i, subject := range subjects {
fmt.Printf("%d.\t%s\n", i+1, subject.name)
}

// Option to quit.
fmt.Printf("%d.\tQuit\n", len(subjects)+1)

for {
var ch int
fmt.Printf("\nEnter your choice: ")
fmt.Scanln(&ch)

if ch > 0 && ch <= len(subjects) {
path := subjects[ch-1].path
url := BASE_URL + path
semTable(url)
} else if ch == len(subjects)+1 {
fmt.Println("Goodbye!")
os.Exit(0)
} else {
fmt.Println("Please enter a valid input!")
}
}
}
46 changes: 46 additions & 0 deletions cmd/semChoose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"
)

func semChoose(url string) {
fmt.Println("Fetching assesments...")
params_url := url

assesments, err := semChooseReq(url)

if err != nil {
fmt.Errorf(err.Error())
return
}

// Display the found items
fmt.Printf("No\tSemesters\n")
for i, assesment := range assesments {
fmt.Printf("%d\t%s\n", i+1, assesment.name) // Extract the text from the span element
}

// Option to add "Back"
fmt.Printf("%d\tBack\n", len(assesments)+1)

for {
var ch int
fmt.Print("\nEnter your assesment: ")
fmt.Scanln(&ch)

if ch > 0 && ch <= len(assesments) {
url = BASE_URL + assesments[ch-1].path
break
} else if ch == len(assesments)+1 {
semTable(stack.Pop())
} else {
fmt.Println("Please enter a valid input!")
}
}

// append to stack
stack.Push(params_url)

year(url)
}
44 changes: 44 additions & 0 deletions cmd/semTable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
)

func semTable(url string) {
fmt.Println("Fetching semesters...")

semesters, err := semTableReq(url)

if err != nil {
fmt.Errorf(err.Error())
return
}

fmt.Printf("No\tSemesters\n")
for i, semester := range semesters {
fmt.Printf("%d\t%s\n", i+1, semester.name)
}

// Option to add "Back".
fmt.Printf("%d\tBack\n", len(semesters)+1)

stack.Push(url)

for {
var ch int
fmt.Print("\nEnter your semester: ")
fmt.Scanln(&ch)

if ch > 0 && ch <= len(semesters) {
url := BASE_URL + semesters[ch-1].path
semChoose(url)
break
} else if ch == len(semesters)+1 {
start()
break
} else {
fmt.Println("Please enter a valid input!")
}
}

}
Loading

0 comments on commit 0b8264e

Please sign in to comment.