LoginSignup
3
11

More than 5 years have passed since last update.

PythonでAmazonの価格を監視(2)

Posted at

PythonでAmazonの価格を監視(2)

前回に引き続き,Amazonの価格監視システムを構築します.今回は商品の登録,削除の部分を実装します.

関数の定義

早速関数を定義します.

functions

def insert_data(dbname, user_name, page_url, mail_address):
    with closing(sqlite3.connect(dbname)) as connection:
        cursor = connection.cursor()
        price = price_checker.get_price(page_url)
        title = price_checker.get_title(page_url)
        sql = 'insert into users (name, title, url, price, mail) values (?,?,?,?,?)'
        user = (user_name, title, page_url, price, mail_address)
        cursor.execute(sql, user)
        connection.commit()
        connection.close()

# データの削除


def delete_data(dbname, page_url):
    with closing(sqlite3.connect(dbname)) as connection:
        cursor = connection.cursor()
        sql = 'delete from users where url = ? '
        data = (page_url,)
        cursor.execute(sql, data)
        connection.commit()
        connection.close()


# データの取得


def get_data(dbname, user_name):
    list = []
    with closing(sqlite3.connect(dbname)) as connection:
        cursor = connection.cursor()
        select_sql = 'select * from users where name = ? '
        data = (user_name,)
        for row in cursor.execute(select_sql, data):
            list.append(row)
        connection.close()
    return  list

今回はデータベースの管理が主となるためデータの挿入,削除,取得を関数かしておきました.
データの挿入の処理だけ少し特殊になっています.価格と商品名を入力するのは面倒なため,前回使用した関数を用いてWebページから価格と商品名を取得するようにしています.
そのほかには特殊な処理は含まれていません.テーブル名は任意で変更して使用してください.

main関数

ではmain関数もみていきましょう.

price_checker_conf
if __name__ == '__main__':

    print('configを開始します')
    name = input('名前を入力してください:')
    items = get_data('price_checker.db',name)
    if items == []:
        print('はじめまして{}さん'.format(name))

    else:
        print('あなたが登録している商品はこちらです')
        for item in items:
            print('\nitem{}'.format(items.index(item)+1))
            print(item[1])

    while True:
        items = get_data('price_checker.db', name)
        print('\n\n何をしますか?数字で入力してください')
        print('1.登録商品の表示\n'\
              '2.商品の追加\n'\
              '3.商品の削除\n'\
              '4.終了')
        choice = int(input('選択:'))
        if choice == 1:
            if items:
                print('あなたが登録している商品はこちらです')
                for item in items:
                    print('\nitem{}'.format(items.index(item) + 1))
                    print(item[1])
            else:
                print('あなたが登録している商品はありません')

        elif choice == 2:
            url = input('追加する商品のurlを入力してください:')
            if item == []:
                mail_addr = input('メールアドレスを入力してください:')
            else:
                mail_addr = item[0][4]

            insert_data('price_checker.db', name, url, mail_addr)
            print('データを登録しました')

        elif choice == 3:
            for item in items:
                print('\nitem{}'.format(items.index(item) + 1))
                print(item[2])
            print('削除する商品の番号を入力してください')
            index = int(input('番号:')) - 1

            delete_data('price_checker.db', items[index][2])
            print('データを削除しました')

        else:
            break

UIが非常に雑です.コンソールを用いているため見づらいです.いつかきれいなUIにしなきゃなあ...個人で利用する分には何も問題ありません.

 初めにユーザー名を入力してもらい,ユーザーの判別を行います.データベースにユーザー名があるかどうかを判別し,一致するユーザがいる場合はその人が登録している商品一覧を.いない場合には新規のユーザーとして処理します.
 その後,データベースの操作に入ります.ユーザが行える操作は登録している商品の表示,追加,削除です.
 どの処理でも過去のデータを使用するため,まず,ループの初めに商品データ一覧を取得します.
 その後はユーザーの入力によって分岐します.表示はそのままリストを表示します.追加の際にはユーザーのメールアドレスの情報が必要になるため,過去の情報を参照し,適宜ユーザーにメールアドレスを要求します.削除の処理では一度商品一覧を表示し,そのインデックスで指定するようにします.

課題

 今回作成した関数は脆弱すぎます.まずパスワードがない,重複を削除する機能がないなどひどく粗末です.オンラインでサービスを実装するためにはこれらの機能を持たせる必要があります.

以上データベースの管理でした.最後まで読んでいただきありがとうございました。

3
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
11