Skip to content

Commit

Permalink
feat: repository in publish-ready state
Browse files Browse the repository at this point in the history
  • Loading branch information
brittonhayes committed Aug 20, 2023
1 parent 9471d33 commit 2811521
Show file tree
Hide file tree
Showing 20 changed files with 4,369 additions and 41 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Britton Hayes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 👩‍⚕️ Psych - Find a mental health professional

Meet **Psych**, a Go application that allows you to find therapists from psychologytoday.com using a more powerful search engine. This tool provides various functionalities, including web scraping, browsing therapists in the terminal, and running a GraphQL API server to more effectively search for a therapist that meets your needs.

<img src="https://raw.githubusercontent.com/ashleymcnamara/gophers/master/GOPHER_SHARE.png" alt="drawing" width="300"/>

## Features

- **Scraping:** Retrieve therapist information from psychologytoday.com based on various criteria such as state, county, city, or zip code.
- **Browsing:** View therapist information in the terminal in a user-friendly interface.
- **GraphQL API:** Run a GraphQL server to query therapist data programmatically.

## Installation

### Install with Go

```bash
go install github.com/brittonhayes/psych@latest
```

## Usage

Psych provides a set of commands to perform various tasks. Here's a brief overview:

### Scrape

Use the `scrape` command to retrieve therapist information from psychologytoday.com.

```bash
# Retrieve all therapists in the United States in your county
psych scrape --state <state> --county <county>

# Retrieve all therapists in your zip code
psych scrape --zip <zip>

# Retrieve all therapists in your city
psych scrape --city <city> --state <state>
```

Replace `<state>`, `<county>`, `<city>`, and `<zip>` with the desired criteria for searching therapists.

### Browse

Browse therapists in the terminal using the `browse` command.

```bash
psych browse
```

### GraphQL API

Run a GraphQL API server to query therapist data using the `api` command.

```bash
psych api --port <port>
```

Replace `<port>` with the desired port number for the GraphQL server.

### Additional Flags

- Use `-v` or `--verbose` to enable verbose logging.
- Use `-c` or `--config` to specify the configuration directory path.
- Use `--db` to specify the path to the SQLite DB file.

## Configuration

Psych allows you to customize its behavior using command-line flags. You can also modify the application's source code to further customize its behavior according to your needs.

## License

This project is licensed under the [MIT License](LICENSE).

---

Note:** This README provides a high-level overview of the `Psych` tool. For detailed usage instructions and examples, refer to the application's help documentation by running `psych --help` and `psych <command> --help`.

*This project is not affiliated with or endorsed by psychologytoday.com.*
26 changes: 26 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# https://taskfile.dev

version: '3'

tasks:
default:
cmds:
- task: install
silent: true

install:
desc: Install psych tool
cmds:
- go install ./cmd/psych/

test:
desc: Run tests
cmds:
- go test ./...
silent: true

gen:
desc: Generate code
cmds:
- go generate ./...
silent: true
24 changes: 24 additions & 0 deletions api/therapist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package api

type Therapist struct {
ID int `bun:"id,pk,autoincrement" json:"id"`
Title string `json:"title"`
Credentials string `json:"credentials"`
Verified string `json:"verified"`
Statement string `json:"statement"`
Phone string `json:"phone"`
Location string `json:"location"`
Link string `json:"link"`
}

type GetTherapistParams struct {
Title *string `json:"title"`
Credentials *string `json:"credentials"`
Verified *string `json:"verified"`
Statement *string `json:"statement"`
Phone *string `json:"phone"`
Location *string `json:"location"`
Link *string `json:"link"`
Limit *int `json:"limit"`
Offset *int `json:"offset"`
}
57 changes: 55 additions & 2 deletions cmd/psych/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io/fs"
"net/http"
"net/url"
"path/filepath"
"sort"
Expand All @@ -13,13 +14,21 @@ import (

"log/slog"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/brittonhayes/therapy"
"github.com/brittonhayes/therapy/api"
"github.com/brittonhayes/therapy/graph"
"github.com/brittonhayes/therapy/scrape"
"github.com/brittonhayes/therapy/sqlite"
"github.com/brittonhayes/therapy/tui"
"github.com/urfave/cli/v2"
)

const (
ErrNotEnoughFlags = "not enough flags provided to generate web scraping URL"
)

func main() {
var (
repo therapy.Repository
Expand Down Expand Up @@ -152,7 +161,7 @@ func main() {
s := scrape.NewScraper(c.Context, logger, repo)
therapists := s.Scrape(config)

uniqueTherapists := map[string]therapy.Therapist{}
uniqueTherapists := map[string]api.Therapist{}
for _, therapist := range therapists {
uniqueTherapists[therapist.Title] = therapist
}
Expand Down Expand Up @@ -190,6 +199,50 @@ func main() {
return tui.Run(therapists)
},
},
{
Name: "api",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",
Usage: "Port to run the API server on",
Value: "8080",
},
},
Before: func(c *cli.Context) error {
if _, err := os.Stat(c.String("config")); err != nil {
err := os.MkdirAll(c.String("config"), fs.ModePerm)
if err != nil {
return err
}
}

repo = sqlite.NewRepository(c.String("db"), logger)

err := repo.Init(context.Background())
if err != nil {
return err
}

err = repo.Migrate(context.Background())
if err != nil {
return err
}

return nil
},
Action: func(c *cli.Context) error {

srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{
Repo: repo,
}}))

http.Handle("/", playground.ApolloSandboxHandler("GraphQL playground", "/query"))
http.Handle("/query", srv)

logger.InfoContext(c.Context, "connect to url for GraphQL playground", slog.String("url", "http://localhost:"+c.String("port")))
return http.ListenAndServe(":"+c.String("port"), nil)
},
},
}}

sort.Sort(cli.FlagsByName(app.Flags))
Expand Down Expand Up @@ -217,5 +270,5 @@ func buildURL(state string, county string, city string, zip string) (string, err
return url.JoinPath(base, state, city)
}

return "", errors.New("not enough flags provided to generate web scraping URL")
return "", errors.New(ErrNotEnoughFlags)
}
13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/brittonhayes/therapy
go 1.21.0

require (
github.com/99designs/gqlgen v0.17.36
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.7.1
Expand All @@ -11,10 +12,12 @@ require (
github.com/uptrace/bun/dialect/sqlitedialect v1.1.14
github.com/uptrace/bun/driver/sqliteshim v1.1.14
github.com/urfave/cli/v2 v2.25.7
github.com/vektah/gqlparser/v2 v2.5.8
)

require (
github.com/PuerkitoBio/goquery v1.5.1 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/andybalholm/cascadia v1.2.0 // indirect
github.com/antchfx/htmlquery v1.2.3 // indirect
github.com/antchfx/xmlquery v1.2.4 // indirect
Expand All @@ -25,8 +28,10 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
Expand All @@ -35,6 +40,7 @@ require (
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
Expand All @@ -54,9 +60,10 @@ require (
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.1 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.24.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
Expand Down
Loading

0 comments on commit 2811521

Please sign in to comment.