-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb_searcher.py
80 lines (66 loc) · 2.37 KB
/
fb_searcher.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
# -*- coding=UTF-8 -*-
import argparse
from sqlalchemy import exc, or_, func
from helper import get_logger, get_session, timeit
from models import Track, FBShop
from fb_api import FBBot
__author__ = "Wen-Hao Lee"
__email__ = "[email protected]"
__copyright__ = "Copyright 2014, Numnum"
logger = get_logger('fb_searcher', __file__)
class FBSearcher(object):
def __init__(self, env):
self.fb_bot = FBBot()
self.session = get_session(env)
@timeit(logger)
def search_locations(self, limit=None):
locs = (
self.session.query(Track)
.order_by('update_ts').limit(limit).all())
updated = 0
for loc in locs:
updated += self.search_location(loc)
return updated
def search_location(self, loc):
shops = self.fb_bot.search_restaurant(loc.lat, loc.lon)
loc.found = len(shops)
updated = 0
for shop in shops:
updated += self._save_fb_shop(shop.get('id'), loc)
logger.info(
"Track_id: {track_id} Found: {found} Updated: {updated}".format(
track_id=loc.id_,
found=loc.found,
updated=updated))
loc.update_ts = func.current_timestamp()
try:
self.session.commit()
except exc.SQLAlchemyError as err:
self.session.rollback()
logger.error(err)
return updated
def _save_fb_shop(self, fbid, loc):
fb_shop = self.session.query(FBShop).filter(
or_(FBShop.fbid == int(fbid), FBShop.old_fbid == int(fbid))).first()
if not fb_shop:
fb_shop = FBShop(fbid=int(fbid), track_id=loc.id_)
try:
loc.saved += 1
self.session.add(fb_shop)
self.session.commit()
except exc.SQLAlchemyError as err:
self.session.rollback()
logger.error(err)
return 1
return 0
def main():
parser = argparse.ArgumentParser(description="Search fb shops from tracks")
parser.add_argument(
'-e', '--env', help='environment in dev/stage/prod', required=True)
parser.add_argument(
'-l', '--limit', help='limit num of locations to search', default=500)
args = parser.parse_args()
fb_searcher = FBSearcher(args.env)
fb_searcher.search_locations(args.limit)
if __name__ == '__main__':
main()