What I'm going to do now is I'm going to, we're going to build a
little web application that uses cookies to keep track of how many times
you've visited a website. And then I'll show you, you know, how
to use cookies in App Engine and some, some things we need to
keep in, keep in mind, so. This is the kind of template
I've been working from for App Engine stuff. First thing I want to do
is, is just set the content type to text so we don't have
to deal with HTML for right now. Okay, so, I've added this line
here that gets a cookie called visits. The way this
works is we look at the request object, which is always
on self. The request object will have a cookies object, which
is a convenience thing that App Engine did for us, it
parsed our HTP hatters and it threw all of the
cookies into this dictionary-like object and we can call get on
it which you know, distinct from this get, of course. This
is a function we call in dictionaries to check to see
if a key is in the dictionary and if it is, we
get the value of that key. And if it's not, we get the
default which we'll set to zero. What I want to do is I want
to print through our user how many times we visited the site. Okay,
so I added this line to just write to the user you've
been here %s times. Remember our
string substitution? And we'll print visits there.
So let's go ahead and give this a test in our browser. And
I'm, I've loaded my page and it says I've been here zero times.
And if I reload the page over and over, it still says zero
times because we're not doing any counting, we're just printing the number zero
over and over. So let's go ahead and store how many times we've
been to the site in a cookie. So the way we do this
first we're going to take the number of visits we have and we're going to
increment it by one. Visits plus equals one and then we're going to store
the number of visits in this cookie called visits so the next time
we come back we'll get it. The way we set a cookie in
App Engine is we just set the cookie header. Okay,
so I added two more lines here. Visits plus equals 1,
which will increment the visits count that we got from our
cookie, and then we're going to set the Cookie. So the way
we do that is on the response object, there is
a headers object. You know, just like when we set the
Content Type. And now we want to, we're going to call add_header. We
could treat it like a dictionary, but add_header here is special
because remember, you can have multiple headers with the same name,
and so if you used a dictionary syntax, you'd overwrite the header.
So in this case, we're going to use add_header, that's what we're going to
use for cookies here. So we're going to use the Set-Cookie header, and
I'm just going to use, I'm not going to worry about domain and path
right now. That's not what we're trying to experiment with here and
we're going to say, visits equals %s, and we're going to include our Visits
parameter. All right, so let's see if that works. We're on our,
our browser and you know when I, and I've
reloaded the page. And so we see I've been
here one time, and I reload the page again.
Ooh, and we see an error, and this makes sense.
Need a string, int found on visits plus equals
1. Cookies you know, the browser doesn't care what datatype
our cookie is here we just says that it's,
you know, it's basically this, this, this string, this blob
of data and here I am treating it like an
integer. So let's, let's go ahead and update this code to
make this work properly for strings. Okay, so, we've improved
our code a little bit here. I changed our, our default
zero to the string zero and so that this next
line will work without exceptions. If visits.isdigit, remember that checks to
see if visits is made up of only, only digits in
which case we can safely convert it to an integer and
we add one to it. So we increment visits every time,
otherwise we just set visits to zero and that's what we print.
Okay, so let's give this a whirl. So now we've got our
page here, and I re-load it, the arrow went away, and we
can see this counting up. Pretty neat huh, so that count is
stored in a cookie in this browser. The server has no idea
what the count is, so, let's go ahead and add some functionality
to further my agenda. Now what we want to do, is, is let's,
let's do something with this cookie value. Let's say we want to reward users who
have been to our website 100 times with a special message of thanks. So
let's, let's do it like let's, let's make some changes here. We'll say if
visits is greater than 100, self.write you are
the best ever. And for everybody else, we'll just
tell them how many times they've been here. Let's see this in action.
Actually so we want to, we want to do it on 100, but let's go ahead
and, and test it on ten so we don't have to reload the
page 100 times, just to make sure this works. Okay, so I'm going to save
this, and we're going to go to our browser and give it a shot.
Conveniently I'm at nine times we, reload ten times. Hah greater than ten after
I reloaded it switched to you are the best ever. Great, we feel very
good let's go back to our code and change that ten to a 100.
So change that 10 to a 100, we go back to
our browser, we keep reloading again. Okay, so we really want to see
our, our special message. But it's going to take us awhile to
get there, we're going to have to sit here and reload the page.
Actually let's make this 100,000, okay? We're never going to get there,
we're going to have to be a super loyal user to, to get
up to that amount. So, what I'd like you to do
is think about, how we might cheat the system a little bit.
Webアプリケーションを構築し
Cookieを利用してユーザがサイトを
訪問した回数を追跡します
GAEでのCookieの利用法や
注意すべき点についてご説明します
これはテンプレートです
GAEのために作ってきたものです まず最初に
Content-Typeをtextに設定します
今はHTMLを扱わないからです
visitsというCookieを取得します
selfのrequestオブジェクトがあり
そのrequestオブジェクトは
cookiesオブジェクトを持っています
GAEによるものです
HTTPヘッダを解析して
Cookieをすべてオブジェクトのように辞書に投げ込み
getを呼び出します
もちろんこのgetではありません
この関数は辞書内をチェックして
キーがあればキーの値を取得して
キーがなければ
ゼロに設定される初期値を取得します
そのサイトを訪問した回数を出力したいので
この行を追加してwriteで
You've been here %s times!と出力させます
文字列の入れ替えでここにvisitsが入ります
ブラウザでテストしてみましょう
ページを読み込むと訪問回数は0回と表示されます
何度更新しても0回のままです
カウントせずにゼロを出力しているだけだからです
サイトの訪問回数をCookieに格納しましょう
それを行うにはまず最初に
visitsの数を取得してインクリメントします
visits+=1と記述したあとvisitsの数を
visitsという名前のCookieに格納します
GAEにCookieを設定するため
Cookieヘッダを設定します
ここに2行追加します visits+=1は
Cookieから取得したvisits数を
インクリメントしてCookieに設定します
これはresponseオブジェクト上で行います
Content-Typeに設定されたheadersがあり
辞書のように扱えるadd_headerを呼び出します
このadd_headerは特別で
同じ名前で複数のヘッダを持てます
辞書の文法を使ったら
ヘッダを上書きしてしまったでしょう
ですからadd_headerを使います
そしてSet-Cookieヘッダを使用します
domainやpathは気にしません
ここで試そうとしていることではないからです
visits=%sを記述しvisitsパラメータを入れます
それでは動作するか見てみましょう
ブラウザでページを更新すると
1回と表示されました
もう一度更新します
エラーが表示されましたが これは当然です
visits+=1で文字列に整数が代入されたからです
ブラウザはCookieのデータ型を気にしませんが
ここでは基本的に
この文字列はBLOBデータなのに
整数のように扱っています
このコードを更新して
文字列に対して正常に動作するようにしましょう
コードを少し改良して初期値の0を
文字列のゼロに変更したので次の行は
正常に動作します If visits.isdigitはvisitsが
数字だけでできているかどうかチェックします
これで安心して整数に変換できます
+1してvisitsをインクリメントします
数字でなかったらvisitsを0に設定します
それではこれを試してみましょう
ページを更新すると
加算されていくのが分かります この回数は
ブラウザのCookieに格納されます
さらに機能を追加していきましょう
講義の予定に沿って進めます
次はCookie値を使って何かを行います
100回訪れてくれたユーザを
特別な感謝のメッセージで報います
ここにいくつか変更を加えましょう
visitsが100より大きければself.writeで
You are the best ever!と出力します
100回以下の人々に対しては
訪問した回数を知らせるだけです
100回でこの処理を実行したいところですが
10回でテストしてみれば
100回も更新する必要はありません
保存してブラウザで試します
都合よく9回になっていたので
更新すると10回になります
10回を超えるとメッセージが変わりました
コードに戻って10を100に変更します
10から100に変更したら
ブラウザに戻り何度も更新します
本当に見たいのは
特別なメッセージですが
たどり着くのに時間がかかります
更新し続けなければなりません
10万個にするとどうなるでしょう
その数にたどり着くためには
ヘビーユーザにならなくてはなりません
そこで少しシステムを
だます方法を考えてください