• R/O
  • SSH
  • HTTPS

dietmemorer: Commit


Commit MetaInfo

Revision33 (tree)
Time2008-04-17 00:40:41
Authoryaggy

Log Message

・カロリーAPIに対応
・グラフ強化
・昨日入力
・基礎代謝入力
・httpの含まれているメールをはじく

Change Summary

Incremental Difference

--- trunk/db/DDL.txt (revision 32)
+++ trunk/db/DDL.txt (revision 33)
@@ -1,3 +1,4 @@
1+DROP TABLE dmetabo;
12 DROP TABLE denergy;
23 DROP TABLE dfat;
34 DROP TABLE dweight;
@@ -63,6 +64,15 @@
6364 CREATE INDEX idx_dfat_userdate ON dfat (mailadd, adddate);
6465
6566
67+CREATE TABLE dmetabo (
68+ mailadd VARCHAR(128),
69+ metabo INT,
70+ adddate DATE
71+);
72+
73+CREATE INDEX idx_dmetabo_userdate ON dmetabo (mailadd, adddate);
74+
75+
6676 CREATE TABLE dmailhistory (
6777 mailadd VARCHAR(128),
6878 mailsubject TEXT,
--- trunk/src/graph_creator.rb (revision 32)
+++ trunk/src/graph_creator.rb (revision 33)
@@ -16,6 +16,9 @@
1616 @@ModeGraphWeight = 1
1717 @@ModeGraphFat = 2
1818 @@ModeGraphEnergy = 3
19+ @@ModeGraphWF = 4
20+ @@ModeGraphFE = 5
21+ @@ModeGraphEW = 6
1922
2023 # 体重グラフ作成
2124 def createwgraph(basepath, email)
@@ -54,6 +57,7 @@
5457 # 体重
5558 tsth = @dbh.prepare("SELECT weight, adddate FROM dweight WHERE mailadd = ? AND adddate > DATE_ADD(CURDATE(), INTERVAL -90 DAY) ORDER BY adddate")
5659 tsth.execute(email)
60+ wrows = tsth.rows
5761 if 0 != tsth.rows then
5862 tsth.fetch do |weight|
5963 hweight[weight[1].to_s] = weight[0]
@@ -64,6 +68,7 @@
6468 # 体脂肪
6569 tsth = @dbh.prepare("SELECT fat, adddate FROM dfat WHERE mailadd = ? AND adddate > DATE_ADD(CURDATE(), INTERVAL -90 DAY) ORDER BY adddate")
6670 tsth.execute(email)
71+ frows = tsth.rows
6772 if 0 != tsth.rows then
6873 tsth.fetch do |fat|
6974 hfat[fat[1].to_s] = fat[0]
@@ -78,6 +83,7 @@
7883 "GROUP BY donly " + \
7984 "ORDER BY donly ");
8085 tsth.execute(email)
86+ erows = tsth.rows
8187 if 0 != tsth.rows then
8288 tsth.fetch do |energy|
8389 henergy[energy[1].to_s] = energy[0]
@@ -98,6 +104,34 @@
98104 afat = []
99105 aenergy = []
100106
107+ if @@ModeGraphALL == mode then
108+ if 0 != wrows and 0 != frows and 0 != erows then
109+ mode = @@ModeGraphALL
110+ Debug.p "ModeGraphALL"
111+ elsif 0 == wrows and 0 != frows and 0 != erows then
112+ mode = @@ModeGraphFE
113+ Debug.p "ModeGraphFE"
114+ elsif 0 != wrows and 0 == frows and 0 != erows then
115+ mode = @@ModeGraphEW
116+ Debug.p "ModeGraphEW"
117+ elsif 0 != wrows and 0 != frows and 0 == erows then
118+ mode = @@ModeGraphWF
119+ Debug.p "ModeGraphWF"
120+ elsif 0 == wrows and 0 == frows and 0 != erows then
121+ mode = @@ModeGraphEnergy
122+ Debug.p "ModeGraphEnergy"
123+ elsif 0 != wrows and 0 == frows and 0 == erows then
124+ mode = @@ModeGraphWeight
125+ Debug.p "ModeGraphWeight"
126+ elsif 0 == wrows and 0 != frows and 0 == erows then
127+ mode = @@ModeGraphFat
128+ Debug.p "ModeGraphFat"
129+ else
130+ Debug.p "ModeGraph Unknown....."
131+ end
132+ end
133+
134+
101135 while i >= 0
102136
103137 day = (nowday - i).to_s
@@ -115,6 +149,18 @@
115149 if nil != henergy[day] then
116150 started = true
117151 end
152+ elsif @@ModeGraphFE == mode then
153+ if nil != hfat[day] and nil != henergy[day] then
154+ started = true
155+ end
156+ elsif @@ModeGraphEW == mode then
157+ if nil != hweight[day] and nil != henergy[day] then
158+ started = true
159+ end
160+ elsif @@ModeGraphWF == mode then
161+ if nil != hweight[day] and nil != hfat[day] then
162+ started = true
163+ end
118164 elsif @@ModeGraphALL == mode then
119165 if nil != hweight[day] and nil != hfat[day] and nil != henergy[day] then
120166 started = true
@@ -167,7 +213,19 @@
167213 elsif @@ModeGraphEnergy == mode then
168214 g.title = "メモらーグラフ kcal"
169215 g.data("ENERGY(kcal)", aenergy)
170- elsif @@ModeGraphALL == mode then
216+ elsif @@ModeGraphFE == mode then
217+ g.title = "メモらーグラフ kcal"
218+ g.data("体脂肪率", afat)
219+ g.data("ENERGY", aenergy)
220+ elsif @@ModeGraphEW == mode then
221+ g.title = "メモらーグラフ kcal"
222+ g.data("体重", aweight)
223+ g.data("ENERGY", aenergy)
224+ elsif @@ModeGraphWF == mode then
225+ g.title = "メモらーグラフ kcal"
226+ g.data("体重", aweight)
227+ g.data("体脂肪率", afat)
228+ else
171229 g.title = "メモらーグラフ"
172230 g.data("体重", aweight)
173231 g.data("体脂肪率", afat)
--- trunk/src/mail/dmr_footer.txt (revision 32)
+++ trunk/src/mail/dmr_footer.txt (revision 33)
@@ -1,3 +1,4 @@
11 ---
2-ダイエットメモらー 0.3
2+ダイエットメモらー 0.5
33 kcal@dietmemo.net
4+
--- trunk/src/mail_analizer.rb (revision 32)
+++ trunk/src/mail_analizer.rb (revision 33)
@@ -1,5 +1,6 @@
11 require 'dmem_debug.rb'
22 require 'graph_creator.rb'
3+require 'calapi.rb'
34
45 require 'dbi'
56 require 'jcode'
@@ -67,9 +68,15 @@
6768 # 体脂肪記録のキーワード
6869 @@body_fat = ["体脂肪", "体脂肪率", "脂肪", "たいしぼう", "たいしぼうりつ", "しぼう"]
6970
71+ # 基礎代謝記録のキーワード
72+ @@body_metabo = ["代謝", "基礎代謝", "たいしゃ", "きそたいしゃ"]
73+
7074 # 一日最大エネルギーのキーワード
7175 @@body_limit = "最大"
72-
76+
77+ # 当日リセットのキーワード
78+ @@sub_yesterday = ["昨日", "きのう"]
79+
7380 # 登録時メール返信文
7481 @@repmail_first = "mail/dmr_first.txt"
7582
@@ -107,6 +114,8 @@
107114 # _dbh_ :: mysqlへのデータベースハンドル
108115 def initialize(dbh)
109116 @dbh = dbh
117+ @basedate = "CURDATE()"
118+ @basedateflg = "now"
110119 end
111120
112121 # 配列の中に含まれているかを返すメソッド
@@ -257,7 +266,7 @@
257266 # 当日データ全消去
258267 dsth = @dbh.prepare("DELETE FROM denergy " + \
259268 " WHERE mailadd = ? AND " + \
260- " DATE_FORMAT(adddate, '%Y-%m-%d') = DATE_FORMAT(CURDATE(), '%Y-%m-%d')")
269+ " DATE_FORMAT(adddate, '%Y-%m-%d') = DATE_FORMAT(" + @basedate + ", '%Y-%m-%d')")
261270
262271 dsth.execute(to)
263272 dsth.finish
@@ -328,6 +337,7 @@
328337 if 0 != isth.rows then
329338
330339 befdate = Time.local(1975, 1, 1, 0, 0, 0)
340+ befmetabo = 0
331341 daysum = 0
332342
333343 isth.fetch do |irow|
@@ -336,12 +346,43 @@
336346 nenergy = irow[2]
337347 nave = irow[3]
338348 ndate = irow[4]
339- nsum = irow[5]
349+ nsum = irow[5].to_i
340350 rndate = Time.local(ndate.year, ndate.month, ndate.day, 0, 0, 0)
341351
342352 if rndate.to_f != befdate.to_f then
343353 # 日替わり表示
344354 detail.concat("\n" + rndate.month.to_s + "/" + rndate.day.to_s + "(" + @@wdays[rndate.wday] + ") 計" + nsum.to_s + "kcal\n")
355+
356+ # 基礎代謝
357+ tsth = @dbh.prepare("SELECT metabo FROM dmetabo WHERE mailadd = ? AND adddate <= ? ORDER BY adddate DESC")
358+ tsth.execute(mailfrom, rndate)
359+ if 0 != tsth.rows then
360+ tsth.fetch do |metabo|
361+ if nil != metabo[0] then
362+ befmetabo = metabo[0]
363+ else
364+ befmetabo = 0
365+ end
366+ break
367+ end
368+ else
369+ befmetabo = 0
370+ end
371+ tsth.finish
372+
373+ if 0 != befmetabo then
374+ # 一日消費量は、0.7で割った値として計算
375+ cons = befmetabo * 100 / 70
376+ detail.concat("基礎代謝量:" + befmetabo.to_s + "kcal 一日消費量:" + cons.to_s + "kcal ")
377+ if nsum > cons then
378+ caldiff = nsum - cons
379+ detail.concat(caldiff.to_s + "kcal オーバー\n")
380+ else
381+ caldiff = cons - nsum
382+ detail.concat("あと" + caldiff.to_s + "kcal OK\n")
383+ end
384+ end
385+
345386 befdate = rndate
346387
347388 w = false;
@@ -429,11 +470,17 @@
429470 end
430471
431472 mailfrom = mail.from.to_s
432-
473+
474+ if mailbody.length > 5000 then
475+ Debug.p "Invalid mail..."
476+ return
477+ end
478+
433479 # エラーメールっぽかったら無視
434- if mailsub[/DAEMON/] or mailsub[/Daemon/] or mailsub[/daemon/] \
435- or mailsub[/RETURN/] or mailsub[/Return/] or mailsub[/Return/] \
436- or mailfrom[/DAEMON/] or mailfrom[/Delivery/] then
480+ if mailsub[/DAEMON/i] \
481+ or mailsub[/RETURN/i] \
482+ or mailfrom[/DAEMON/i] or mailfrom[/Delivery/i] \
483+ or mailbody[/http/i] then
437484 Debug.p "Invalid mail..."
438485 return
439486 end
@@ -450,6 +497,16 @@
450497 printf "mailadd %s, adddate %s\n", row[0], row[1]
451498 end
452499
500+ # 基準日を確定
501+ if MailAnalizer.in_ary(@@sub_yesterday, mailsub) then
502+ @basedate = "DATE_ADD(CURDATE(), INTERVAL -1 DAY)"
503+ @basedateflg = "yesterday"
504+ else
505+ @basedate = "CURDATE()"
506+ @basedateflg = "now"
507+ end
508+
509+
453510 # ヘルプ要求っぽかったら、ヘルプ送信
454511 if MailAnalizer.in_ary(@@sub_help, mailsub) or MailAnalizer.in_ary(@@sub_help, mailbody) then
455512 sth.finish
@@ -564,6 +621,8 @@
564621 # 邪魔な文字列を除去
565622 utf8body = utf8body.tr(" ", " ")
566623 utf8body = utf8body.tr("\t", " ")
624+ utf8body = utf8body.tr("<", "")
625+ utf8body = utf8body.tr(">", "")
567626 utf8body = utf8body.squeeze(" ")
568627 utf8body = strip_lines(utf8body)
569628
@@ -570,6 +629,10 @@
570629 Debug.p "parse fields"
571630 nowtime = Time.now
572631
632+ if "now" != @basedateflg then
633+ nowtime = nowtime - 1 * 60 * 60 * 24
634+ end
635+
573636 # スペースで分割
574637 CSV::Reader.parse(utf8body, " ") {|field|
575638 if 1 == field.size then
@@ -592,6 +655,18 @@
592655 end
593656 end
594657
658+ # わからない場合は、カロリーAPIから調べる
659+ if nil == newene then
660+ apical = CalApi.getcal(field[0])
661+ if 0 != apical then
662+ newene = apical
663+ # マスタにも登録
664+ isth = @dbh.prepare("INSERT INTO menergy (food, energy, adddate) values(?, ?, now())")
665+ isth.execute(field[0], newene)
666+ isth.finish
667+ end
668+ end
669+
595670 isth = @dbh.prepare("INSERT INTO denergy (mailadd, food, energy, adddate) values(?, ?, ?, ?)")
596671 isth.execute(mailfrom, field[0], newene, nowtime)
597672 isth.finish
@@ -631,6 +706,21 @@
631706 end
632707 tsth.finish
633708
709+ elsif MailAnalizer.in_ary(@@body_metabo, field[0]) then
710+ # 基礎代謝入力っぽい場合
711+ tsth = @dbh.prepare("SELECT mailadd, adddate FROM dmetabo WHERE mailadd = ? AND adddate = ?")
712+ tsth.execute(mailfrom, nowtime)
713+ if 0 != tsth.rows then
714+ isth = @dbh.prepare("UPDATE dmetabo SET metabo = ? WHERE mailadd = ? AND adddate = ?")
715+ isth.execute(field[1], mailfrom, nowtime)
716+ isth.finish
717+ else
718+ isth = @dbh.prepare("INSERT INTO dmetabo (mailadd, metabo, adddate) values(?, ?, ?)")
719+ isth.execute(mailfrom, field[1], nowtime)
720+ isth.finish
721+ end
722+ tsth.finish
723+
634724 else
635725 # 入力項目がふたつの場合は、そのものとエネルギーを入力する
636726 isth = @dbh.prepare("INSERT INTO denergy (mailadd, food, energy, adddate) values(?, ?, ?, ?)")
@@ -638,8 +728,8 @@
638728 isth.finish
639729
640730 # マスタにも登録
641- isth = @dbh.prepare("INSERT INTO menergy (food, energy, adddate) values(?, ?, ?)")
642- isth.execute(field[0], field[1], nowtime)
731+ isth = @dbh.prepare("INSERT INTO menergy (food, energy, adddate) values(?, ?, now())")
732+ isth.execute(field[0], field[1])
643733 isth.finish
644734 end
645735
--- trunk/src/calapi.rb (nonexistent)
+++ trunk/src/calapi.rb (revision 33)
@@ -0,0 +1,79 @@
1+require 'dmem_debug.rb'
2+
3+require 'net/http'
4+require "rexml/document"
5+require 'kconv'
6+require "cgi"
7+
8+include REXML
9+
10+class CalApi
11+
12+ def self.getcal(keyword)
13+
14+ cal = 0
15+
16+ begin
17+ cal = getcal_core(keyword)
18+ rescue => exc
19+ Debug.p exc
20+ end
21+
22+ cal
23+
24+ end
25+
26+
27+ def self.getcal_core(keyword)
28+
29+ name = ""
30+ kcal = 0
31+
32+ esckeywords = CGI.escape(keyword)
33+ address = "soogle.ddo.jp"
34+ path = "/calapi/api?name=" + esckeywords + "&output=xml"
35+ body = Net::HTTP.get(address , path)
36+
37+ doc = Document.new body
38+
39+ firstitem = true
40+
41+ Debug.p doc
42+
43+ doc.elements.each("result/kcalInfo/item") { |element|
44+ if true == firstitem
45+ name = element.get_text("menuName")
46+ kcal = element.get_text("kcalMiddle")
47+ firstitem = false
48+ end
49+
50+ if element.get_text("menuName") == keyword
51+ name = element.get_text("menuName")
52+ kcal = element.get_text("kcalMiddle")
53+ end
54+ }
55+
56+ Debug.p name
57+ Debug.p kcal
58+
59+ kcal
60+
61+ end
62+
63+end
64+
65+
66+# testcode
67+
68+# key = "キットカット"
69+# Debug.p key
70+# Debug.p CalApi.getcal(key)
71+#
72+# key = "まぐろ"
73+# Debug.p key
74+# Debug.p CalApi.getcal(key)
75+#
76+# key = "おにぎり"
77+# Debug.p key
78+# Debug.p CalApi.getcal(key)
79+#
Show on old repository browser