-
Notifications
You must be signed in to change notification settings - Fork 2
/
insert.py
133 lines (108 loc) · 3.17 KB
/
insert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
from models import Food, FoodDailyInfo
from google.appengine.ext import ndb, db
def process_item(item):
# print item
food = Food.get_or_insert(item['name'])
for key, value in item.items():
try:
if key != 'date':
setattr(food, key, value)
print key, value
except Exception as e:
print e
food.push_info(item['date'])
food.aggregate()
food.get_recommand()
return food
def download(date):
print 'start'
from works import pork, checken, fish, vegetable
results = []
for item in vegetable.get(date):
results.append(process_item(item))
print len(results)
for item in fish.get(date):
results.append(process_item(item))
print len(results)
for item in checken.get(date):
if u'其他' in item['name']:
continue
if u'待訂' in item['name']:
continue
results.append(process_item(item))
print len(results)
for item in pork.get(date):
results.append(process_item(item))
ndb.put_multi(results)
print len(results)
from datetime import datetime, timedelta, date
def run():
time = date(2014,6,1)
today = datetime.now().date()
while time <= today:
print time
download(time)
time += timedelta(1)
def fix_date(food):
pass
def sort():
vegetables = Food.query(Food.type=="vegetable").fetch(1000)
vegetables.sort(key=lambda x:x.get_point())
vegetables.reverse()
meats = Food.query(Food.type=="meat").fetch(1000)
meats.sort(key=lambda x:x.get_point())
meats.reverse()
fishs = Food.query(Food.type=="fish").fetch(1000)
fishs.sort(key=lambda x:x.get_point())
fishs.reverse()
c = 0
for vegetable in vegetables:
print 'vegetable', c, vegetable.name
vegetable.rank = c
print vegetable.point
if c:
vegetable.prev = vegetables[c-1].name
if c < len(vegetables) - 1:
vegetable.next_one = vegetables[c+1].name
c += 1
c=0
for meat in meats:
print 'meat', c, meat.name
print meat.point
meat.rank = c
if c:
meat.prev = meats[c-1].name
if c < len(meats) - 1:
meat.next_one = meats[c+1].name
c += 1
c=0
for fish in fishs:
print 'fish', c, fish.name
print fish.point
fish.rank = c
if c:
fish.prev = fishs[c-1].name
if c < len(fishs) - 1:
fish.next_one = fishs[c+1].name
c += 1
result = []
result.extend(vegetables)
result.extend(fishs)
result.extend(meats)
ndb.put_multi(result)
def set_image():
import google_search_image
foods = Food.query().fetch(1000)
for food in foods:
try:
print food.name, food.image
assert not food.image, 'had image'
key = u'{}'.format(food.name)
print key
img = google_search_image.search(key.encode('utf-8')).next()
food.image = img.get('url')
print food.name, food.image
food.put()
except Exception as e:
print e