-
Notifications
You must be signed in to change notification settings - Fork 3
/
updateDB.py
55 lines (41 loc) · 1.3 KB
/
updateDB.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
import json
import sqlite3
import subprocess
import sys
import requests
def getLastPostOnHabr():
""" Get last post on Habr """
url = "https://m.habr.com/kek/v2/articles/"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0'
}
params = {
"date": "day",
"sort": "date"
}
r = requests.get(url, headers=headers, params=params)
data = json.loads(r.text)
lastPostOnHabr = data['articleIds'][0]
return lastPostOnHabr
def getLastPostInBase():
""" Get last post in database """
query = "SELECT max(id) from articles"
try:
conn = sqlite3.connect('habr.db')
c = conn.cursor()
c.execute(query)
lastPostInBase = c.fetchone()[0]
except sqlite3.OperationalError:
return 0
return lastPostInBase
lastPostBase = getLastPostInBase()
lastPostHabr = getLastPostOnHabr()
print("min={} max={}".format(lastPostBase, lastPostHabr))
argsArticles = "{} articles.py --min {} --max {}".format(
sys.executable, lastPostBase, lastPostHabr)
argsComments = "{} comments.py --min {} --max {}".format(
sys.executable, lastPostBase, lastPostHabr)
subprocess.call(argsArticles)
print("Articles downloaded")
subprocess.call(argsComments)
print("Comments downloaded")