├── README.md ├── storage └── storage.go ├── LICENSE ├── main.go └── cl ├── search.go └── city.go /README.md: -------------------------------------------------------------------------------- 1 | # multicraig 2 | -------------------------------------------------------------------------------- /storage/storage.go: -------------------------------------------------------------------------------- 1 | package storage 2 | 3 | import "sync" 4 | 5 | type Store interface { 6 | Set(key string, val interface{}) error 7 | Get(key string) (interface{}, error) 8 | Del(key string) error 9 | } 10 | 11 | func NewStore() Store { 12 | return newMemory() 13 | } 14 | 15 | func newMemory() *memory { 16 | return &memory{ 17 | m: make(map[string]interface{}), 18 | } 19 | } 20 | 21 | type memory struct { 22 | m map[string]interface{} 23 | sync.RWMutex 24 | } 25 | 26 | func (m *memory) Set(key string, val interface{}) error { 27 | m.Lock() 28 | defer m.Unlock() 29 | m.m[key] = val 30 | return nil 31 | } 32 | 33 | func (m *memory) Get(key string) (interface{}, error) { 34 | m.RLock() 35 | defer m.RUnlock() 36 | return m.m[key], nil 37 | } 38 | 39 | func (m *memory) Del(key string) error { 40 | m.Lock() 41 | defer m.Unlock() 42 | delete(m.m, key) 43 | return nil 44 | } 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ian Lozinski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "net/http" 7 | "strconv" 8 | 9 | "github.com/i/multicraig/cl" 10 | ) 11 | 12 | func main() { 13 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 14 | fmt.Fprintf(w, "ok") 15 | }) 16 | 17 | http.HandleFunc("/cities", func(w http.ResponseWriter, r *http.Request) { 18 | cities, err := cl.GetCities() 19 | if err != nil { 20 | fmt.Fprint(w, err) 21 | return 22 | } 23 | buf, err := json.Marshal(cities) 24 | if err != nil { 25 | fmt.Fprint(w, err) 26 | return 27 | } 28 | 29 | w.Write(buf) 30 | }) 31 | 32 | http.HandleFunc("/closeCities", func(w http.ResponseWriter, r *http.Request) { 33 | distanceMi, err := strconv.ParseFloat(r.FormValue("distanceMi"), 64) 34 | if err != nil { 35 | fmt.Fprint(w, err.Error()) 36 | return 37 | } 38 | 39 | city, err := cl.GetCity(r.FormValue("city")) 40 | if err != nil { 41 | fmt.Fprint(w, err.Error()) 42 | return 43 | } 44 | cities := city.CitiesWithin(distanceMi) 45 | buf, err := json.Marshal(cities) 46 | if err != nil { 47 | fmt.Fprint(w, err.Error()) 48 | return 49 | } 50 | w.Write(buf) 51 | }) 52 | 53 | http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { 54 | city := r.FormValue("city") 55 | query := r.FormValue("query") 56 | category := r.FormValue("category") 57 | if category == "" { 58 | category = "sss" 59 | } 60 | distanceMi, err := strconv.ParseFloat(r.FormValue("distanceMi"), 64) 61 | if err != nil { 62 | distanceMi = 0 63 | } 64 | 65 | results, err := cl.Search(city, category, query, distanceMi) 66 | if err != nil { 67 | fmt.Fprintf(w, err.Error()) 68 | return 69 | } 70 | 71 | buf, err := json.Marshal(results) 72 | if err != nil { 73 | w.WriteHeader(http.StatusInternalServerError) 74 | fmt.Fprint(w, err.Error()) 75 | } 76 | 77 | w.Write(buf) 78 | }) 79 | 80 | http.ListenAndServe("localhost:3000", nil) 81 | } 82 | -------------------------------------------------------------------------------- /cl/search.go: -------------------------------------------------------------------------------- 1 | package cl 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "sync" 7 | "time" 8 | 9 | "github.com/SlyMarbo/rss" 10 | ) 11 | 12 | const ( 13 | jpgType = "image/jpeg" 14 | searchCityStr = "%s/search/%s?format=rss&query=%s" 15 | ) 16 | 17 | func init() { 18 | rss.CacheParsedItemIDs(false) 19 | } 20 | 21 | func Search(cityName, category, query string, distanceMI float64) ([]SearchResult, error) { 22 | city, err := GetCity(cityName) 23 | if err != nil { 24 | return nil, err 25 | } 26 | 27 | cities := append([]City{city}, city.CitiesWithin(distanceMI)...) 28 | results := make([]SearchResult, 0) 29 | 30 | var wg sync.WaitGroup 31 | var m sync.Mutex 32 | for _, city := range cities { 33 | wg.Add(1) 34 | go func() { 35 | defer wg.Done() 36 | posts, err := city.Search(category, query) 37 | if err != nil { 38 | log.Print(err) 39 | return 40 | } 41 | m.Lock() 42 | results = append(results, SearchResult{City: city, Posts: posts}) 43 | m.Unlock() 44 | }() 45 | } 46 | wg.Wait() 47 | 48 | return results, nil 49 | } 50 | 51 | type Post struct { 52 | Title string 53 | URL string 54 | Date time.Time 55 | Image string 56 | } 57 | 58 | type SearchResult struct { 59 | City City 60 | Posts []Post 61 | } 62 | 63 | func (c City) Search(category, query string) ([]Post, error) { 64 | feed, err := rss.Fetch(fmt.Sprintf(searchCityStr, c.URL, category, query)) 65 | if err != nil { 66 | fmt.Println(fmt.Sprintf(searchCityStr, c.URL, category, query)) 67 | return nil, err 68 | } 69 | 70 | results := make([]Post, 0) 71 | for _, item := range feed.Items { 72 | results = append(results, newPost(item)) 73 | } 74 | return results, nil 75 | } 76 | 77 | func newPost(item *rss.Item) Post { 78 | post := Post{ 79 | Title: item.Title, 80 | Date: item.Date, 81 | URL: item.Link, 82 | } 83 | for _, e := range item.Enclosures { 84 | if e.Type == jpgType { 85 | post.Image = e.Url 86 | break 87 | } 88 | } 89 | return post 90 | } 91 | -------------------------------------------------------------------------------- /cl/city.go: -------------------------------------------------------------------------------- 1 | package cl 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "log" 8 | "math" 9 | "net/http" 10 | "net/url" 11 | "os" 12 | "regexp" 13 | "strings" 14 | "sync" 15 | 16 | "github.com/i/jdog" 17 | "github.com/i/multicraig/storage" 18 | ) 19 | 20 | const ( 21 | kmtomiles = float64(0.621371192) 22 | earthRadius = float64(6371) 23 | clCityURL = "http://www.craigslist.org/about/sites" 24 | cityLocationURL = "https://maps.googleapis.com/maps/api/geocode/json?address=%s,+%s&key=%s" 25 | ) 26 | 27 | var ( 28 | //cityRE = regexp.MustCompile(`