aboutsummaryrefslogtreecommitdiff
path: root/newspipe/commands.py
blob: 03d5053e1091a45b6a4d87ef53c46d347ee623c8 (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
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import os
from dateutil.relativedelta import relativedelta
from datetime import datetime, date

import click
from werkzeug.security import generate_password_hash

import newspipe.models
from newspipe.bootstrap import application, db
from newspipe.controllers import UserController

logger = logging.getLogger("commands")


@application.cli.command("db_empty")
def db_empty():
    "Will drop every datas stocked in db."
    with application.app_context():
        newspipe.models.db_empty(db)


@application.cli.command("db_create")
def db_create():
    "Will create the database from conf parameters."
    with application.app_context():
        try:
            db.create_all()
        except Exception as e:
            print(e)


@application.cli.command("create_admin")
@click.option('--nickname', default='admin', help='Nickname')
@click.option('--password', default='password', help='Password')
def create_admin(nickname, password):
    "Will create an admin user."
    admin = {
        "is_admin": True,
        "is_api": True,
        "is_active": True,
        "nickname": nickname,
        "pwdhash": generate_password_hash(password),
    }
    with application.app_context():
        try:
            UserController(ignore_context=True).create(**admin)
        except Exception as e:
            print(e)


@application.cli.command("delete_user")
@click.option('--user-id', required=True, help='Id of the user to delete.')
def delete_user(user_id=None):
    "Delete the user with the id specified in the command line."
    try:
        user = UserController().delete(user_id)
        print("User {} deleted".format(user.nickname))
    except Exception as e:
        print(e)


@application.cli.command("delete_inactive_users")
@click.option('--last-seen', default=6, help='Number of months since last seen.')
def delete_inactive_users(last_seen):
    "Delete inactive users."
    filter["last_seen__lt"] = date.today() - relativedelta(months=last_seen)
    try:
        user = UserController().read(**filter).all()
        for us in user:
            print(us.nickname)
        print("Inactive users deleted.")
    except Exception as e:
        print(e)


@application.cli.command("fetch_asyncio")
@click.option('--user-id', default=None, help='Id of the user')
@click.option('--feed-id', default=None, help='If of the feed')
def fetch_asyncio(user_id=None, feed_id=None):
    "Crawl the feeds with asyncio."
    import asyncio

    with application.app_context():
        from newspipe.crawler import default_crawler

        filters = {}
        filters["is_active"] = True
        filters["automatic_crawling"] = True
        if None is not user_id:
            filters["id"] = user_id
        users = UserController().read(**filters).all()

        try:
            feed_id = int(feed_id)
        except:
            feed_id = None

        loop = asyncio.get_event_loop()
        queue = asyncio.Queue(maxsize=3, loop=loop)

        producer_coro = default_crawler.retrieve_feed(queue, users, feed_id)
        consumer_coro = default_crawler.insert_articles(queue, 1)

        logger.info("Starting crawler.")
        start = datetime.now()
        loop.run_until_complete(asyncio.gather(producer_coro, consumer_coro))
        end = datetime.now()
        loop.close()
        logger.info("Crawler finished in {} seconds.".format((end - start).seconds))
bgstack15