Skip to content

Commit ae0c25b

Browse files
authored
Merge pull request #2 from Shadi-A/feature/file-output
Feature/file output
2 parents 8b45558 + 68da6ec commit ae0c25b

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

crawl

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
#! /usr/bin/env python
2-
import sys
2+
import argparse
33

4-
from expediacrawler import offers_finder
4+
from expediacrawler import offersFinder
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument("depart_city", help="The city which you are departing from, 3 letter code")
8+
parser.add_argument("destination_city", help="Destination city, 3 letter code")
9+
parser.add_argument("depart_date", help="Departure date, mm/dd/yyyy")
10+
parser.add_argument("return_date", help="Date of return, mm/dd/yyyy")
11+
parser.add_argument("--add", type=int, help="Search also for flights on these following dates, "
12+
"i.e if departing on 07/15 and -add 2 , then search for 07/16 and 07/17")
13+
parser.add_argument("--file", help="Append the results to this csv file")
14+
args = parser.parse_args()
515

6-
orig_city = sys.argv[1]
7-
dest_city = sys.argv[2]
8-
leave = sys.argv[3]
9-
back = sys.argv[4]
1016
flex = 0
11-
if len(sys.argv) == 6:
12-
flex = int(sys.argv[5])
13-
offers_finder.get_offers(orig_city=orig_city, dest_city=dest_city, leave=leave, back=back, forward_days=flex)
17+
if args.add:
18+
flex = args.add
19+
offersFinder.find_offers(orig_city=args.depart_city, dest_city=args.destination_city,
20+
leave=args.depart_date, back=args.return_date, forward_days=flex, results_file=args.file)

expediacrawler/offers_finder.py expediacrawler/offersFinder.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import expediacrawler.soupParser as soupParser
22
import expediacrawler.urlBuilder as urlBuilder
33
from expediacrawler.dateCalculator import calculate_new_dates
4+
from expediacrawler.offersWriter import write_offers
5+
6+
7+
def find_offers(orig_city, dest_city, leave, back, forward_days, results_file):
8+
all_offers = get_offers(orig_city, dest_city, leave, back, forward_days)
9+
write_offers(all_offers, results_file)
410

511

612
def get_offers(orig_city, dest_city, leave, back, forward_days):
@@ -12,11 +18,7 @@ def get_offers(orig_city, dest_city, leave, back, forward_days):
1218
out_str, return_str = calculate_new_dates(leave, back, i)
1319
day_offer = get_day_offers(orig_city, dest_city=dest_city, leave=out_str, back=return_str)
1420
all_offers[out_str + '-' + return_str] = day_offer[0:5]
15-
16-
for day in all_offers:
17-
print(day, ":")
18-
for offer in all_offers[day]:
19-
print(offer)
21+
return all_offers
2022

2123

2224
def get_day_offers(orig_city, dest_city, leave, back):

expediacrawler/offersWriter.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import csv
2+
3+
4+
def write_offers(all_offers, results_file):
5+
if results_file:
6+
append_to_csv(all_offers, results_file)
7+
for day in all_offers:
8+
print(day, ":")
9+
for offer in all_offers[day]:
10+
print(offer)
11+
12+
13+
def append_to_csv(all_offers, file_name):
14+
with open(file_name, 'a') as f:
15+
writer = csv.writer(f)
16+
for day in all_offers:
17+
for offer in all_offers[day]:
18+
offer_info = list(offer.values());
19+
offer_info.append(day)
20+
writer.writerow(offer_info)

0 commit comments

Comments
 (0)