Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature implement database #7

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migration file added, ticket company table insert query updated
raselahmed2k7 committed Dec 15, 2021
commit 442d72d48eeb4f6dba2491231e022ee274a77144
41 changes: 41 additions & 0 deletions migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'sqlite3'

db = SQLite3::Database.open "db_tour_scraper.db"

db.execute <<SQL
CREATE TABLE IF NOT EXISTS tickets_summary (
id INTEGER PRIMARY KEY AUTOINCREMENT,
departure_date Date,
return_date Date,
time_from_out VARCHAR(20),
time_to_out VARCHAR(20),
search_time DateTime,
total_tickets_out INTEGER,
total_tickets_in INTEGER
);
SQL

db.execute <<SQL
CREATE TABLE IF NOT EXISTS tickets_airline_companies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ticket_summary_id INTEGER,
airline_company_name VARCHAR(100),
ticket_lowest_price FLOAT,
total_flights_available INTEGER,
ticket_type VARCHAR(10),
FOREIGN KEY(ticket_summary_id) REFERENCES tickets_summary(id)
);
SQL

db.execute <<SQL
CREATE TABLE IF NOT EXISTS airline_flights (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ticket_airline_id INTEGER,
flight_code VARCHAR(50),
flight_price INTEGER,
flight_changable_status VARCHAR(50),
flight_ticket_type VARCHAR(100),
FOREIGN KEY(ticket_airline_id) REFERENCES tickets_airlines(id)
);
SQL

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove extra new line

4 changes: 2 additions & 2 deletions tour_site_scraper.rb
Original file line number Diff line number Diff line change
@@ -96,14 +96,14 @@ def save_scrap_data(tickets_out_lists, tickets_in_lists, departure_date, return_
DB.execute("INSERT INTO tickets_summary values(?, ?, ?, ?, ?, ?, ?, ? )", [nil, departure_date.to_s, return_date.to_s, TIME_FROM_OUT, TIME_TO_OUT, Time.now.strftime("%Y-%m-%d %H:%M:%S"), total_ticket_out_found, total_ticket_in_found])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please limit to 80 character per line for all. Ref: Ruby Style Guide

ticket_summary_id = DB.last_insert_row_id()
all_ticket_out_lists.each do |tickets_out|
DB.execute("INSERT INTO tickets_airline_companies values(?, ?, ?, ?, ?, ?)", [nil, DB.last_insert_row_id(), tickets_out[:ticket_company_name], tickets_out[:ticket_minimum_price], tickets_out[:number_of_ticket_found], 'round_trip'])
DB.execute("INSERT INTO tickets_airline_companies values(?, ?, ?, ?, ?, ?)", [nil, DB.last_insert_row_id(), tickets_out[:ticket_company_name], tickets_out[:ticket_minimum_price], tickets_out[:number_of_ticket_found], 'out'])
tickets_out[:ticket_flight_lists].each do |flight|
DB.execute("INSERT INTO airline_flights values(?, ?, ?, ?, ?, ?)", [nil, DB.last_insert_row_id(), flight['flight_code'], flight['flight_price'], flight['flight_changable_status'], flight['flight_type']])
end
end

all_ticket_in_lists.each do |tickets_in|
DB.execute("INSERT INTO tickets_airline_companies values(?, ?, ?, ?, ?, ?)", [nil, ticket_summary_id, tickets_in[:ticket_company_name], tickets_in[:ticket_minimum_price], tickets_in[:number_of_ticket_found], 'round_trip'])
DB.execute("INSERT INTO tickets_airline_companies values(?, ?, ?, ?, ?, ?)", [nil, ticket_summary_id, tickets_in[:ticket_company_name], tickets_in[:ticket_minimum_price], tickets_in[:number_of_ticket_found], 'in'])
tickets_in[:ticket_flight_lists].each do |flight|
DB.execute("INSERT INTO airline_flights values(?, ?, ?, ?, ?, ?)", [nil, DB.last_insert_row_id(), flight['flight_code'], flight['flight_price'], flight['flight_changable_status'], flight['flight_type']])
end