blob: 6333ea0f8925980b75e079a05d58bd8da836edb6 [file] [log] [blame]
Justin Yip5fa5bbc2014-09-19 04:25:11 -07001"""
2itemrank quickstart import data
3"""
4
5import predictionio
6
7import random
Justin Yipfd634ed2014-11-11 18:59:31 -08008import sys
Justin Yip5fa5bbc2014-09-19 04:25:11 -07009
Justin Yipfd634ed2014-11-11 18:59:31 -080010def import_itemrank(access_key):
Justin Yip5fa5bbc2014-09-19 04:25:11 -070011
12 random.seed()
13
Justin Yipfd634ed2014-11-11 18:59:31 -080014 client = predictionio.EventClient(access_key)
Justin Yip3343dff2014-09-19 15:32:07 -070015
16 print client.get_status()
Justin Yip5fa5bbc2014-09-19 04:25:11 -070017
18 # generate 10 users, with user ids 1,2,....,10
19 user_ids = [str(i) for i in range(1, 11)]
20 for user_id in user_ids:
21 print "Set user", user_id
22 client.set_user(user_id)
23
24 # generate 50 items, with item ids 1,2,....,50
25 # assign type id 1 to all of them
26 item_ids = [str(i) for i in range(1, 51)]
27 for item_id in item_ids:
28 print "Set item", item_id
29 client.set_item(item_id, {
Justin Yipc5e90242014-12-12 16:13:15 -080030 "itypes" : ['1']
Justin Yip5fa5bbc2014-09-19 04:25:11 -070031 })
32
33 # each user randomly views 10 items
34 for user_id in user_ids:
35 for viewed_item in random.sample(item_ids, 10):
36 print "User", user_id ,"views item", viewed_item
37 client.record_user_action_on_item("view", user_id, viewed_item)
38
39 client.close()
40
41
42if __name__ == '__main__':
Justin Yipfd634ed2014-11-11 18:59:31 -080043 if len(sys.argv) < 2:
44 sys.exit("Usage: python -m examples.itemrank_quick_start <access_key>")
45 import_itemrank(sys.argv[1])