django html5 video plugin doesn't show in editor - python

I am trying to install the CKEditor Html5Video plugin.
I have put the add-on html5video into my plugins folder.
I wrote 'html5video' into my settings.py file.
Problem:
Video uploading icon doesn't show inside the editor.
How can I fix this problem ?
What I've tried:
I added this setting in configs.js but doesn't work.
CKEDITOR.config.allowedContent = true
Below is my configs for ckeditor in settings.py:
CKEDITOR_CONFIGS = {
'default': {
'skin': 'moono',
# 'skin': 'office2013',
'toolbar_Basic': [
['Source', '-', 'Bold', 'Italic']
],
'toolbar_YourCustomToolbarConfig': [
{'name': 'document', 'items': ['Source', '-', 'Save', 'NewPage', 'Preview', 'Print', '-', 'Templates']},
{'name': 'clipboard', 'items': ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo']},
{'name': 'editing', 'items': ['Find', 'Replace', '-', 'SelectAll']},
{'name': 'forms',
'items': ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton',
'HiddenField']},
'/',
{'name': 'basicstyles',
'items': ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat']},
{'name': 'paragraph',
'items': ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-',
'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl',
'Language']},
{'name': 'links', 'items': ['Link', 'Unlink', 'Anchor']},
{'name': 'insert',
'items': ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe']},
'/',
{'name': 'styles', 'items': ['Styles', 'Format', 'Font', 'FontSize']},
{'name': 'colors', 'items': ['TextColor', 'BGColor']},
{'name': 'tools', 'items': ['Maximize', 'ShowBlocks']},
{'name': 'about', 'items': ['About']},
'/', # put this to force next toolbar on new line
{'name': 'yourcustomtools', 'items': [
# put the name of your editor.ui.addButton here
'Preview',
'Maximize',
]},
],
'toolbar': 'YourCustomToolbarConfig', # put selected toolbar config here
# 'toolbarGroups': [{ 'name': 'document', 'groups': [ 'mode', 'document', 'doctools' ] }],
# 'height': 291,
# 'width': '100%',
# 'filebrowserWindowHeight': 725,
# 'filebrowserWindowWidth': 940,
# 'toolbarCanCollapse': True,
# 'mathJaxLib': '//cdn.mathjax.org/mathjax/2.2-latest/MathJax.js?config=TeX-AMS_HTML',
'tabSpaces': 4,
'extraPlugins': ','.join([
'uploadimage', # the upload image feature
'html5video',
'div',
'autolink',
'autoembed',
'embedsemantic',
'autogrow',
# 'devtools',
'widget',
'lineutils',
'clipboard',
'dialog',
'dialogui',
'widgetselection',
'elementspath'
]),
}
}

Related

Updating a list of map in Dynamo DB using Boto3 with Python

I have a response from my Dynamo DB as:
{
'subject': 'Mathematics',
'course_id': '123',
'Term': 'Second',
'stats':
{'student_stats':
[
{'student_id': '234',
'registration_id': '321'},
{'student_id': '987',
'registration_id': '456'}
]
},
}
Where the partition key is 'subject' and the sort key is 'course_id'.
I would like to update the 'student_stats' for student with 'student_id' as '234' to passed.
The output should look like this:
{
'subject': 'Mathematics',
'course_id': '123',
'Term': 'Second',
'stats':
{'student_stats':
[
{'student_id': '234',
'registration_id': '321',
'status: 'passed'},
{'student_id': '987',
'registration_id': '456'}
]
},
}
This is my implementation so far:
dynamodb = boto3.resource(
'dynamodb',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=ACCESS_SECRET
)
table = dynamodb.Table(TABLE_NAME)
response = table.update_item(Key={'subject': 'Mathematics',
'course_id': '123'},
UpdateExpression="set #status=:s",
ExpressionAttributeNames={
'#status': 'stats.student_stats'
},
ExpressionAttributeValues={
':s': 'passed'
},
ReturnValues="UPDATED_NEW"
)

How load nested Response JSON into a dataframe without saving in a file?

I'm attempting to load a pandas a dataframe with a heavily nesting JSON. This JSON is the result of an API code in Python. Here is the structure of the JSON file:
{
'results': [{
'accountId': 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
'id': '6ed909bd16',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'NewSeptemberFile20220908',
'description': None,
'created': '2022-09-12T22:04:55.799+00:00',
'lastModified': '2022-09-12T22:06:10.838+00:00',
'lastIndexed': '2022-09-12T22:05:04.551+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '6ed909bd16',
'thumbnailId': '5f04af4d-e382-4387-9573-6d9e4bad3b68',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXX',
'id': '34344818e8',
'partition': None,
'externalId': None,
'metadata': None,
'name': '3September',
'description': None,
'created': '2022-09-09T17:55:59.696+00:00',
'lastModified': '2022-09-09T17:57:51.057+00:00',
'lastIndexed': '2022-09-09T17:56:04.544+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '34344818e8',
'thumbnailId': 'baae7ed1-a791-4481-853c-1707b40b5e77',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'id': '82da4b60ef',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'film10',
'description': None,
'created': '2022-08-22T14:24:08.442+00:00',
'lastModified': '2022-09-08T23:13:16.416+00:00',
'lastIndexed': '2022-08-22T14:24:12.605+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 58,
'thumbnailVideoId': '82da4b60ef',
'thumbnailId': '5a5f6a71-0302-46a6-93c8-beb918c00b14',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-GB',
'sourceLanguages': ['en-GB'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXX',
'id': '7ea0c5e34a',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'davide_quatela--people_in_frankfurt',
'description': None,
'created': '2022-09-07T21:31:52.818+00:00',
'lastModified': '2022-09-08T22:52:52.833+00:00',
'lastIndexed': '2022-09-07T21:31:57.328+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': True,
'hasSourceVideoFile': True,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '100%',
'durationInSeconds': 131,
'thumbnailVideoId': '7ea0c5e34a',
'thumbnailId': '3aba8f42-a3a7-4d77-92b0-8cabcc275a3b',
'searchMatches': [],
'indexingPreset': 'Default',
'streamingPreset': 'Default',
'sourceLanguage': 'en-US',
'sourceLanguages': ['en-US'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}, {
'accountId': 'XXXXXXXXXXXXXXXXXX',
'id': '7c45ae7ffe',
'partition': None,
'externalId': None,
'metadata': None,
'name': 'Untitled project',
'description': None,
'created': '2022-08-17T17:36:23.72+00:00',
'lastModified': '2022-08-17T17:36:49.95+00:00',
'lastIndexed': '2022-08-17T17:36:49.95+00:00',
'privacyMode': 'Private',
'userName': 'M M',
'isOwned': True,
'isBase': False,
'hasSourceVideoFile': False,
'state': 'Processed',
'moderationState': 'OK',
'reviewState': 'None',
'processingProgress': '',
'durationInSeconds': 0,
'thumbnailVideoId': None,
'thumbnailId': '00000000-0000-0000-0000-000000000000',
'searchMatches': [],
'indexingPreset': None,
'streamingPreset': 'Default',
'sourceLanguage': 'en-US',
'sourceLanguages': ['en-US'],
'personModelId': '00000000-0000-0000-0000-000000000000'
}
],
'nextPage': {
'pageSize': 25,
'skip': 0,
'done': True
}
}
And this is my code in Python 3.x:
url = "https://api.videoindexer.ai/" + Location + "/Accounts/" + AzVIAccountID + "/Videos?pageSize=25&skip=0&accessToken=" + iAccessToken
response = requests.get(url,headers=hdr )
### Response: 200 OK
print("If Response=200 Script run is OK : " , response.status_code)
I want the result to be like this:
Question: Is there a possibility to fetch 'name', 'id', and 'create' of all videos which are showing in the JSON result (Response API) into a dataframe directly?
If data contains the data from the question you can do:
df = pd.DataFrame(data["results"])
columns_i_want = ["id", "name", "created"]
df = df[columns_i_want]
print(df)
Prints:
id name created
0 6ed909bd16 NewSeptemberFile20220908 2022-09-12T22:04:55.799+00:00
1 34344818e8 3September 2022-09-09T17:55:59.696+00:00
2 82da4b60ef film10 2022-08-22T14:24:08.442+00:00
3 7ea0c5e34a davide_quatela--people_in_frankfurt 2022-09-07T21:31:52.818+00:00
4 7c45ae7ffe Untitled project 2022-08-17T17:36:23.72+00:00
EDIT:
url = (
"https://api.videoindexer.ai/"
+ Location
+ "/Accounts/"
+ AzVIAccountID
+ "/Videos?pageSize=25&skip=0&accessToken="
+ iAccessToken
)
response = requests.get(url, headers=hdr)
data = response.json()
# make sure the correct response is sent:
print(data)
# if above command prints correct data, you can do next:
df = pd.DataFrame(data["results"])
columns_i_want = ["id", "name", "created"]
df = df[columns_i_want]
print(df)

Keep having this error : JSONDecodeError: Expecting value: line 2 column 1 (char 1)

JSON File
[https://drive.google.com/file/d/1Jb3OdoffyA71vYfojxLedZNPDLq9bn7b/view?usp=sharing]
I am trying to read the JSON file in Python, the JSON file is the same as the link above. The code that I wrote looks like this below
lst = []
for line in open(json_path,'r'):
lst.append(json.loads(line))
But for some reason, I kept having this error JSONDecodeError: Expecting value: line 2 column 1 (char 1) I am wondering did I do something wrong with code or the JSON file has an error in it?
Update
You can strip the break line (\n) out before using json.loads function (Thanks to #DeepSpace for the comment):
import json
lst = []
for line in open("sample.json",'r'):
stripped = line.strip("\n")
if stripped != "":
lst.append(json.loads(stripped))
lst
Also you can use ast module too:
import ast
lst = []
for line in open("sample.json",'r'):
if line.strip("\n") != "":
lst.append(ast.literal_eval(line))
Explanation
ast.literal_eval changes a dictionary or list in the shape of a string (such as "[1,2,3]"), to an useable dictionary or list in python (such as [1,2,3]).
The output of both codes above would be:
[{'content': [{'c_id': '002',
'p_id': 'P02',
'source': 'internet',
'type': 'org'},
{'c_id': '003', 'p_id': 'P03', 'source': 'internet', 'type': 'org'},
{'c_id': '005', 'p_id': 'K01', 'source': 'news', 'type': 'people'}],
'doc_id': '7098727',
'id': 'lni001',
'pub_date': '20220301',
'unique_id': '64WP-UI-POLI'},
{'content': [{'c_id': '002',
'p_id': 'P02',
'source': 'internet',
'type': 'org'},
{'c_id': '003', 'p_id': 'P03', 'source': 'internet', 'type': 'org'},
{'c_id': '005', 'p_id': 'K01', 'source': 'news', 'type': 'people'}],
'doc_id': '7098727',
'id': 'lni001',
'pub_date': '20220301',
'unique_id': '64WP-UI-POLI'},
{'content': [{'c_id': '002',
'p_id': 'P02',
'source': 'internet',
'type': 'org'},
{'c_id': '003', 'p_id': 'P03', 'source': 'internet', 'type': 'org'},
{'c_id': '005', 'p_id': 'K01', 'source': 'news', 'type': 'people'}],
'doc_id': '7098727',
'id': 'lni001',
'pub_date': '20220301',
'unique_id': '64WP-UI-POLI'},
{'content': [{'c_id': '012',
'p_id': 'K21',
'source': 'internet',
'type': 'location'},
{'c_id': '034', 'p_id': 'P17', 'source': 'news', 'type': 'people'},
{'c_id': '098', 'p_id': 'K54', 'source': 'news', 'type': 'people'}],
'doc_id': '7097889',
'id': 'lni002',
'pub_date': '20220301',
'unique_id': '64WP-UI-CFGT'},
{'content': [{'c_id': '012',
'p_id': 'K21',
'source': 'internet',
'type': 'location'},
{'c_id': '034', 'p_id': 'P17', 'source': 'news', 'type': 'people'},
{'c_id': '098', 'p_id': 'K54', 'source': 'news', 'type': 'people'}],
'doc_id': '7097889',
'id': 'lni002',
'pub_date': '20220301',
'unique_id': '64WP-UI-CFGT'}]

Django-ckeditor upload permission for all users; Add scroll bar; & automatically adjust editor width as per screen size

Issue 1:
I am using Django-CKEditor and when I am trying to upload any file or image in it then it is showing
Alert error: "Incorrect Server Response",
And when I checked in the terminal,
there it is showing "GET /admin/login/?next=/ckeditor/upload/ HTTP/1.1"
I don't know what to do to make this work! Please help me here...
Issue 2:
when I am copy-pasting 100 lines of text in the editor it increases its height rather providing any scroll bar in it,
here is the config code I am using:
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_CONFIGS = {
'default': {
'height': '200',
'width': 1250,
'toolbar_Basic': [
['Source', '-', 'Bold', 'Italic']
],
'toolbar_YourCustomToolbarConfig': [
{'name': 'document', 'items': ['Source']},
{'name': 'clipboard', 'items': ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo']},
'/',
{'name': 'basicstyles',
'items': ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat']},
{'name': 'paragraph',
'items': ['NumberedList', 'BulletedList', '-', 'Blockquote', '-',
'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl']},
{'name': 'links', 'items': ['Link', 'Unlink', 'Anchor']},
{'name': 'insert',
'items': ['Image', 'Table', 'SpecialChar']},
'/',
{'name': 'styles', 'items': ['Styles', 'Format', 'Font', 'FontSize']},
{'name': 'colors', 'items': ['TextColor', 'BGColor']},
{'name': 'tools', 'items': ['Maximize']},
],
'toolbar': 'YourCustomToolbarConfig', # put selected toolbar config here
# 'toolbarGroups': [{ 'name': 'document', 'groups': [ 'mode', 'document', 'doctools' ] }],
# 'height': 291,
# 'width': '100%',
# 'filebrowserWindowHeight': 725,
# 'filebrowserWindowWidth': 940,
# 'toolbarCanCollapse': True,
# 'mathJaxLib': '//cdn.mathjax.org/mathjax/2.2-latest/MathJax.js?config=TeX-AMS_HTML',
'tabSpaces': 4,
'extraPlugins': ','.join([
'uploadimage', # the upload image feature
# your extra plugins here
'div',
'autolink',
'autoembed',
'embedsemantic',
'autogrow',
# 'devtools',
'widget',
'lineutils',
'clipboard',
'dialog',
'dialogui',
'elementspath'
]),
}
}
Urls.py
path('ckeditor/', include('ckeditor_uploader.urls')),
models.py
from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField
class table_name(models.Model):
update_message = RichTextUploadingField(blank=True, null=True)
class Meta:
db_table = "table_name"
Issue 3:
How to make the width of the editor to automatically adjust as per screen.
Issue 1: try to replace
path('ckeditor/', include('ckeditor_uploader.urls')),
in your urls.py with the following
url(r'^ckeditor/upload/', login_required(ckeditor_views.upload), name='ckeditor_upload'),
url(r'^ckeditor/browse/', never_cache(login_required(ckeditor_views.browse)), name='ckeditor_browse'),
also add the following in your urls.py
from django.contrib.auth.decorators import login_required
from django.views.decorators.cache import never_cache
from ckeditor_uploader import views as ckeditor_views
Issue 2:
EDIT: you had an extraplugin "autogrow" and wrong height setting.
step 1: remove "autogrow";
step 2: replace
'height': '200',
with
'height':'200px', or 'height':200,
Issue 3:
replace
'width': 1250,
with
'width': '100%',
and in your html and css, make the div containing the ckeditor form to be responsive.
Note please pay special attention to ' ' in your code. I see you commented out lines that have correct settings for width and height.

Python - Iterating lists and dictionaries [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Hi I'm reposting this question but providing more information about what I'm trying to achieve. Its been driving me crazy for the last few days and I can't seem to make a progress. Basically, I have this data structure:
data_in =\
{'map': {'command_line': u'command goes here',
'scaninfo': {u'tcp': {'method': u'syn', 'services': u'80,443'}},
'stats': {'downhosts': u'0',
'elapsed': u'1.71',
'timestr': u'Thu Mar 20 18:18:09 2014',
'totalhosts': u'3',
'uphosts': u'3'}},
'scan': {u'2a00:2384:0:208f::13': {'addresses': {u'ipv6': u'2a00:2384:0:f467::13',
u'mac': u'00:gf:88:9:56:D5'},
'hostname': u'static.abc.com',
'status': {'reason': u'nd-response',
'state': u'up'},
u'tcp': {80: {'conf': u'3',
'cpe': '',
'extrainfo': '',
'name': u'http',
'product': '',
'reason': u'syn-ack',
'state': u'open',
'version': ''},
443: {'conf': u'3',
'cpe': '',
'extrainfo': '',
'name': u'https',
'product': '',
'reason': u'syn-ack',
'script': {u'ssl-cert': u'place holder'},
'state': u'open',
'version': ''}},
'vendor': {u'00:0C:29:7C:13:D3': u'VMware'}},
u'2a00:2384:0:208f::15': {'addresses': {u'ipv6': u'a848:2384:0:3456::15',
u'mac': u'00:gf:29:99:6D:96'},
'hostname': u'static.xyz.com',
'status': {'reason': u'nd-response',
'state': u'up'},
u'tcp': {80: {'conf': u'3',
'cpe': '',
'extrainfo': '',
'name': u'http',
'product': '',
'reason': u'syn-ack',
'state': u'open',
'version': ''},
443: {'conf': u'3',
'cpe': '',
'extrainfo': '',
'name': u'https',
'product': '',
'reason': u'syn-ack',
'script': {u'ssl-cert': u'place holder'},
'state': u'open',
'version': ''}},
'vendor': {u'00:0C:67:99:6f:96': u'VMware'}},
u'2a00:2384:0:208f::16': {'addresses': {u'ipv6': u'8938:8584:0:8685::16',
u'mac': u'00:54:29:fg:55:0F'},
'hostname': u'static.edf.com',
'status': {'reason': u'nd-response',
'state': u'up'},
u'tcp': {80: {'conf': u'3',
'cpe': '',
'extrainfo': '',
'name': u'http',
'product': '',
'reason': u'syn-ack',
'state': u'open',
'version': ''},
443: {'conf': u'3',
'cpe': '',
'extrainfo': '',
'name': u'https',
'product': '',
'reason': u'syn-ack',
'script': {u'ssl-cert': u'place holder'},
'state': u'open',
'version': ''}},
'vendor': {u'00:0C:55:AE:33:ff': u'VMware'}}}}
And need to create a simplified version of it that looks like this:
data_out =\
[{'address': u'2a00:2384:0:208f::13',
'hostname': u'static.bt.com',
'ports': [{80: {'reason': u'syn-ack', 'state': u'open'}},
{443: {'reason': u'syn-ack',
'ssl_cert': u'place holder',
'state': u'open'}}]}]
As per previous advice from #jonrsharpe I've created a helper function that enables me to find keys. This has proved helpful, but I still struggling to get the desired results.
def find_key(data, search_key, out=None):
"""Find all values from a nested dictionary for a given key."""
if out is None:
out = []
if isinstance(data, dict):
if search_key in data:
out.append(data[search_key])
for key in data:
find_key(data[key], search_key, out)
return out
Any help would be really appreciated here!
This isn't all that hard; you just have to go through and look at the data structure that leads to what you want - which is made much harder by poor formatting, so I re-indented your input and marked the keys (<==) and fields (!!!) you are seeking:
data_in = {
'map': {
'stats': {
'uphosts': u'3',
'timestr': u'Thu Mar 20 18:18:09 2014',
'downhosts': u'0',
'totalhosts': u'3',
'elapsed': u'1.71'
},
'scaninfo': {
u'tcp': {
'services': u'80,443',
'method': u'syn'
}
},
'command_line': u'command goes here'
},
'scan': { # <==
u'2a00:2384:0:208f::13': { # <== !!!
'status': {
'state': u'up',
'reason': u'nd-response'
},
'hostname': u'static.abc.com', # !!!
'vendor': {
u'00:0C:29:7C:13:D3': u'VMware'
},
'addresses': {
u'mac': u'00:gf:88:9:56:D5',
u'ipv6': u'2a00:2384:0:f467::13'
},
u'tcp': { # <==
80: { # <== !!!
'product': '',
'state': u'open', # !!!
'version': '',
'name': u'http',
'conf': u'3',
'extrainfo': '',
'reason': u'syn-ack', # !!!
'cpe': ''
},
443: { # <== !!!
'product': '',
'state': u'open', # !!!
'version': '',
'name': u'https',
'conf': u'3',
'script': { # <==
u'ssl-cert': u'place holder' # !!!
},
'extrainfo': '',
'reason': u'syn-ack', # !!!
'cpe': ''
}
}
},
u'2a00:2384:0:208f::15': {
'status': {
'state': u'up',
'reason': u'nd-response'
},
'hostname': u'static.xyz.com',
'vendor': {
u'00:0C:67:99:6f:96': u'VMware'
},
'addresses': {
u'mac': u'00:gf:29:99:6D:96',
u'ipv6': u'a848:2384:0:3456::15'
},
u'tcp': {
80: {
'product': '',
'state': u'open',
'version': '',
'name': u'http',
'conf': u'3',
'extrainfo': '',
'reason': u'syn-ack',
'cpe': ''
},
443: {
'product': '',
'state': u'open',
'version': '',
'name': u'https',
'conf': u'3',
'script': {
u'ssl-cert': u'place holder'
},
'extrainfo': '',
'reason': u'syn-ack',
'cpe': ''
}
}
},
u'2a00:2384:0:208f::16': {
'status': {
'state': u'up',
'reason': u'nd-response'
},
'hostname': u'static.edf.com',
'vendor': {
u'00:0C:55:AE:33:ff': u'VMware'
},
'addresses': {
u'mac': u'00:54:29:fg:55:0F',
u'ipv6': u'8938:8584:0:8685::16'
},
u'tcp': {
80: {
'product': '',
'state': u'open',
'version': '',
'name': u'http',
'conf': u'3',
'extrainfo': '',
'reason': u'syn-ack',
'cpe': ''
},
443: {
'product': '',
'state': u'open',
'version': '',
'name': u'https',
'conf': u'3',
'script': {
u'ssl-cert': u'place holder'
},
'extrainfo': '',
'reason': u'syn-ack',
'cpe': ''
}
}
}
}
}
and likewise for your desired output (with appropriate adjustments):
data_out = [
{
'address': u'2a00:2384:0:208f::13',
'hostname': u'static.bt.com',
'ports': {
80: {
'state': u'open',
'reason': u'syn-ack'
},
443: {
'ssl_cert': u'place holder',
'state': u'open',
'reason': u'syn-ack'
}
}
}
]
then the extraction becomes:
def remap_port(port, port_data):
result = {
"state": port_data["state"],
"reason": port_data["reason"]
}
try:
result["ssl_cert"] = port_data["script"]["ssl-cert"]
except KeyError:
pass
return port, result
def remap_scanned_address(address, address_data):
return {
"address": address,
"hostname": address_data["hostname"],
"ports": dict(remap_port(port, port_data) for port,port_data in address_data["tcp"].items())
}
def remap_scan_data(data_in):
return [remap_scanned_address(address, address_data) for address, address_data in data_in["scan"].items()]
data_out = remap_scan_data(data_in)
which results in the desired output,
[{'address': u'2a00:2384:0:208f::13',
'hostname': u'static.abc.com',
'ports': {80: {'reason': u'syn-ack', 'state': u'open'},
443: {'reason': u'syn-ack',
'ssl_cert': u'place holder',
'state': u'open'}}},
{'address': u'2a00:2384:0:208f::15',
'hostname': u'static.xyz.com',
'ports': {80: {'reason': u'syn-ack', 'state': u'open'},
443: {'reason': u'syn-ack',
'ssl_cert': u'place holder',
'state': u'open'}}},
{'address': u'2a00:2384:0:208f::16',
'hostname': u'static.edf.com',
'ports': {80: {'reason': u'syn-ack', 'state': u'open'},
443: {'reason': u'syn-ack',
'ssl_cert': u'place holder',
'state': u'open'}}}]

Categories