-
Notifications
You must be signed in to change notification settings - Fork 84
plugin oncrawl v1.0.0 #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chiktika
wants to merge
6
commits into
dataiku:master
Choose a base branch
from
cogniteev:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f10ca3
plugin oncrawl v1.0.0
579328f
add a way to get last crawl, add a link to contact us to get an api k…
c5e0513
add a way to get last crawl, add a link to contact us to get an api k…
fb350d2
Update requirements.txt
chiktika 7677eea
Update plugin url
chiktika f190a0c
update tags
chiktika File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "pythonInterpreter": "PYTHON36", | ||
| "acceptedPythonInterpreters": ["PYTHON36"], | ||
| "forceConda": false, | ||
| "installCorePackages": true, | ||
| "installJupyterSupport": false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| prison | ||
| pendulum | ||
chiktika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "meta": { | ||
| "label": "OnCrawl data queries", | ||
| "description": "Export URLs or aggregations from crawls or log monitoring events", | ||
| "icon": "icon-globe", | ||
| "iconColor": "sky" | ||
| }, | ||
|
|
||
| "kind": "PYTHON", | ||
|
|
||
| "inputRoles": [], | ||
|
|
||
| "outputRoles": [ | ||
| { | ||
| "name": "output", | ||
| "label": "output", | ||
| "description": "output", | ||
| "arity": "UNARY", | ||
| "required": true, | ||
| "acceptsDataset": true | ||
| } | ||
| ], | ||
|
|
||
| 'paramsModule' : 'oncrawl-data_queries.module', | ||
| 'paramsPythonSetup' : 'data_queries.py', | ||
| 'paramsTemplate' : 'data_queries.html', | ||
|
|
||
| "params": [] | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import dataiku | ||
| from dataiku.customrecipe import * | ||
chiktika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import requests | ||
| import json | ||
chiktika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from oncrawl import oncrawlDataAPI as ocd | ||
|
|
||
| output_names = get_output_names_for_role('output') | ||
| output_datasets = [dataiku.Dataset(name) for name in output_names] | ||
| output = output_datasets[0] | ||
|
|
||
| #------------------------------config & vars | ||
| config = get_recipe_config() | ||
|
|
||
| headers = { | ||
| 'Content-Type': 'application/json', | ||
| 'Accept': 'application/json', | ||
| 'Authorization' : 'Bearer {}'.format(config['api_key']) | ||
| } | ||
|
|
||
| #--according index, ids are related to projects or crawls - each id represent a crawl or a project | ||
| if config['index'] != 'logs': | ||
| ids = config['list_configs_crawls'][config['crawl_config']] | ||
chiktika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if config['crawls_id'] != 'all': | ||
| ids = [config['crawls_id']] | ||
| else: | ||
| ids = config['list_projects_id_name'].keys() | ||
| if config['projects_id'] != 'all': | ||
| ids = [config['projects_id'].split(',')[0]] | ||
|
|
||
| #------------------------------schema | ||
| #fields not returned by oncrawl API | ||
| metadata = { | ||
| 'project_id': 'string', | ||
| 'project_name': 'string', | ||
| 'crawl_id': 'string', | ||
| 'config_name': 'string', | ||
| 'crawl_start_timestamp': 'bigint' | ||
| } | ||
|
|
||
| metadata_fields = ocd.build_schema_from_metadata(config, metadata) | ||
|
|
||
| schema = { | ||
| 'dataset_schema': metadata_fields['schema'], | ||
| 'dataset_schema_field_list': metadata_fields['list'] | ||
| } | ||
| fields_to_request_by_ids = {} | ||
|
|
||
| for i, id in enumerate(ids): | ||
|
|
||
| progress = '#{} {} {}/{}'.format(id, 'crawls' if config['index'] != 'logs' else 'projects', (i+1), len(ids)) | ||
|
|
||
| #when aggregate data, all items have same schema | ||
| if config['data_action'] == 'aggs': | ||
|
|
||
| if i == 0: | ||
| f = ocd.build_schema_from_config(config=config) | ||
| schema['dataset_schema'] = schema['dataset_schema'] + f | ||
| print('############################\r\n############################\r\nBuil dataset schema with: ', progress) | ||
| else: | ||
| break | ||
| else: | ||
|
|
||
| print('############################\r\n############################\r\nBuil dataset schema with: ', progress) | ||
| #when export data, for many reasons all items could not have same schema | ||
| #return new fields to add to dataset schema and all fields to request for this item | ||
| f = ocd.build_schema_from_oncrawl(config=config, id=id, headers=headers, schema=schema) | ||
|
|
||
| if 'item_schema' not in f.keys() or len(f['item_schema']) == 0: | ||
| continue | ||
|
|
||
| schema['dataset_schema'] = schema['dataset_schema'] + f['dataset_schema'] | ||
| schema['dataset_schema_field_list'] = schema['dataset_schema_field_list'] + f['dataset_schema_field_list'] | ||
|
|
||
| fields_to_request_by_ids[id] = f['item_schema'] | ||
|
|
||
| output.write_schema(schema['dataset_schema']) | ||
|
|
||
| #------------------------------data & writer | ||
| total_count = 0 | ||
| with output.get_writer() as writer: | ||
|
|
||
| for i, id in enumerate(ids): | ||
|
|
||
| #this case happend when project has no log feature or unexpected ES issue | ||
| if config['data_action'] == 'export' and id not in fields_to_request_by_ids.keys(): | ||
| continue | ||
|
|
||
| progress = '#{} {} {}/{}'.format(id, 'crawls' if config['index'] != 'logs' else 'projects', (i+1), len(ids)) | ||
| print('############################\r\n############################\r\nGet data for: ', progress) | ||
|
|
||
| metadata_value = ocd.fill_metadata(config, id) | ||
| if config['data_action'] == 'export': | ||
| data = ocd.export(config_oql=config['oql'], fields=fields_to_request_by_ids[id], config_index=config['index'], id=id, headers=headers) | ||
| else: | ||
| data = ocd.aggs(config_oql=config['oql'], config_index=config['index'], id=id, headers=headers) | ||
|
|
||
| count_result = 0 | ||
| try: | ||
| for json_line in data: | ||
|
|
||
| row = metadata_value + [] | ||
|
|
||
| if config['data_action'] == 'export': | ||
| #oncrawl export api send values not in the same order as schema... | ||
| for field in schema['dataset_schema_field_list']: | ||
| if field not in list(metadata.keys()): | ||
| if field in list(json_line.keys()): | ||
| if field in ['title', 'meta_description', 'h1'] and json_line[field] is not None: | ||
| row.append(json_line[field].encode(encoding = 'utf8', errors = 'replace')) | ||
| else: | ||
| row.append(json_line[field]) | ||
| else: | ||
| row.append(None) | ||
| else: | ||
| row = row + list(json_line.values()) | ||
|
|
||
| writer.write_row_array(row) | ||
| count_result += 1 | ||
| print(progress, 'row: ',count_result) | ||
| print(progress, ': total row recorded: ', count_result, '\r\n############################\r\n############################') | ||
|
|
||
| except Exception as e: | ||
| raise Exception('{}'.format(e)) | ||
|
|
||
| total_count += 1 | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.