Skip to content

Commit

Permalink
Working query formulation with gorilla and proper yaml unmarshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
samling committed May 1, 2018
1 parent 253a60d commit 19dee21
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 72 deletions.
34 changes: 22 additions & 12 deletions Config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
Search:
URL: "https://santabarbara.craigslist.org/search/apa"
Scheme: "https"
Location: "santabarbara"
URL: "craigslist.org/search/apa"


SMTP:
host: test
port: test2
user: test3
pass:
host: hostname
port: 123
user: username
pass: password

Query:
#------ SEARCH OPTIONS ------#

# Search result format? (rss=RSS, (blank)=html)
format: rss

# Listing has photos? (0=no, 1=yes)
hasPic: 1

Expand Down Expand Up @@ -38,7 +44,7 @@ Query:
postedToday: 0

# Open house date (all+dates=any day, 2018-05-01=May 1, 2018)
sale_date: 2018-05-10
sale_date: all+dates

# Availability (0=all dates, 1=within 30 days, 2=beyond 30 days)
availabilityMode: 0
Expand All @@ -56,10 +62,12 @@ Query:
# Search nearby areas? (0=no, 1=yes)
#searchNearby: 0

# Nearby areas to search (requires searchNearby=1; comma-separated integers)
# Nearby areas to search (requires searchNearby=1; list of integers)
# 7=los angeles
# 208=ventura county
nearbyAreas: [7 208]
nearbyAreas:
- 7
- 208



Expand Down Expand Up @@ -95,7 +103,7 @@ Query:
# Wellchair access? (0=no, 1=yes)
#wheelchaccess: 0

# Housing type (comma-separated integers)
# Housing type (list of integers)
# 1=apartment
# 2=condo
# 3=cottage/cabin
Expand All @@ -108,17 +116,19 @@ Query:
# 10=manufactured
# 11=assisted living
# 12=land
housing_type: 1
housing_type:
- 1
- 2

# Laundry (comma-separated integers)
# Laundry (list of integers)
# 1=w/d in unit
# 2=w/d hookups
# 3=laundry in building
# 4=laundry on site
# 5=no laundry on site
#laundry: #

# Parking (comma-separated integers)
# Parking (list of integers)
# 1=carport
# 2=attached garage
# 3=detached garage
Expand Down
Binary file modified main
Binary file not shown.
Binary file added pkg/linux_amd64/github.com/gorilla/Schema.a
Binary file not shown.
Binary file added pkg/linux_amd64/github.com/gorilla/schema.a
Binary file not shown.
1 change: 1 addition & 0 deletions src/github.com/gorilla/Schema
Submodule Schema added at d0e4c2
1 change: 1 addition & 0 deletions src/github.com/gorilla/schema
Submodule schema added at d0e4c2
111 changes: 51 additions & 60 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,61 @@ import (
"io/ioutil"
"log"
"net/url"
//"strings"

"github.com/gorilla/Schema"
"gopkg.in/yaml.v2"
)

type Config struct {
Search struct {
URL string `yaml:"URL"`
Scheme string `yaml:"Scheme"`
Location string `yaml:"Location"`
URL string `yaml:"URL"`
} `yaml:"Search"`

SMTP struct {
Host string `yaml:"host,omitempty"`
Port string `yaml:"port,omitempty"`
User string `yaml:"user,omitempty"`
Pass string `yaml:"pass,omitempty"`
} `yaml:"SMTP,omitempty"`
Host string `yaml:"host,omitempty"`
Port string `yaml:"port,omitempty"`
User string `yaml:"user,omitempty"`
Pass string `yaml:"pass,omitempty"`
} `yaml:"SMTP"`

Query struct {
HasPic string `yaml:"hasPic"`
} `yaml:"Query,omitempty"`
Format string `yaml:"format,omitempty"`
HasPic string `yaml:"hasPic,omitempty"`
SrchType string `yaml:"srchType,omitempty"`
BundleDuplicates string `yaml:"bundleDuplicates,omitempty"`
MinPrice string `yaml:"min_price,omitempty"`
MaxPrice string `yaml:"max_price,omitempty"`
PostedToday string `yaml:"postedToday,omitempty"`
SaleDate string `yaml:"sale_date,omitempty"`
AvailabilityMode string `yaml:"availabilityMode,omitempty"`
SearchDistance string `yaml:"search_distance,omitempty"`
Postal string `yaml:"postal,omitempty"`
SearchNearby string `yaml:"searchNearby,omitempty"`
NearbyAreas []string `yaml:"nearbyAreas,omitempty"`
MinBedrooms string `yaml:"min_bedrooms,omitempty"`
MaxBedrooms string `yaml:"max_bedrooms,omitempty"`
MinBathrooms string `yaml:"min_bathrooms,omitempty"`
MaxBathrooms string `yaml:"max_bathrooms,omitempty"`
MinSqft string `yaml:"minSqft,omitempty"`
MaxSqft string `yaml:"maxSqft,omitempty"`
PetsCat string `yaml:"pets_cat,omitempty"`
PetsDog string `yaml:"pets_dog,omitempty"`
IsFurnished string `yaml:"is_furnished,omitempty"`
Wheelchaccess string `yaml:"wheelchaccess,omitempty"`
HousingType []string `yaml:"housing_type,omitempty"`
Laundry []string `yaml:"laundry,omitempty"`
Parking []string `yaml:"parking,omitempty"`
} `yaml:"Query"`
}

func main() {
c := Config{}
c.getConf()
fmt.Println(c)
//m := getConfigMap()
createQueryString(c)
//fmt.Println(q)

q := createQueryString(c)
fmt.Println(q)
}

func (c *Config) getConf() *Config {
Expand All @@ -46,58 +72,23 @@ func (c *Config) getConf() *Config {
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
fmt.Printf("%v", c)

return c
}

//func getConfigMap() map[string]map[string]string {
// // Since the nested values are also a map, need to do some mapception
// m := make(map[string]map[string]string)
//
// yamlFile, err := ioutil.ReadFile("Config.yaml")
// if err != nil {
// log.Printf("yamlFile.Get err #%s ", err)
// }
//
// // Dynamically unmarshal key/value pairs into our map of string maps
// err = yaml.Unmarshal(yamlFile, m)
// if err != nil {
// log.Fatal(err)
// }
//
// return m
//}

func createQueryString(c Config) *url.URL {
u, err := url.Parse(c.Search.URL)
if err != nil {
log.Fatal(err)
}
u := new(url.URL)
host := c.Search.Location + "." + c.Search.URL
u.Scheme = c.Search.Scheme
u.Host = host

form := url.Values{}

encoder := schema.NewEncoder()
encoder.SetAliasTag("yaml")
encoder.Encode(c.Query, form)

u.RawQuery = form.Encode()

// Create an empty query string that we Set() key/value pairs on; results in "key=value&key=value&..."
// q := u.Query()
// for k, v := range m["Query"] {
// // TODO: Handle sequence of values so that they result in a series of duplicate keys, e.g. housing=1&housing=2&housing=3
// if strings.Contains(v, ",") { // Tried giving values in YAML file like: "housing: 1,2"
// s := strings.Split(v, ",")
// for i, j := range s {
// q.Set(k, j)
// fmt.Println(i)
// fmt.Println(j)
// // Guess there'd be a 'continue' here or something
// }
// fmt.Println(k)
// fmt.Println(v)
// }
// q.Set(k, v) // But what to do about this if it's a sequence...
// // Would rather set value in config like:
// // housing: [1 2]
// // Problem is map explicitly contains strings, so results in: "line 63: cannot unmarshal !!seq into string"
// // Tried changing map to map[string]map[string]interface{} but then it gets messy with type conversions etc.
// }
//
// u.RawQuery = q.Encode()
//
return u
}

0 comments on commit 19dee21

Please sign in to comment.