Skip to content

Instantly share code, notes, and snippets.

View ddahan's full-sized avatar

David Dahan ddahan

View GitHub Profile
@ddahan
ddahan / prepa-environnement.sh
Created January 2, 2017 15:55
generer-cartes-qr-code-unique-python-1
mkdir qr_card_generator
cd qr_card_generator
virtualenv -p /le/chemin/de/python3 venv # Init du virtualenv en spécifiant le chemin de Python3 (sur mon système, Python 2.7 est par défaut)
source venv/bin/activate # Activation du virtualenv
pip install qrcode pillow # Installation des deux libs nécessaires
touch card-generator.py
def create_UUIDs(qty):
''' Return a list of `qty` UUIDs '''
return [str(uuid4()) for _ in range(qty)]
def create_UUIDs(qty):
''' Return a list of `qty` UUIDs '''
return [str(uuid4()) for _ in range(qty)]
def data_to_qrcode(data):
''' Return a qrcode image from data '''
qrc = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_Q,
box_size=8,
border=0)
qrc.add_data(data)
qrc.make(fit=True)
img = qrc.make_image()
for cpt, elt_id in enumerate(input_ids):
# Copy the background to the new image, then draw it
new_bg = copy(bg_image)
draw = ImageDraw.Draw(new_bg)
# Generate a QR code as image
qr_image = data_to_qrcode(elt_id)
qr_x, qr_y = qr_image.size # Get width/height of QR code image
def page_to_index(page_num):
''' Transforms page number into start index to be written in Yelp URL '''
return (page_num - 1)*10
def build_yelp_url(page, c):
''' Builds Yelp URL for the given page and cflt to be parsed according to
config variables '''
url = "http://www.yelp.fr/search?&start={0}".format(page_to_index(page))
if CITY: url += "&find_loc={0}".format(CITY)
url += "&cflt={0}".format(c) # We assume that CFLTS list is not empty
if PARIS_DISTRICTS: url += "&l=p:FR-75:Paris::{0}".format(
build_arglist(PARIS_DISTRICTS))
search_results = soup.find_all('div', attrs={"class":u"search-result"}):
for sr in search_results:
shop_name = sr.find('a', attrs={"class":u"biz-name"}).get_text()
def is_advertisement(search_result):
''' Return True is the search result is an add '''
if search_result.find('span', attrs={"class":u"yloca-tip"}):
return True
return False