3
3
# This Source Code Form is subject to the terms of the Mozilla Public
4
4
# License, v. 2.0. If a copy of the MPL was not distributed with this
5
5
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
+ """GitHub Webhook module for assigning priority to sites."""
6
7
7
8
import base64
8
9
import datetime
16
17
17
18
import requests
18
19
from requests .exceptions import ConnectionError
19
- from sqlalchemy import create_engine
20
20
from sqlalchemy import Column
21
- from sqlalchemy import Integer
22
- from sqlalchemy import String
21
+ from sqlalchemy import create_engine
23
22
from sqlalchemy .ext .declarative import declarative_base
23
+ from sqlalchemy import Integer
24
24
from sqlalchemy .orm import sessionmaker
25
+ from sqlalchemy import String
25
26
26
27
# Add webcompat module to import path
27
28
sys .path .append (os .path .realpath (os .pardir ))
36
37
ATS_HASH_ALGORITHM = 'HmacSHA256'
37
38
ATS_COUNT = 100
38
39
40
+ # Location of the DB and its backup.
41
+ DB_PATH = app .config ['DATA_PATH' ]
42
+
39
43
# Regions to dump to topsites.db
40
44
REGIONS = ['GLOBAL' , 'US' , 'FR' , 'IN' , 'DE' , 'TW' , 'ID' , 'HK' , 'SG' , 'PL' ,
41
45
'GB' , 'RU' ]
48
52
# Cache parsed sites, change priority if raised
49
53
topsites = {}
50
54
51
- engine = create_engine ('sqlite:///' + os .path .join (
52
- app .config ['BASE_DIR' ], 'topsites-new.db' ))
55
+ engine = create_engine ('sqlite:///' + os .path .join (DB_PATH , 'topsites-new.db' ))
53
56
Base = declarative_base ()
54
57
Session = sessionmaker (bind = engine )
55
58
session = Session ()
56
59
57
60
58
61
class Site (Base ):
59
62
"""SQLAchemy base object for an Alexa top site."""
63
+
60
64
__tablename__ = "topsites"
61
65
62
66
url = Column (String , primary_key = True )
@@ -65,6 +69,7 @@ class Site(Base):
65
69
ranking = Column (Integer )
66
70
67
71
def __init__ (self , url , priority , country_code , ranking ):
72
+ """Initialize parameters of the Alexa top site DB."""
68
73
self .url = url
69
74
self .priority = priority
70
75
self .country_code = country_code
@@ -168,7 +173,7 @@ def build_query_string(country_code, start_ranking):
168
173
169
174
170
175
def gen_sign (data ):
171
- """Computes RFC 2104-compliant HMAC signature."""
176
+ """Compute RFC 2104-compliant HMAC signature."""
172
177
dig = hmac .new (ats_secret_key , data , hashlib .sha256 ).digest ()
173
178
return base64 .b64encode (dig )
174
179
@@ -199,8 +204,8 @@ def node_text(tree, tag_name):
199
204
# Archive topsites.db and rename topsites-new.db to topsites.db
200
205
session .close ()
201
206
archive_date = time .strftime ("%Y%m%d" , time .localtime ())
202
- os .rename (os .path .join (app . config [ 'BASE_DIR' ] , 'topsites.db' ),
203
- os .path .join (app . config [ 'BASE_DIR' ] ,
207
+ os .rename (os .path .join (DB_PATH , 'topsites.db' ),
208
+ os .path .join (DB_PATH ,
204
209
'topsites-archive-{}.db' .format (archive_date )))
205
- os .rename (os .path .join (app . config [ 'BASE_DIR' ] , 'topsites-new.db' ),
206
- os .path .join (app . config [ 'BASE_DIR' ] , 'topsites.db' ))
210
+ os .rename (os .path .join (DB_PATH , 'topsites-new.db' ),
211
+ os .path .join (DB_PATH , 'topsites.db' ))
0 commit comments