Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Include the app URL when printing discounts
Browse files Browse the repository at this point in the history
  • Loading branch information
rg3 committed Dec 20, 2017
1 parent 8b379ca commit 7efdc53
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions steam_discounts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
import HTMLParser
import htmlentitydefs
import itertools
import re

class Entry(object):
def __init__(self):
self.title = u''
self.orig_price = u''
self.discount = u''
self.price = u''
self.url = u''

# Entries can be sorted by title.
def __cmp__(self, other):
return cmp(self.title.lower(), other.title.lower())

class DiscountsParser(HTMLParser.HTMLParser):
def __init__(self):
self.url_re_ = re.compile(
r'^(http://store\.steampowered\.com/(?:app|sub)/\d+/)')
HTMLParser.HTMLParser.__init__(self)

@staticmethod
Expand All @@ -45,10 +49,15 @@ class DiscountsParser(HTMLParser.HTMLParser):
def handle_starttag(self, tag, attrs):
attrs_map = dict(attrs)

if tag == 'a':
match = self.url_re_.search(attrs_map.get('href', ''))
if match is not None:
# First field to extract, hence new entry.
self.current_entry_ = Entry()
self.current_entry_.url += match.group(1)

if tag == 'h4':
# First field to extract, hence new entry.
self.in_h4_ = True
self.current_entry_ = Entry()

elif tag == 'div':
if attrs_map.get('class', '') == 'tab_discount discount_pct':
Expand Down Expand Up @@ -170,13 +179,14 @@ if __name__ == '__main__':
# Compose output.
output = 'Listing %d discounts.\n' % (obtained, )
for entry in sorted(itertools.chain(*batches)):
output += (u'%s ...%s %7s [%7s %s]\n' %
output += (u'%s ...%s %7s [%7s %s] -- %s\n' %
(
entry.title[:TITLE_WIDTH],
'.' * (TITLE_WIDTH - len(entry.title)),
entry.price,
entry.orig_price,
entry.discount,
entry.url,
)
).encode('utf-8')

Expand Down

0 comments on commit 7efdc53

Please sign in to comment.