|
| 1 | +import tornado.web |
| 2 | +from tornado.concurrent import run_on_executor |
| 3 | +from concurrent.futures import ThreadPoolExecutor |
| 4 | +from tornado.gen import coroutine |
| 5 | +import tornado.ioloop |
| 6 | + |
| 7 | + |
| 8 | +# import file |
| 9 | +try: |
| 10 | + from instagram_monitering.insta_datafetcher import * |
| 11 | + from instagram_monitering.subpinsta import * |
| 12 | +except: |
| 13 | + from insta_datafetcher import * |
| 14 | + from subpinsta import * |
| 15 | +MAX_WORKERS = 10 |
| 16 | + |
| 17 | + |
| 18 | +class StartHandlerinsta(tornado.web.RequestHandler): |
| 19 | + executor = ThreadPoolExecutor(max_workers=MAX_WORKERS) |
| 20 | + |
| 21 | + @run_on_executor |
| 22 | + def background_task(self, user, tags, type, productId): |
| 23 | + try: |
| 24 | + instasubprocess(user=user, tags=tags, type=type, productId=productId) |
| 25 | + except: |
| 26 | + print("error::background_task>>",sys.exc_info()[1]) |
| 27 | + |
| 28 | + @coroutine |
| 29 | + def get(self): |
| 30 | + try: |
| 31 | + q=self.get_argument("q") |
| 32 | + user = self.get_argument("userId") |
| 33 | + type = self.get_argument("type") |
| 34 | + productId = self.get_argument("productId") |
| 35 | + except: |
| 36 | + self.send_error(400) |
| 37 | + if " " in q: |
| 38 | + q = q.replace(" ","") |
| 39 | + self.background_task(user=user, tags=q, type=type, productId=productId) |
| 40 | + temp = {} |
| 41 | + temp["query"] = q |
| 42 | + temp["userId"] = user |
| 43 | + temp["status"] = True |
| 44 | + temp["productId"] = productId |
| 45 | + print("{0}, {1}, {2}, {3}".format(temp["userId"], temp["productId"], temp["query"], temp["status"])) |
| 46 | + self.write(ujson.dumps(temp)) |
| 47 | + |
| 48 | + |
| 49 | +class StopHandlerinsta(tornado.web.RequestHandler): |
| 50 | + def get(self): |
| 51 | + try: |
| 52 | + q=self.get_argument("q") |
| 53 | + user = self.get_argument("userId") |
| 54 | + # tags = self.get_argument("hashtags") |
| 55 | + productId = self.get_argument("productId") |
| 56 | + except: |
| 57 | + self.send_error(400) |
| 58 | + obj = InstaPorcessClass() |
| 59 | + result = obj.deletProcess(tags=q, user=user, productId=productId) |
| 60 | + temp = {} |
| 61 | + temp["query"] = q |
| 62 | + temp["userId"] = user |
| 63 | + temp["productId"] = productId |
| 64 | + temp["status"] = result |
| 65 | + print("{0}, {1}, {2}, {3}".format(temp["userId"], temp["productId"], temp["query"], temp["status"])) |
| 66 | + self.write(ujson.dumps(temp)) |
| 67 | + |
| 68 | +class StatusHandlerinsta(tornado.web.RequestHandler): |
| 69 | + def get(self): |
| 70 | + try: |
| 71 | + q=self.get_argument("q") |
| 72 | + user = self.get_argument("userId") |
| 73 | + productId = self.get_argument("productId") |
| 74 | + # tags = self.get_argument("hashtags") |
| 75 | + except: |
| 76 | + self.send_error(400) |
| 77 | + obj = InstaPorcessClass() |
| 78 | + result = obj.statusCheck(tags=q, user=user, productId=productId) |
| 79 | + temp = {} |
| 80 | + temp["query"] = q |
| 81 | + temp["userId"] = user |
| 82 | + temp["status"] = result |
| 83 | + temp["productId"] = productId |
| 84 | + print("{0}, {1}, {2}, {3}".format(temp["userId"], temp["productId"], temp["query"], temp["status"])) |
| 85 | + self.write(ujson.dumps(temp)) |
| 86 | + |
| 87 | +# class SenderHandlerinsta(tornado.web.RequestHandler): |
| 88 | +# def get(self): |
| 89 | +# try: |
| 90 | +# q = self.get_argument("q") |
| 91 | +# user = self.get_argument("userId") |
| 92 | +# type = self.get_argument("type") |
| 93 | +# productId = self.get_argument("productId") |
| 94 | +# except: |
| 95 | +# self.send_error(400) |
| 96 | +# recordsobj = DBDataFetcher(user=user, tags=q, type=type, productId=productId) |
| 97 | +# data = recordsobj.dbFetcher() |
| 98 | +# self.write(data) |
| 99 | + |
| 100 | +class SenderHandlerinstaLess(tornado.web.RequestHandler): |
| 101 | + def get(self): |
| 102 | + try: |
| 103 | + q = self.get_argument("q") |
| 104 | + user = self.get_argument("userId") |
| 105 | + type = self.get_argument("type") |
| 106 | + productId = self.get_argument("productId") |
| 107 | + date = self.get_argument("date") |
| 108 | + limit = self.get_argument("limit") |
| 109 | + except: |
| 110 | + self.send_error(400) |
| 111 | + recordsobj = DBDataFetcher(user=user, tags=q, type=type, productId=productId) |
| 112 | + data = recordsobj.DBFetcherLess(limit=limit, date=date) |
| 113 | + # print("{0}, {1}, {2}, {3}".format(temp["userId"], temp["productId"], temp["query"], temp["status"])) |
| 114 | + self.write(data) |
| 115 | + |
| 116 | +class SenderHandlerinstaGreater(tornado.web.RequestHandler): |
| 117 | + def get(self): |
| 118 | + try: |
| 119 | + q = self.get_argument("q") |
| 120 | + user = self.get_argument("userId") |
| 121 | + type = self.get_argument("type") |
| 122 | + productId = self.get_argument("productId") |
| 123 | + date = self.get_argument("date") |
| 124 | + limit = self.get_argument("limit") |
| 125 | + except: |
| 126 | + self.send_error(400) |
| 127 | + recordsobj = DBDataFetcher(user=user, tags=q, type=type, productId=productId) |
| 128 | + data = recordsobj.DBFetcherGreater(limit=limit, date=date) |
| 129 | + # print("{0}, {1}, {2}, {3}".format(temp["userId"], temp["productId"], temp["query"], temp["status"])) |
| 130 | + self.write(data) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + application = tornado.web.Application([(r"/instagram/monitoring/start", StartHandlerinsta), |
| 135 | + (r"/instagram/monitoring/stop", StopHandlerinsta), |
| 136 | + (r"/instagram/monitoring/status", StatusHandlerinsta), |
| 137 | + (r"/instagram/monitoring/less", SenderHandlerinstaLess), |
| 138 | + (r"/instagram/monitoring/greater", SenderHandlerinstaGreater),]) |
| 139 | + |
| 140 | + application.listen(7074) |
| 141 | + print("server running") |
| 142 | + tornado.ioloop.IOLoop.instance().start() |
0 commit comments