Skip to content

Commit

Permalink
Merge pull request #10 from xzebra/remove-token
Browse files Browse the repository at this point in the history
Remove need to use token
  • Loading branch information
xzebra authored Feb 9, 2021
2 parents 4e8a7ad + e741c64 commit 0b496fd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 105 deletions.
37 changes: 15 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,6 @@
[![GoReportCard](https://goreportcard.com/badge/github.com/xzebra/unizar-calendar?.svg)](https://goreportcard.com/report/github.com/xzebra/unizar-calendar)
[![Docs](https://godoc.org/github.com/xzebra/unizar-calendar?status.svg)](https://godoc.org/github.com/xzebra/unizar-calendar)

## Compile

Compilation is automatic thanks to Go modules. That means you have to
enable modules support by setting the environment variable
`GO111MODULE=on`.

go build

## Setup

First of all, go to [Google Calendar API site](https://developers.google.com/calendar/quickstart/go), create a new project
with the Calendar API enabled, and save the `credentials.json` file in
this project root folder.

After that, compile and run the application. It will ask you to login
and paste the authorization code returned by Google.

Once `token.json` and `credentials.json` are present in project root
folder, you are good to go.

## Usage

You can interact with the CLI by running the executable from a
Expand All @@ -32,11 +12,24 @@ flag `-h`.

$ ./unizar-calendar -h

## Compile

Compilation is automatic thanks to Go modules. That means you have to
enable modules support by setting the environment variable
`GO111MODULE=on`.

go build

## Requirements

- Go 1.14 (or higher).
- Go modules enabled.
- Project with Google Calendar API enabled.
- `credentials.json` and `token.json` generated.

If you are going to use the `webdata` module or `pkg/gcal`, you need
the following:
- Project with [Google Calendar API site](https://developers.google.com/calendar/quickstart/go) enabled.
- Create a Service Account.
- Download JSON credentials of Service Account.
- Set the path to the JSON file in the
`GOOGLE_APPLICATION_CREDENTIALS` environment variable.

34 changes: 31 additions & 3 deletions cmd/uzcalendar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package main
import (
"errors"
"fmt"
"github.com/xzebra/unizar-calendar/internal/exports"
"github.com/xzebra/unizar-calendar/internal/semester"
"github.com/xzebra/unizar-calendar/pkg/schedules"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/xzebra/unizar-calendar/internal/exports"
"github.com/xzebra/unizar-calendar/internal/semester"
"github.com/xzebra/unizar-calendar/pkg/schedules"

"flag"
"log"
)
Expand All @@ -18,6 +21,10 @@ var (
ErrInvalidSemester = errors.New("semester parameter invalid")
)

var (
semesterDataURL = "https://xzebra.github.io/unizar-calendar/data/semester%d.json"
)

func cliUsage() {
fmt.Fprintf(flag.CommandLine.Output(),
"Usage: %s [options] subjectsFile scheduleFile\n\n",
Expand Down Expand Up @@ -86,7 +93,18 @@ func main() {
os.Exit(1)
}

semesterData, err := getSemesterData(semesterNum)
if err != nil {
log.Fatal(err)
}

sem, err := semester.NewSemesterFromData(semesterData)
if err != nil {
log.Fatal(err)
}

data, err := semester.NewData(
sem,
&schedules.SemesterFiles{
Subjects: subjectsFile,
Schedule: scheduleFile,
Expand All @@ -107,3 +125,13 @@ func main() {

log.Print(exports.Export(data, exportType))
}

func getSemesterData(semesterNum int) ([]byte, error) {
resp, err := http.Get(fmt.Sprintf(semesterDataURL, semesterNum))
if err != nil {
return nil, err
}
defer resp.Body.Close()

return ioutil.ReadAll(resp.Body)
}
15 changes: 2 additions & 13 deletions internal/semester/merged.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package semester
import (
"fmt"

"github.com/xzebra/unizar-calendar/pkg/gcal"
"github.com/xzebra/unizar-calendar/pkg/schedules"
)

Expand All @@ -14,25 +13,15 @@ type Data struct {

// Merged is an association between class ids and a list of all
// days when the class should occur.
Merged map[string][]timeRange `json:"-"`
Merged map[string][]timeRange
}

func NewData(files *schedules.SemesterFiles, number int) (*Data, error) {
func NewData(semester *Semester, files *schedules.SemesterFiles, number int) (*Data, error) {
parsed, err := schedules.ParseSemesterFiles(files)
if err != nil {
return nil, err
}

cal, err := gcal.NewGoogleCalendar()
if err != nil {
return nil, err
}

semester, err := NewSemester(cal, number)
if err != nil {
return nil, err
}

s := &Data{
Semester: semester,
Schedule: parsed.Schedule,
Expand Down
7 changes: 7 additions & 0 deletions internal/semester/semester.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package semester

import (
"encoding/json"
"time"

"github.com/xzebra/unizar-calendar/pkg/gcal"
Expand Down Expand Up @@ -79,6 +80,12 @@ func NewSemester(cal *gcal.GoogleCalendar, number int) (semester *Semester, err
return semester, nil
}

func NewSemesterFromData(data []byte) (semester *Semester, err error) {
semester = &Semester{}
err = json.Unmarshal(data, semester)
return
}

type timeRange struct {
Start, End time.Time
}
Expand Down
67 changes: 0 additions & 67 deletions pkg/gcal/token.go

This file was deleted.

0 comments on commit 0b496fd

Please sign in to comment.