In [47]:
import urllib.request 
import json
from pprint import pprint
import MySQLdb as mariadb
connection = mariadb.connect('192.168.132.139','user','p','SCEBase')
cursor = connection.cursor()

In [69]:
query = ("SELECT id,post_title,post_name,post_date,"
+"post_excerpt,post_content,post_type FROM scecms_posts WHERE post_status='publish'")

cursor.execute(query)


Out[69]:
5131

In [74]:
post_types = {}
counter = 1
posts = []
for doc in cursor:
    post_type = doc[6]
    docdict = {
        'id': doc[0],
        'title': doc[1],
        'name': doc[2],
        'date': doc[3],
        'excerpt': doc[4],
        'content': doc[5],
        'type': doc[6]
    }
    posts.append(docdict)
    if post_type in post_types:
        post_types[post_type] += 1
    else:
        post_types[post_type] = 1

In [75]:
print(len(post_types))
pprint (post_types)
print(len(posts))


14
{'acf-field': 146,
 'acf-field-group': 17,
 'document': 222,
 'entry': 1988,
 'image': 1961,
 'ml-slider': 1,
 'nav_menu_item': 12,
 'overview': 39,
 'page': 12,
 'partner': 66,
 'recording': 4,
 'staff-member': 619,
 'text-blocks': 2,
 'video': 42}
5131

In [85]:
pposts = []
for post in posts:
    if post['type'] == 'overview':
        pposts.append(post)
pprint(pposts[0])


{'content': 'Even into the 21<sup>st</sup> century, South Carolina\x92s '
            'economy and culture are tied to agriculture. Historically '
            'important crops include rice, indigo, cotton, soybeans, peaches, '
            'and tobacco. With more than 4 million acres of farmland, the '
            'state produces $3 billion annually, between crops and livestock. '
            'Check out the entries on rice, indigo, cotton, soybeans, peaches, '
            'and tobacco to learn more.',
 'date': datetime.datetime(2015, 11, 9, 14, 55, 16),
 'excerpt': '',
 'id': 338,
 'name': 'agriculture-south-carolina',
 'title': 'Agriculture',
 'type': 'overview'}

In [42]:
url = 'https://www.scencyclopedia.org/sce/wp-json/wp/v2/posts?categories=1'
response = urllib.request.urlopen(url)
bytes = response.read()
print(response.headers)
text = bytes.decode('utf-8')
ddata = json.loads(text)
pprint(ddata)


Date: Tue, 23 Jan 2018 19:21:22 GMT
Server: Apache/2.4.18 (Ubuntu)
X-Robots-Tag: noindex
Link: <https://www.scencyclopedia.org/sce/wp-json/>; rel="https://api.w.org/"
X-Content-Type-Options: nosniff
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
Access-Control-Allow-Headers: Authorization, Content-Type
X-WP-Total: 0
X-WP-TotalPages: 0
Allow: GET
Content-Length: 4
Content-Type: application/json; charset=UTF-8
Connection: close


[]

In [22]:
print(ddata['routes'].keys())


dict_keys(['/yoast/v1', '/wp/v2/types', '/wp/v2/users/(?P<id>[\\d]+)', '/wp/v2/categories', '/wp/v2/categories/(?P<id>[\\d]+)', '/wp/v2/comments/(?P<id>[\\d]+)', '/wp/v2/tags/(?P<id>[\\d]+)', '/wp/v2/pages/(?P<id>[\\d]+)', '/wp/v2/pages/(?P<parent>[\\d]+)/revisions', '/wp/v2/pages', '/wp/v2/posts/(?P<parent>[\\d]+)/revisions', '/', '/wp/v2/settings', '/yoast/v1/ryte', '/yoast/v1/statistics', '/wp/v2/comments', '/wp/v2/users', '/wp/v2/posts/(?P<id>[\\d]+)', '/wp/v2/taxonomies/(?P<taxonomy>[\\w-]+)', '/wp/v2/statuses', '/wp/v2/media', '/wp/v2/posts', '/yoast/v1/reindex_posts', '/wp/v2/types/(?P<type>[\\w-]+)', '/oembed/1.0', '/wp/v2/taxonomies', '/wp/v2', '/wp/v2/media/(?P<id>[\\d]+)', '/oembed/1.0/proxy', '/wp/v2/users/me', '/wp/v2/tags', '/yoast/v1/configurator', '/oembed/1.0/embed', '/wp/v2/posts/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)', '/wp/v2/pages/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)', '/wp/v2/statuses/(?P<status>[\\w-]+)'])

In [ ]:
print