Skip to content

Instantly share code, notes, and snippets.

@joeydi
Last active December 28, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeydi/7488831 to your computer and use it in GitHub Desktop.
Save joeydi/7488831 to your computer and use it in GitHub Desktop.
Import a csv of location data into Wordpress. Data is formatted to work with the SimpleMap plugin. Depends on the wordpress_xmlrpc and pygeocoder libraries.
Category Type Name Address City Zip Code Phone Number URL
General Bank Chase Bank 36132 Hidden Springs Rd Wildomar 92595 (951) 678-9231 https://www.chase.com/
Public Services Fire Station Station 61 32637 Gruwell St. Wildomar 92595 951-246-2337 http://cityofwildomar.org
Entertainment Museum Temecula Valley Museum 28314 Mercedes St Temecula 92592 (951) 694-6450 http://temeculavalleymuseum.com/
import csv
from urllib import quote_plus
from pygeocoder import Geocoder
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost, EditPost
from settings import settings
def save_row(row, wp):
# Parse location data from row
location = {
'name': row.get('Name', ''),
'category': row.get('Category', '').replace('&', '&'),
'address': row.get('Address', ''),
'city': row.get('City', ''),
'zip_code': row.get('Zip Code', ''),
'state': row.get('State', 'CA'),
'country': row.get('Country', 'US'),
'phone': row.get('Phone Number', ''),
'url': row.get('URL', ''),
}
# Geocode address to get coordinates
address_str = '%s, %s, %s, %s' % (location['address'], location['city'], location['state'], location['zip_code'])
results = Geocoder.geocode(address_str)
location['latitude'] = results[0].coordinates[0]
location['longitude'] = results[0].coordinates[1]
# Create the post object
post = WordPressPost()
post.post_status = 'publish'
post.post_type = 'sm-location'
post.title = location['name']
# Set the custom field values
post.custom_fields = []
post.custom_fields.append({
'key': 'location_address',
'value': location['address']
})
post.custom_fields.append({
'key': 'location_city',
'value': location['city']
})
post.custom_fields.append({
'key': 'location_zip',
'value': location['zip_code']
})
post.custom_fields.append({
'key': 'location_state',
'value': location['state']
})
post.custom_fields.append({
'key': 'location_country',
'value': location['country']
})
post.custom_fields.append({
'key': 'location_lat',
'value': location['latitude']
})
post.custom_fields.append({
'key': 'location_lng',
'value': location['longitude']
})
post.custom_fields.append({
'key': 'location_phone',
'value': location['phone']
})
post.custom_fields.append({
'key': 'location_url',
'value': location['url']
})
# Set the taxonomy terms
post.terms_names = {
'sm-category': [location['category']]
}
# Save the post
try:
post.id = wp.call(NewPost(post))
print 'Saved ID %s: %s' % (post.id, post.title)
except Exception, e:
print 'Error Saving: %s \n %s' % (post.title, e)
if __name__ == "__main__":
wp = Client(settings['endpoint'], settings['username'], settings['password'])
with open(settings['locations'], 'r') as f:
reader = csv.DictReader(f)
for row in reader:
save_row(row, wp)
settings = {
'locations': 'PATH_TO_YOUR_CSV',
'endpoint': 'URL_OF_XMLRPC_ENDPOINT',
'username': 'YOUR_USERNAME',
'password': 'YOUR_PASSWORD',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment