Exemplo n.º 1
0
def post():
    form = PostForm()
    if request.method == 'GET':
        if request.args.get('isbn'):
            form.isbn.data = request.args.get('isbn')
    if form.validate_on_submit():
        isbn = form.isbn.data.strip().replace('-','').replace(' ','')
        price = form.price.data
        if price == '':
            price = None
        cond = form.condition.data
        comments = form.comments.data
        courses = form.courses.data.strip().replace(' ','').upper()
        if len(courses) > 9:
            courses = ''
        if not Book.query.get(isbn):
            info = get_amazon_info(isbn)
            image = get_amazon_image(isbn)
            b = Book(isbn=isbn, title=info['title'], author=info['author'], amazon_url=info['url'], image=image, courses=[courses])
            add_commit(db, b)
        else:
            b = Book.query.get(isbn)
            old_courses = list(b.courses)
            old_courses.append(courses)
            b.courses = list(set(old_courses))
            db.session.commit()
        p = Post(uid=current_user.id, timestamp=datetime.utcnow(), isbn=isbn, price=price,condition=cond,comments=comments)
        add_commit(db, p)
        email_subbers(p)
        return redirect(url_for('book',isbn=isbn))
    return render_template('post.html',
                           post_form = form)
Exemplo n.º 2
0
def main():
    args = parser.parse_args()
    client = redis.from_url(url=os.environ.get('REDIS_URL', 'redis://localhost:6379/0'))
    with requests.Session() as session:
        # Fetch all expanded legal sets
        names = args.sets
        params = {}
        if names:
            params['name'] = ','.join(names)
        if args.standard:
            params['standardLegal'] = 'true'
        else:
            params['expandedLegal'] = 'true'
        sets = session.get(url='https://api.pokemontcg.io/v1/sets', params=params).json()
        for info in sets['sets']:
            print('Fetching set: {name}'.format(**info))
            if info['name'] == 'Generations':
                # Account for the Radiant collection in the total count
                info['totalCards'] -= 32
            # Get all cards for this set
            params = {'set': info['name'], 'pageSize': '200'}
            if args.cards:
                params['number'] = '|'.join(args.cards)
            cards = session.get(
                url='https://api.pokemontcg.io/v1/cards',
                params=params).json()
            for card in cards['cards']:
                details = {}
                try:
                    int(card['number'])
                    set_number = '{}/{}'.format(card['number'], info['totalCards'])
                except ValueError:
                    set_number = card['number']
                card['setNumber'] = set_number
                if not client.hget(card['id'], 'asin') or not args.missing:
                    # Lookup ASIN from Amazon
                    details.update(get_amazon_info(card, session=session))
                else:
                    print('Already have ASIN for {name}'.format(**card))
                # Copy info from card dict
                for name in ('id', 'name', 'imageUrl', 'subtype', 'supertype', 'setNumber',
                             'number', 'rarity', 'series', 'set', 'setCode'):
                    details[name] = card[name]
                client.hmset(card['id'], details)
                # Sleep to prevent rate limits on the API
                time.sleep(0.8 if not args.missing else 0.25)
Exemplo n.º 3
0
def info(isbn):
    isbn = isbn.strip().replace('-', '').replace(' ', '')
    if len(isbn) != 13 or not isbn.isdigit():
        return jsonify(data=None)
    i = Book.query.get(isbn)
    if i:
        return jsonify(title=i.title,
                       image=i.image,
                       author=i.author,
                       courses=i.courses)
    time.sleep(1)  # dont send requests too fast to not spam amazon
    img = get_amazon_image(isbn)
    time.sleep(1)
    info = get_amazon_info(isbn)
    if info:
        return jsonify(title=info['title'], image=img, author=info['author'])
    return jsonify(title=None)
Exemplo n.º 4
0
def post():
    form = PostForm()
    if request.method == 'GET':
        if request.args.get('isbn'):
            form.isbn.data = request.args.get('isbn')
    if form.validate_on_submit():
        isbn = form.isbn.data.strip().replace('-', '').replace(' ', '')
        price = form.price.data
        if price == '':
            price = None
        cond = form.condition.data
        comments = form.comments.data
        courses = form.courses.data.strip().replace(' ', '').upper()
        if len(courses) > 9:
            courses = ''
        if not Book.query.get(isbn):
            info = get_amazon_info(isbn)
            image = get_amazon_image(isbn)
            b = Book(isbn=isbn,
                     title=info['title'],
                     author=info['author'],
                     amazon_url=info['url'],
                     image=image,
                     courses=[courses])
            add_commit(db, b)
        else:
            b = Book.query.get(isbn)
            old_courses = list(b.courses)
            old_courses.append(courses)
            b.courses = list(set(old_courses))
            db.session.commit()
        p = Post(uid=current_user.id,
                 timestamp=datetime.utcnow(),
                 isbn=isbn,
                 price=price,
                 condition=cond,
                 comments=comments)
        add_commit(db, p)
        email_subbers(p)
        return redirect(url_for('book', isbn=isbn))
    return render_template('post.html', post_form=form)
Exemplo n.º 5
0
def info(isbn):
    isbn = isbn.strip().replace('-','').replace(' ','')
    if len(isbn) != 13 or not isbn.isdigit():
        return jsonify(data=None)
    i = Book.query.get(isbn)
    if i:
        return jsonify(
            title=i.title,
            image=i.image,
            author=i.author,
            courses=i.courses)
    time.sleep(1) # dont send requests too fast to not spam amazon
    img = get_amazon_image(isbn)
    time.sleep(1)
    info = get_amazon_info(isbn)
    if info:
        return jsonify(
            title=info['title'],
            image=img,
            author=info['author'])
    return jsonify(title=None)