-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_wp.py
executable file
·147 lines (105 loc) · 4.82 KB
/
import_wp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python
import MySQLdb
import sys,os
import string as s
os.environ['DJANGO_SETTINGS_MODULE'] = 'MyDjangoSites.settings'
from datetime import datetime
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from django.contrib.comments.models import Comment
from django.db.utils import IntegrityError
from MyDjangoSites.blog.models import Entry, Tag
from django.db import connection
unic= lambda x: x #.decode('latin1')
from random import choice
def RandStr(length=3, chars=s.letters + s.digits):
return ''.join([choice(chars) for i in xrange(length)])
pwds=open('pwds').readlines()
pwds=map(s.strip,pwds)
blogs=[{'sitename':'BlogBlog','dbuser':'blogblog','dbname':'blogblog','pwd':pwds[0]},
{'sitename':'Fiket','dbuser':'fiket','dbname':'fiket','pwd':pwds[1]},
{'sitename':'Atheist','dbuser':'atheist','dbname':'atheistundgut','pwd':pwds[2]},
{'sitename':'ApparentBrightness','dbuser':'appbright','dbname':'appbright','pwd':pwds[4]},
{'sitename':'Erderhitzung','dbuser':'erderhitzung','dbname':'erderhitzung','pwd':pwds[5]},
]
########################
def do_tags(cursor):
cursor.execute('select name,slug from wp_terms;')
for name,slug in cursor.fetchall():
print name, slug
t=Tag(slug=slug)
t.name=name
t.save()
t.site.add(SITE)
t.save()
def do_comments(cursor,ID,entry):
cursor.execute('select comment_author,comment_author_email,comment_author_url,comment_author_IP,comment_date,comment_content from wp_comments where comment_approved=1 and comment_post_ID=%s'%ID)
comments=cursor.fetchall()
for comment_author,comment_author_email,comment_author_url,comment_author_IP,comment_date,comment_content in comments:
comm=Comment(content_object=entry,site=SITE,user_name=unic(comment_author)[:49],user_email=comment_author_email,user_url=comment_author_url,comment=unic(comment_content),ip_address='127.0.0.1',submit_date=comment_date,is_public=True,is_removed=False)
try: comm.save(force_insert=True)
except Exception, e:
print comment_author,comment_author_email,comment_author_url,comment_author_IP,comment_date,comment_content
print Exception, e
if 'Incorrect string value' in e:
comm.comment=comment_content.decode('latin1')
comm.save(force_insert=True)
def do_entries(cursor):
cursor.execute('select ID,post_date,post_title,post_content,post_name from wp_posts where post_status="publish" and (post_type="post");')
posts=cursor.fetchall()
for ID,post_date,post_title,post_content,post_name in posts:
entry=Entry(pub_date=post_date,title=unic(post_title),lang=LANG,
body=unic(post_content),author=AUTHOR,slug=post_name[:50])
entry.save(force_insert=True)
entry.site.add(SITE)
try: entry.save()
except IntegrityError,e:
print e
if 'column slug is not unique' in e:
entry.slug=entry.slug+RandStr()
entry.save()
if 'Incorrect string value' in e:
entry.body=post_content.decode('latin1')
entry.save(force_insert=True)
cursor.execute('select name,slug from wp_terms, wp_posts, wp_term_relationships, wp_term_taxonomy where wp_posts.ID="%s" AND wp_posts.ID=wp_term_relationships.object_ID AND wp_term_relationships.term_taxonomy_id=wp_term_taxonomy.term_taxonomy_id AND wp_term_taxonomy.term_id=wp_terms.term_id'%ID)
tags=cursor.fetchall()
for tag_name,tag_slug in tags:
tag=Tag.objects.get(slug=tag_slug)
entry.tags.add(tag)
entry.save()
do_comments(cursor,ID,entry)
## MAIN LOOP STARTS HERE
for blog in blogs:
conn = MySQLdb.connect (host = "localhost",
user = blog['dbuser'],
passwd = blog['pwd'],
db = blog['dbname'])
cursor = conn.cursor ()
LANG='g'
SITE=Site.objects.get(name=blog['sitename'])
AUTHOR=User.objects.get(pk=1)
do_tags(cursor)
do_entries(cursor)
####### special operations
# get entries tagged Europa into the EU-site
# and the other tags too.
et=Tag.objects.get(slug='europa')
eus=Site.objects.get(name='EU')
ee=Entry.objects.filter(tags=et)
for e in ee:
e.site.add(eus)
e.save()
for t in e.tags.all():
if t.slug != 'europa':
t.site.add(eus)
ALLSITE=Site.objects.get(name='AllBlog')
for tag in Tag.objects.all():
tag.site.add(ALLSITE)
tag.save()
for entry in Entry.objects.all():
entry.site.add(ALLSITE)
entry.save()
### replace ops
cursor=connection.cursor()
cursor.execute('UPDATE blog_entry SET body = replace(body, "</object>", "</object>\n");')
connection.commit()