-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogic.py
101 lines (76 loc) · 2.53 KB
/
logic.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
import os
from uuid import uuid4
from io import open as iopen
import requests
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods.posts import GetPosts
from bs4 import BeautifulSoup
from django.utils import timezone
from django.conf import settings
from comms.models import NewsItem
from utils.function_cache import cache
@cache(120)
def get_posts(details, increment, offset):
xmlrpc_url = '{base_url}{slash}xmlrpc.php'.format(
base_url=details.url,
slash='/' if not details.url.endswith('/') else '',
)
wp = Client(
xmlrpc_url,
details.username,
details.password,
)
posts = wp.call(GetPosts({'number': increment, 'offset': offset}))
return posts
def import_posts(posts_to_import, posts, request, import_object):
for post in posts:
if post.id in posts_to_import:
defaults = {
'body': post.content,
'posted': post.date,
'posted_by': import_object.user,
'start_display': timezone.now(),
}
new_news_item, c = NewsItem.objects.get_or_create(
content_type=request.model_content_type,
object_id=request.site_type.pk,
title=post.title,
defaults=defaults,
)
tags = [tag.name for tag in post.terms]
new_news_item.set_tags(tags)
if c:
rewrite_image_paths(new_news_item)
import_object.delete()
def rewrite_image_paths(news_item):
soup = BeautifulSoup(news_item.body, 'html.parser')
images = soup.find_all('img')
print(images)
for image in images:
img_src = image['src'].split('?')[0]
path = download_and_store_image(img_src)
image['src'] = path
news_item.body = soup.prettify()
news_item.save()
def download_and_store_image(image_source):
image = requests.get(image_source)
image.raw.decode_content = True
name = os.path.basename(image_source)
fileurl = save_media_file(
name,
image.content,
)
return fileurl
def save_media_file(original_filename, source_file):
filename = str(uuid4()) + str(os.path.splitext(original_filename)[1])
filepath = '{media_root}/{filename}'.format(
media_root=settings.MEDIA_ROOT,
filename=filename,
)
fileurl = '{media_url}{filename}'.format(
media_url=settings.MEDIA_URL,
filename=filename,
)
with iopen(filepath, 'wb') as file:
file.write(source_file)
return fileurl