-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to uniform opengraph, microdata and microformat to json-ld
* Add uniform and tests for opengraph, microdata and microformat * from six import urljoin urlparse * Add six and mf2py to requirements * Add tests for uniform.py * add files for tests * fix test * fix uniform, update notes, add uniform to tool * fix tests * Fix the test fix * update readme * update readme * update readme * update readme * update readme * update readme * update readme * add new arguments in tools, remove FIXME in uniform, fix readme
- Loading branch information
Showing
13 changed files
with
502 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,67 @@ | ||
from six.moves.urllib.parse import urlparse, urljoin | ||
|
||
|
||
def _uopengraph(extracted): | ||
out = [] | ||
for obj in extracted: | ||
flattened = dict(obj['properties']) | ||
t = flattened.pop('og:type', None) | ||
if t: | ||
flattened['@type'] = t | ||
flattened['@context'] = obj['namespace'] | ||
out.append(flattened) | ||
return out | ||
|
||
|
||
def _umicrodata_microformat(extracted, schema_context): | ||
res = [] | ||
if isinstance(extracted, list): | ||
for obj in extracted: | ||
res.append(flatten_dict(obj, schema_context, True)) | ||
elif isinstance(extracted, dict): | ||
res.append(flatten_dict(extracted, schema_context, False)) | ||
|
||
return res | ||
|
||
|
||
def flatten_dict(d, schema_context, add_context): | ||
out = dict(d) | ||
typ = out.pop('type', None) | ||
if not typ: | ||
return d | ||
|
||
if isinstance(typ, list): | ||
out['@type'] = typ | ||
context = schema_context | ||
else: | ||
context, typ = infer_context(typ, schema_context) | ||
out['@type'] = typ | ||
|
||
if add_context: | ||
out['@context'] = context | ||
|
||
props = out.pop('properties', {}) | ||
for field, value in props.items(): | ||
if isinstance(value, dict): | ||
value = flatten_dict(value, schema_context, False) | ||
elif isinstance(value, list): | ||
value = [ | ||
flatten_dict(o, schema_context, False) | ||
if isinstance(o, dict) else o | ||
for o in value | ||
] | ||
out[field] = value | ||
return out | ||
|
||
|
||
def infer_context(typ, context='http://schema.org'): | ||
parsed_context = urlparse(typ) | ||
if parsed_context.netloc: | ||
base = ''.join([parsed_context.scheme, '://', parsed_context.netloc]) | ||
if parsed_context.path and parsed_context.fragment: | ||
context = urljoin(base, parsed_context.path) | ||
typ = parsed_context.fragment.strip('/') | ||
elif parsed_context.path: | ||
context = base | ||
typ = parsed_context.path.strip('/') | ||
return context, typ |
This file contains 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 |
---|---|---|
|
@@ -7,4 +7,5 @@ requests | |
rdflib | ||
rdflib-jsonld | ||
mf2py | ||
w3lib | ||
six | ||
w3lib |
This file contains 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
This file contains 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,33 @@ | ||
[ | ||
{ | ||
"@type": ["h-entry"], | ||
"@context": "http://microformats.org/wiki/", | ||
"name": [ | ||
"Microformats are amazing" | ||
], | ||
"author": [ | ||
{ | ||
"@type": ["h-card"], | ||
"name": [ | ||
"W. Developer" | ||
], | ||
"url": [ | ||
"http://example.com" | ||
], | ||
"value": "W. Developer" | ||
} | ||
], | ||
"published": [ | ||
"2013-06-13 12:00:00" | ||
], | ||
"summary": [ | ||
"In which I extoll the virtues of using microformats." | ||
], | ||
"content": [ | ||
{ | ||
"html": "\n<p>Blah blah blah</p>\n", | ||
"value": "\nBlah blah blah\n" | ||
} | ||
] | ||
} | ||
] |
Oops, something went wrong.