Skip to content

Commit

Permalink
Add support for fee preference (#26)
Browse files Browse the repository at this point in the history
* Add fee preference

* Advance fee check, use common function for preferences

* Update README.md

* Remove dev logs
  • Loading branch information
shreyas-sriram authored May 11, 2021
1 parent c1894bf commit bb924b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Flags:
-a, --age int Search appointment for age
-d, --district string Search by district name
-e, --email string Email address to send notifications
-f, --fee string Fee preferences - free (or) paid. Default: No preference
-h, --help help for covaccine-notifier
-i, --interval int Interval to repeat the search. Default: (60) second
-p, --password string Email ID password for auth
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
pinCode, state, district, email, password, date, vaccine string
pinCode, state, district, email, password, date, vaccine, fee string

age, interval int

Expand All @@ -34,11 +34,15 @@ const (
emailPasswordEnv = "EMAIL_PASSOWORD"
searchIntervalEnv = "SEARCH_INTERVAL"
vaccineEnv = "VACCINE"
feeEnv = "FEE"

defaultSearchInterval = 60

covishield = "covishield"
covaxin = "covaxin"

free = "free"
paid = "paid"
)

func init() {
Expand All @@ -50,6 +54,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&password, "password", "p", os.Getenv(emailPasswordEnv), "Email ID password for auth")
rootCmd.PersistentFlags().IntVarP(&interval, "interval", "i", getIntEnv(searchIntervalEnv), fmt.Sprintf("Interval to repeat the search. Default: (%v) second", defaultSearchInterval))
rootCmd.PersistentFlags().StringVarP(&vaccine, "vaccine", "v", os.Getenv(vaccineEnv), fmt.Sprintf("Vaccine preferences - covishield (or) covaxin. Default: No preference"))
rootCmd.PersistentFlags().StringVarP(&fee, "fee", "f", os.Getenv(feeEnv), fmt.Sprintf("Fee preferences - free (or) paid. Default: No preference"))
}

// Execute executes the main command
Expand Down Expand Up @@ -78,6 +83,9 @@ func checkFlags() error {
if !(vaccine == "" || vaccine == covishield || vaccine == covaxin) {
return errors.New("Invalid vaccine, please use covaxin or covishield")
}
if !(fee == "" || fee == free || fee == paid) {
return errors.New("Invalid fee preference, please use free or paid")
}
return nil
}

Expand Down
20 changes: 9 additions & 11 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,13 @@ func searchByStateDistrict(age int, state, district string) error {
return getAvailableSessions(response, age)
}

// isPreferredVaccineAvailable checks for availability of preferred vaccine
func isPreferredVaccineAvailable(current, preference string) bool {
switch preference {
case "":
// isPreferredAvailable checks for availability of preferences
func isPreferredAvailable(current, preference string) bool {
if preference == "" {
return true
case covishield:
return strings.ToLower(current) == covishield
case covaxin:
return strings.ToLower(current) == covaxin
} else {
return strings.ToLower(current) == preference
}

return false
}

func getAvailableSessions(response []byte, age int) error {
Expand All @@ -207,8 +202,11 @@ func getAvailableSessions(response []byte, age int) error {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 1, 8, 1, '\t', 0)
for _, center := range appnts.Centers {
if !isPreferredAvailable(center.FeeType, fee) {
continue
}
for _, s := range center.Sessions {
if s.MinAgeLimit <= age && s.AvailableCapacity != 0 && isPreferredVaccineAvailable(s.Vaccine, vaccine) {
if s.MinAgeLimit <= age && s.AvailableCapacity != 0 && isPreferredAvailable(s.Vaccine, vaccine) {
fmt.Fprintln(w, fmt.Sprintf("Center\t%s", center.Name))
fmt.Fprintln(w, fmt.Sprintf("State\t%s", center.StateName))
fmt.Fprintln(w, fmt.Sprintf("District\t%s", center.DistrictName))
Expand Down

0 comments on commit bb924b4

Please sign in to comment.