aboutsummaryrefslogtreecommitdiff
path: root/app.py
blob: 66ca6c4f49e004736377b20b579d71da45de2c76 (plain)
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
import scrapy
import sys
from scrapy.crawler import CrawlerProcess
from scrapy import spiderloader
from scrapy.utils.project import get_project_settings
from flask import Flask, request, jsonify
import json

from sphinx import sphinx

from database.models import Page, db
import config

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def search():
    query = request.args.get('search')
    index = request.args.get('index')
    if query != '' and query != None and index != '' and index != None:
        response = sphinx.search(query, index)
    elif query is None:
        response = {'error': 1, 'msg': 'Use `search` attribute for make a search'}
    elif index == None:
        response = {'error': 1, 'msg': 'Use `index` attribute to precise an index'}
    elif index == '':

        response = {'error': 1, 'msg': '`index` cannot be null'}
    else:
        response = {'error': 1, 'msg': '`search` cannot be null'}
    return jsonify(response)

def crawl():
    try:
        db.create_tables(Page.__subclasses__())
        settings = get_project_settings()
        process = CrawlerProcess(settings)
        spiders = spiderloader.SpiderLoader.from_settings(settings)
        for spider in spiders.list():
            process.crawl(spider)
        process.start()
    except Exception as e:
        print(e)

def main():

    app.run(debug=True, use_reloader=True)

def show_help():
    print("Launch server      => "+str(sys.argv[0]))
    print("Launch all crawler => "+str(sys.argv[0])+" crawl")

if __name__ == '__main__':
    if len(sys.argv) == 1:
        main()
    elif len(sys.argv) == 2:
        if sys.argv[1] == "crawl":
            crawl()
        else:
            show_help()
    else:
        show_help()