mirror of
https://github.com/internetarchive/brozzler.git
synced 2025-08-08 14:32:23 -04:00
Convert from one big json file, to js files with a regex as a comment at the top.
This commit is contained in:
parent
4c22891093
commit
f2f78d2ced
5 changed files with 90 additions and 28 deletions
|
@ -2,17 +2,25 @@
|
|||
|
||||
from json import dumps, load
|
||||
from time import sleep
|
||||
from itertools import chain
|
||||
import os, re
|
||||
import logging
|
||||
|
||||
behaviors_file = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ['behaviors.json'])
|
||||
behaviors_directory = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ['behaviors.d'])
|
||||
behavior_files = chain(*[[dir + os.path.sep + file for file in files] for dir, dirs, files in os.walk(behaviors_directory)])
|
||||
behaviors = []
|
||||
for file_name in behavior_files:
|
||||
lines = open(file_name).readlines()
|
||||
pattern, script = lines[0][2:].strip(), ''.join(lines[1:])
|
||||
behaviors.append({'site' : pattern, 'script': script})
|
||||
|
||||
print(behaviors)
|
||||
def execute(url, websock, command_id):
|
||||
logger = logging.getLogger('behaviors')
|
||||
with open(behaviors_file) as js:
|
||||
behaviors = load(js)
|
||||
for behavior in behaviors:
|
||||
if re.match(behavior['site'], url):
|
||||
for script in behavior['scripts']:
|
||||
msg = dumps(dict(method="Runtime.evaluate", params={"expression": script}, id=next(command_id)))
|
||||
logger.debug('sending message to {}: {}'.format(websock, msg))
|
||||
websock.send(msg)
|
||||
print(behaviors)
|
||||
for behavior in behaviors:
|
||||
print("Comparing %s and %s" %(behavior['site'], url))
|
||||
if re.match(behavior['site'], url):
|
||||
msg = dumps(dict(method="Runtime.evaluate", params={"expression": behavior['script']}, id=next(command_id)))
|
||||
logger.debug('sending message to {}: {}'.format(websock, msg))
|
||||
websock.send(msg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue