-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgold_price.py
57 lines (47 loc) · 1.83 KB
/
gold_price.py
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
from bs4 import BeautifulSoup
import requests
from csv import writer
import datetime
import statistics
from pathlib import Path
# Example data: 1 Gold = US $0.059
DIR = '/home/pi/wowc_price_tracker/'
def append_list_as_row(file_name, list_of_elem):
my_file = Path(file_name)
if my_file.is_file():
with open(file_name, 'a+', newline='') as write_obj:
csv_writer = writer(write_obj)
csv_writer.writerow(list_of_elem)
else:
with open(file_name, 'a+', newline='') as write_obj:
csv_writer = writer(write_obj)
if len(list_of_elem) == 4:
csv_writer.writerow(['datetime', 'avg', 'min', 'max'])
csv_writer.writerow(list_of_elem)
def get_price_data(url, out_file_name):
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
found = soup.find_all(class_="products__exch-rate input-gold")
out_list = []
price_list = []
raw_list = []
now = str(datetime.datetime.now())
out_list.append(now)
raw_list.append(now)
for i in found:
# print(i.span['data-ppu'])
price_list.append(float(i.span['data-ppu']))
mean = statistics.mean(price_list)
my_min = min(price_list)
my_max = max(price_list)
out_list.append(mean)
out_list.append(my_min)
out_list.append(my_max)
raw_list.append(price_list)
append_list_as_row(DIR + out_file_name + '.csv', out_list)
append_list_as_row(DIR + out_file_name + '_raw.csv', raw_list)
if __name__ == "__main__":
ef_horde = "https://www.g2g.com/wow-classic-tbc/gold-29076-29077?&faction=41398&platform=41202&sorting=lowest_price"
ef_ally = "https://www.g2g.com/wow-classic-tbc/gold-29076-29077?&faction=41398&platform=41200&sorting=lowest_price"
get_price_data(ef_horde, 'ef_horde')
get_price_data(ef_ally, 'ef_ally')