aboutsummaryrefslogtreecommitdiff
path: root/find_session.v
blob: c1250a7632eecc99aff9cb67efcc16fb420f7fcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import json
import net.http
import os
import time
import term
import term.ui as tui

struct StatesResponse {
	states []State
}

struct State {
	state_id int
	state_name string
}

struct DistrictResponse {
	districts []District
}

struct District {
	district_id int
	district_name string
}

struct App {
mut:
    tui &tui.Context = 0
    data VaccineAvailability
	is_loading bool
	idx int = 1
}

struct VaccineAvailability {
mut:
	date string
	available_sessions []Sessions
	state_id  int
	district_id int
	state State
	district District
}

struct SessionResponse {
	sessions []Sessions
}

struct Sessions {
	center_id int
	name string
	address	string
	state_name string
	district_name string
	block_name	string
	pincode	int
	from string
	to string
	lat int
	long int
	fee_type string
	session_id string
	date string
	available_capacity int
	available_capacity_dose1 int
	available_capacity_dose2 int
	fee	 string
	allow_all_age bool
	min_age_limit int
	vaccine	string
	slots []string
}

fn event(e &tui.Event, x voidptr) {
    mut app := &App(x)
	if e.typ == .key_down && e.code == .escape {
        exit(0)
    }

	if e.typ == .key_down && e.code == .enter {
		app.idx = app.idx + 1
		mut date_of_vaccine := time.now().add_days(app.idx).get_fmt_date_str(.hyphen,.ddmmyyyy)
		app.data.date = date_of_vaccine
		app.find_session(app.data.district_id, app.data.date)
	}
}

fn frame(x voidptr) {
    mut app := &App(x)
	app.tui.clear()
	//Draw Area
	app.draw_header()
	app.draw_vaccine_availability()
	app.tui.reset()
    app.tui.flush()
}

fn (mut app App) draw_header() {
	app.tui.draw_text(3,2, term.bold(term.bright_magenta("Vaccine Availability on ${app.data.date} | ${app.data.state.state_name},${app.data.district.district_name} | Total Centers : ${app.data.available_sessions.len}")))
	app.tui.horizontal_separator(3)
}

fn (mut app App) draw_vaccine_availability() {
	available_sessions := app.data.available_sessions
	start_x := 3
	mut start_y := 5
	if available_sessions.len == 0 {
		app.tui.draw_text(app.tui.window_width/2 - 10,app.tui.window_height/2, term.black(term.bg_white("No Vaccine Available")))
	} else {
		for session in available_sessions {
			fee := match session.fee_type{
				'Free' {term.green('Free')}
				'Paid' {term.blue(session.fee)  }
				else { 'N/A' }
			}
			vaccine := match session.vaccine {
				'COVISHIELD' {term.bold(term.green(session.vaccine))}
				'COVAXIN' {term.bold(term.yellow(session.vaccine))}
				else {session.vaccine}
			}
			capacity := term.green(session.available_capacity.str())
			app.tui.draw_text(start_x,start_y,"${session.name} | Vaccine: ${vaccine} | Total Available: ${capacity} | Fee: ${fee}")
			start_y += 2
		}
	}
}

fn init(x voidptr) {
	mut app := &App(x)
	app.find_session(app.data.district_id, app.data.date)
}

fn main() {
	setup_states()
}

fn get_states() ?[]State {
	fetch_config := http.FetchConfig{
		header: http.new_header(
				key : .accept_language,
				value: 'en-US'
		)
	}

	url := 'https://cdn-api.co-vin.in/api/v2/admin/location/states'
	resp := http.fetch(url, fetch_config) or {
		term.clear()
		println(term.fail_message('Failed to fetch States from the server'))
		return error('Failed to fetch States from the server')
	}

	states_data := json.decode(StatesResponse, resp.text) or {
		term.clear()
		println(term.fail_message('Failed to decode data from server'))
		return error('Failed to decode data from server')
	}		
	return states_data.states
}

fn get_districts(state_id int) ?[]District {
	fetch_config := http.FetchConfig{
		header: http.new_header(
				key : .accept_language,
				value: 'en-US'
		)
	}

	url := 'https://cdn-api.co-vin.in/api/v2/admin/location/districts/$state_id'
	resp := http.fetch(url, fetch_config) or {
		term.clear()
		println(term.fail_message('Failed to fetch Districts from the server'))
		return error('Failed to fetch Districts from the server')
	}

	district_data := json.decode(DistrictResponse, resp.text) or {
		term.clear()
		println(term.fail_message('Failed to decode data from server'))
		return error('Failed to Decode data from the server')
	}		
	return district_data.districts
}

fn (mut app App) find_session(district_id int,date string) {
	fetch_config := http.FetchConfig{
		header: http.new_header(
				key : .accept_language,
				value: 'en-US'
		)
	}

	url := 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id=$district_id&date=$date'

	resp := http.fetch(url, fetch_config) or {
		println('failed to fetch data from the server')
		return
	}

	session_data := json.decode(SessionResponse, resp.text) or {
		println(term.fail_message('Failed to decode data'))
		return
	}

	app.data.available_sessions = session_data.sessions.filter( it.available_capacity > 0 && it.min_age_limit<45)
}

fn setup_termui(state State, district District) {
	mut app := &App{}
	app.tui = tui.init(
		user_data: app
		event_fn: event
		frame_fn: frame
		hide_cursor: false,
		init_fn: init
	)
	mut date_of_vaccine := time.now().add_days(app.idx).get_fmt_date_str(.hyphen,.ddmmyyyy)
	if os.args.len ==2 {
		date_of_vaccine = os.args[1]
	}
	println("Searching Vaccine Availability on $date_of_vaccine")
	app.data.date = date_of_vaccine
	app.data.state = state
	app.data.district = district
	app.data.state_id = state.state_id
	app.data.district_id = district.district_id
	app.tui.set_window_title("COWIN Vaccine Finder")
	app.tui.run() or {}
}

fn setup_states() {
	term.clear()
    width, height := term.get_terminal_size()
	println(term.header('Available State', '='))
	start_x :=  width/2 - 10
	mut start_y := 5
	mut selected_state := 0
    term.set_cursor_position(x: start_x, y: start_y)

	states := get_states() or {
		return
	}

	for i in 0 .. states.len {
		println(term.bright_green('$i . ${states[i].state_name}'))
		start_y += 1
		term.set_cursor_position(x: start_x, y: start_y)
	}
	term.set_cursor_position(x: 0, y: height)
	
	for {
        if var := os.input_opt('Please select a State: ') {
            if var.int() < 0 || var.int() >= states.len {
                continue
            }
			setup_districts(states[var.int()])
            break
        }
        println('')
        break
    }
	
}

fn setup_districts(state State) {
	term.clear()
    width, height := term.get_terminal_size()
	println(term.header('Available States', '='))
	start_x :=  width/2 - 10
	mut start_y := 5
    term.set_cursor_position(x: start_x, y: start_y)
	districts := get_districts(state.state_id) or {
		return
	}

	for i in 0 .. districts.len {
		println(term.bright_green('$i . ${districts[i].district_name}'))
		start_y += 1
		term.set_cursor_position(x: start_x, y: start_y)
	}
	term.set_cursor_position(x: 0, y: height)
	for {
        if var := os.input_opt('Please select a District: ') {
            if var.int() < 0 || var.int() >= districts.len {
                continue
            }
			setup_termui(state,districts[var.int()])
            break
        }
        println('')
        break
    }
	
}