forked from prncc/steam-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.py
152 lines (127 loc) · 4.03 KB
/
items.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
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
from datetime import datetime, date
import logging
import scrapy
from scrapy.loader import ItemLoader
from scrapy.loader.processors import Compose, Join, MapCompose, TakeFirst
logger = logging.getLogger(__name__)
class StripText:
def __init__(self, chars=' \r\t\n'):
self.chars = chars
def __call__(self, value):
try:
return value.strip(self.chars)
except: # noqa E722
return value
def simplify_recommended(x):
return True if x == 'Recommended' else False
def standardize_date(x):
"""
Convert x from recognized input formats to desired output format,
or leave unchanged if input format is not recognized.
"""
fmt_fail = False
for fmt in ['%b %d, %Y', '%B %d, %Y']:
try:
return datetime.strptime(x, fmt).strftime('%Y-%m-%d')
except ValueError:
fmt_fail = True
# Induce year to current year if it is missing.
for fmt in ['%b %d', '%B %d']:
try:
d = datetime.strptime(x, fmt)
d = d.replace(year=date.today().year)
return d.strftime('%Y-%m-%d')
except ValueError:
fmt_fail = True
if fmt_fail:
logger.debug(f'Could not process date {x}')
return x
def str_to_float(x):
x = x.replace(',', '')
try:
return float(x)
except: # noqa E722
return x
def str_to_int(x):
try:
return int(str_to_float(x))
except: # noqa E722
return x
class ProductItem(scrapy.Item):
url = scrapy.Field()
id = scrapy.Field()
app_name = scrapy.Field()
reviews_url = scrapy.Field()
title = scrapy.Field()
genres = scrapy.Field(
output_processor=Compose(TakeFirst(), lambda x: x.split(','), MapCompose(StripText()))
)
developer = scrapy.Field()
publisher = scrapy.Field()
release_date = scrapy.Field(
output_processor=Compose(TakeFirst(), StripText(), standardize_date)
)
specs = scrapy.Field(
output_processor=MapCompose(StripText())
)
tags = scrapy.Field(
output_processor=MapCompose(StripText())
)
price = scrapy.Field(
output_processor=Compose(TakeFirst(),
StripText(chars=' $\n\t\r'),
str_to_float)
)
discount_price = scrapy.Field(
output_processor=Compose(TakeFirst(),
StripText(chars=' $\n\t\r'),
str_to_float)
)
sentiment = scrapy.Field()
n_reviews = scrapy.Field(
output_processor=Compose(
MapCompose(StripText(), lambda x: x.replace(',', ''), str_to_int),
max
)
)
metascore = scrapy.Field(
output_processor=Compose(TakeFirst(), StripText(), str_to_int)
)
early_access = scrapy.Field()
class ReviewItem(scrapy.Item):
product_id = scrapy.Field()
page = scrapy.Field()
page_order = scrapy.Field()
recommended = scrapy.Field(
output_processor=Compose(TakeFirst(), simplify_recommended),
)
date = scrapy.Field(
output_processor=Compose(TakeFirst(), standardize_date)
)
text = scrapy.Field(
input_processor=MapCompose(StripText()),
output_processor=Compose(Join('\n'), StripText())
)
hours = scrapy.Field(
output_processor=Compose(TakeFirst(), str_to_float)
)
found_helpful = scrapy.Field(
output_processor=Compose(TakeFirst(), str_to_int)
)
found_unhelpful = scrapy.Field(
output_processor=Compose(TakeFirst(), str_to_int)
)
found_funny = scrapy.Field(
output_processor=Compose(TakeFirst(), str_to_int)
)
compensation = scrapy.Field()
username = scrapy.Field()
user_id = scrapy.Field()
products = scrapy.Field(
output_processor=Compose(TakeFirst(), str_to_int)
)
early_access = scrapy.Field()
class ProductItemLoader(ItemLoader):
default_output_processor = Compose(TakeFirst(), StripText())
class ReviewItemLoader(ItemLoader):
default_output_processor = TakeFirst()