1- from interactions import Extension , slash_command , SlashContext , OptionType , slash_option
2- import requests
1+ from interactions import Extension , slash_command , SlashContext , OptionType , slash_option , Client
2+ from interactions .ext .paginators import Paginator
3+ import logging
4+ import datetime as dt
35import utils .html_parser as html_parser
6+ import utils .embeds as embeds
7+ import utils .pullnsave as pullnsave
48
59
610class Commands (Extension ):
11+ # Extension init
12+ def __init__ (self , client : Client ):
13+ self .client = client
714
815
916 # User-invoked command to manually search for make
@@ -20,46 +27,94 @@ class Commands(Extension):
2027 required = False ,
2128 opt_type = OptionType .STRING
2229 )
23- async def search (self , ctx : SlashContext , make : str , model : str = "" ):
30+ @slash_option (
31+ name = "year" ,
32+ description = "Model year of vehicle to search" ,
33+ required = False ,
34+ opt_type = OptionType .INTEGER
35+ )
36+ async def search (self , ctx : SlashContext , make : str , model : str = None , year : int = None ):
37+ logging .info (f'COMMANDS-SEARCH: Command Invoked! args: { make } { model } { year } ' )
38+
2439 # Defer response to give time for request
2540 await ctx .defer ()
2641
27- if model == "" :
28- model = "0"
42+ # Query pullnsave site
43+ data = pullnsave .search_vehicles (make , model , year )
44+ if not data :
45+ logging .error ('COMMANDS-SEARCH: pullnsave returned no data, exiting...' )
46+ await ctx .send ('pullnsave returned no data' )
47+ return
48+
49+ # Parse site data
50+ all_listings = html_parser .parse_listings (data )
51+ if not all_listings :
52+ logging .info ('COMMANDS-SEARCH: no results, exiting...' )
53+ await ctx .send ('No results found' )
54+ return
55+
56+ # Filter listings
57+ filtered_listings = html_parser .filter_listings (all_listings )
2958
30- # Make request
31- r = requests .post (
32- url = 'https://pullnsave.com/wp-admin/admin-ajax.php' ,
33- headers = {
34- 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' ,
35- 'host' : 'pullnsave.com' ,
36- 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/112.0'
37- },
38- data = f"makes={ make } &models={ model } &years=0&store=0&beginDate=&endDate=&action=getVehicles"
39- )
59+ # Build embeds
60+ listing_embeds = []
61+ for listing in filtered_listings :
62+ listing_embeds .append (embeds .vehicle_listing (listing ))
63+
64+ # Send paginated resposne
65+ paginator = Paginator .create_from_embeds (self .client , * listing_embeds )
66+ await paginator .send (ctx )
67+ logging .info ('COMMANDS-SEARCH: paginated results sent!' )
68+
69+
70+ # User-invoked command to get new stock
71+ @slash_command (name = "new_stock" , description = "Get all latest arrivals" )
72+ async def new_stock (self , ctx : SlashContext ):
73+ logging .info ('COMMANDS-NEW_STOCK: Command Invoked!' )
74+
75+ # Defer response to give time for requests
76+ await ctx .defer ()
4077
41- if r .status_code == 200 :
42- all_listings = html_parser .parse_listings (r .text )
43- if all_listings :
44- filtered_listings = html_parser .filter_listings (all_listings )
45- message = ""
46- for listing in filtered_listings :
47- message += f"""
48- **Model Year:** { listing ['model_year' ]}
49- **Model:** { listing ['model' ]}
50- **Color:** { listing ['color' ]}
51- **Location:** { listing ['location' ]}
52- **Row:** { listing ['row' ]}
53- **Date Recieved:** { listing ['date_recieved' ].strftime ("%A %B %m %Y" )}
54- **Image:** { listing ['image' ]}
78+ # Query pullnsave for all makes
79+ make_data = pullnsave .get_makes ()
80+ if not make_data :
81+ logging .error ('COMMANDS-NEW_STOCK: pullnsave returned no data, exiting...' )
82+ await ctx .send ("Couldn't grab make data" )
83+ return
84+
85+ # Parse make data
86+ all_makes = html_parser .parse_makes (make_data )
87+ if not all_makes :
88+ logging .error ('COMMANDS-NEW_STOCK: make parse error, exiting...' )
89+ await ctx .send ("Couldn't parse make data" )
90+ return
91+
92+ # Grab all recent listings
93+ all_listings = []
94+ for make in all_makes :
95+ data = pullnsave .search_vehicles (make , begin_date = (dt .datetime .now () - dt .timedelta (days = 3 )).strftime ('%Y-%m-%d' ))
96+ if not data :
97+ logging .error ('COMMANDS-NEW_STOCK: pullnsave returned no data, skipping...' )
98+ continue
99+ listings = html_parser .parse_listings (data )
100+ if not listings :
101+ logging .error ('COMMANDS-NEW_STOCK: pullnsave returned no data, skipping...' )
102+ continue
103+ for listing in listings :
104+ all_listings .append (listing )
105+
106+ # Filter and sort listings
107+ filtered_listings = html_parser .filter_listings (all_listings )
55108
56- """
57- await ctx .send (message )
58- else :
59- await ctx .send (f"No listings found for Make: { make } Model: { model } " )
60- else :
61- await ctx .send (f"Unhandled request exception: { r .status_code } " )
62-
109+ # Build embeds
110+ listing_embeds = []
111+ for listing in filtered_listings :
112+ listing_embeds .append (embeds .vehicle_listing (listing ))
113+
114+ # Send paginated resposne
115+ paginator = Paginator .create_from_embeds (self .client , * listing_embeds )
116+ await paginator .send (ctx )
117+ logging .info ('COMMANDS-SEARCH: paginated results sent!' )
63118
64119
65120def setup (bot ):
0 commit comments