Skip to content

Instantly share code, notes, and snippets.

@shidarin
Last active February 4, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shidarin/2ac2022c9b6d600a686df8898bdd4d43 to your computer and use it in GitHub Desktop.
Save shidarin/2ac2022c9b6d600a686df8898bdd4d43 to your computer and use it in GitHub Desktop.
Auto build for sale posts for posters from ExpressoBeans.
#!/usr/bin/env python
"""Auto build for sale posts for posters from ExpressoBeans.
Save your Trades page as an HTML file, and put the file location
you saved it at in FILE_PATH below. Right now this will only work on one
page of trades, so if you're selling a billion items this won't
work for you.
At the bottom of the script, put markdown(), facebook() or bbcode()
depending on where you are posting the posters.
Python 2.7, although I ran into enough unicode BS that I _almost_
wished I coded it in 3.
License
=======
The MIT License (MIT)
expressocash
Copyright (c) 2017 Sean Wallitsch
https://gist.github.com/shidarin/2ac2022c9b6d600a686df8898bdd4d43
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from bs4 import BeautifulSoup
import re
FILE_PATH = '../sample_data/shidarin_trade_list.htm'
URL_BASE = 'http://www.expressobeans.com'
ENTRY_BBCODE = u"""[url={url}][size=115]{title} - {year}[/size]
[img]{image_url}[/img][/url]
[b]${price}[/b]"""
ENTRY_FACEBOOK = u"{title} - ${price}"
ENTRY_MARKDOWN = u"""[{title} - {artist}]({url})
![{title}]({image_url})
${price}
"""
class Item(object):
def __init__(self, item):
self._item = item
self.image_url = self.absolute_url(self._get_image_url())
self.url = self.absolute_url(self._get_item_url())
self.price = self._get_price()
self.title = self._get_title()
self.year = self._get_year()
self.artist = self._get_artist()
def _get_artist(self):
itemInfo_links = self._item.find_all('a', attrs={'class': 'itemInfo'})
elems = [
elem for elem in
itemInfo_links if ['Artist' in elem['href'] or 'lblCondition' in elem['href']]
]
artist = elems[0].text
if artist == 'VG':
long = elems[0].parent.text
long = long.replace(u'\xa0', u' ') # remove no break spaces
artist_search = re.search(r'.+\d{4} (.+?)VG', long)
artist = artist_search.group(1)
return artist
def _get_image_url(self):
return self._item.find('img')['src'].replace('&t=sm', '')
def _get_item_url(self):
return self._item.find(text='View item').parent['href']
def _get_price(self):
possibles = self._item.text.split('$')
if len(possibles) == 1:
return '???'
else:
return possibles[-1]
def _get_title(self):
trade_detail = self._item.find(text='View trade detail')
links = self._item.find_all('a', attrs={'href': trade_detail.parent['href']})
elems = [elem for elem in links if not elem.text == 'View trade detail']
return elems[0].text
def _get_year(self):
elem = self._item.find(text=self.title).parent.parent.parent
year = elem.text.split(u'\xa0')[0]
year_match = re.search(r'.+(\d{4}).*', year)
return year_match.group(1)
@classmethod
def absolute_url(cls, path):
return URL_BASE + path
@property
def bbcode(self):
return ENTRY_BBCODE.format(
url=self.url,
title=self.title,
year=self.year,
image_url=self.image_url,
artist=self.artist,
price=self.price
)
@property
def facebook(self):
return ENTRY_FACEBOOK.format(
title=self.title,
price=self.price
)
@property
def markdown(self):
return ENTRY_MARKDOWN.format(
url=self.url,
title=self.title,
artist=self.artist,
image_url=self.image_url,
price=self.price
)
def _fetch_items(file_path):
with open(file_path, 'r') as f:
html_page = f.read()
soup = BeautifulSoup(html_page, 'html.parser')
elems = soup.find_all('div', attrs={'class': 'itemOutside'})
items = [
Item(elem) for elem in elems
]
items.sort(key=lambda x: x.artist)
return items
def bbcode():
items = _fetch_items(FILE_PATH)
prev_artist = ''
for item in items:
if item.artist != prev_artist:
prev_artist = item.artist
print '\n[size=135]{artist}[/size]\n'.format(artist=item.artist)
print item.bbcode
print '\n'
def facebook():
items = _fetch_items(FILE_PATH)
prev_artist = ''
for item in items:
if item.artist != prev_artist:
prev_artist = item.artist
if len([i for i in items if i.artist == item.artist]) > 1:
print '\n{artist}\n'.format(artist=item.artist)
else:
print ''
print item.facebook
def markdown():
items = _fetch_items(FILE_PATH)
for item in items:
print item.markdown
markdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment