Skip to content

Commit

Permalink
saving results to postgresql db
Browse files Browse the repository at this point in the history
  • Loading branch information
bertop89 committed Sep 24, 2016
1 parent 06c37e5 commit cf8e7b3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Go script to scrape a popular Spanish website for flat renting. It will send an
```
### RoadMap

- [ ] Save to csv
- [ ] Save to DB
- [X] Save to DB
- [X] Searching across different areas
- [X] Concurrent search
- [X] Filter with more options
Expand Down
33 changes: 33 additions & 0 deletions db/database_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package database_handler

import (
"database/sql"
_ "github.com/lib/pq"
"flat-scraper/flat"
"time"
"fmt"
"log"
)

func FlatInsert(flats []flat.Flat) {
db, err := sql.Open("postgres", "user=postgres password='postgres' dbname=flats")
if err != nil {
log.Fatal(err)
}

err = db.Ping()
if err != nil {
log.Fatal("Error: Could not establish a connection with the database")
}

fmt.Printf("Connected to DB \n")

t := time.Now()
t_str := fmt.Sprintf("%d-%02d-%02d",t.Year(), t.Month(), t.Day())
for _,v := range flats {
query := fmt.Sprintf("INSERT INTO flats(id,name,price,rooms,size,store,elevator,link,area,date_published)" +
" VALUES (%d,'%s',%d,%d,%d,%d,%t,'%s','%s','%s');", v.Id, v.Name, v.Price, v.Rooms, v.Size, v.Store,
v.Elevator, v.Link, v.Area, t_str)
db.QueryRow(query)
}
}
Binary file modified main
Binary file not shown.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"flat-scraper/flat"
"flat-scraper/utils"
"flat-scraper/db"
"fmt"
"sync"
"bytes"
Expand All @@ -23,7 +24,6 @@ func FlatScraper(c utils.Configuration) []flat.Flat {
for _, area := range c.Areas {
go func(area string) {
fmt.Println(area+" starts")
//defer wg.Done()
var resultList []flat.Flat
doc, err := goquery.NewDocument("https://www.idealista.com/alquiler-viviendas/madrid/"+area+"/con-pisos,estado_buen-estado,amueblado_amueblados/")
if err != nil {
Expand Down Expand Up @@ -95,6 +95,8 @@ func main() {
resultList := FlatScraper(config)
filteredList := Filter(resultList, config)

database_handler.FlatInsert(resultList)

filteredLength := strconv.Itoa(len(filteredList))
var buffer bytes.Buffer
for _,v := range filteredList {
Expand Down
12 changes: 12 additions & 0 deletions sql/create_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE flats (
id integer CONSTRAINT firstkey PRIMARY KEY,
name varchar(150) NOT NULL,
price integer NOT NULL,
rooms integer NOT NULL,
size integer NOT NULL,
store integer NOT NULL,
elevator boolean NOT NULL,
link varchar NOT NULL,
area varchar(50) NOT NULL,
date_published date NOT NULL
);

0 comments on commit cf8e7b3

Please sign in to comment.