Socialify

Folder ..

Viewing context_processors.py
216 lines (175 loc) • 6.6 KB

  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import re

import akismet
import dotenv
import requests
from bs4 import BeautifulSoup
from django.conf import settings
from django.core.cache import cache
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, guess_lexer

from .models import Category, Comment, Post

dotenv.load_dotenv()

akismet_api = akismet.Akismet(
    key=os.getenv("AKISMET_API_KEY"),
    blog_url="https://preview.thatcomputerscientist.com"
    if settings.DEBUG
    else "https://thatcomputerscientist.com",
)


def check_spam(user_ip, user_agent, comment, author):
    akismet_data = {
        "comment_type": "comment",
        "comment_author": author,
        "comment_content": comment,
        "is_test": settings.DEBUG,
    }
    return akismet_api.comment_check(user_ip, user_agent, **akismet_data)


def add_excerpt(post):
    soup = BeautifulSoup(post.body, "html.parser")

    # Create excerpt, count min 1000 characters and max upto next paragraph
    excerpt = ""
    for paragraph in soup.find_all("p"):
        paragraph = "<p>" + str(paragraph.text) + "</p>"
        excerpt += str(paragraph)

        if len(excerpt) >= 1000:
            break
    return excerpt


def add_num_comments(post):
    num_comments = Comment.objects.filter(post=post).count()
    return num_comments


def recent_posts():
    recent_posts = Post.objects.filter(is_public=True).order_by("-date")[:5]
    for post in recent_posts:
        post.excerpt = add_excerpt(post)
        post.num_comments = add_num_comments(post)
    return recent_posts


def categories(request):
    categories = Category.objects.all()[0:5]
    return {"categories": categories}


def archives(request):
    archives = Post.objects.filter(is_public=True).dates("date", "month", order="DESC")[
        0:5
    ]
    return {"archives": archives}


def avatar_list():
    avatar_list = {}
    directory = os.path.join(settings.BASE_DIR, "static", "images", "avatars")
    for directory in os.listdir(directory):
        # ignore hidden files
        if directory.startswith("."):
            continue
        avatar_list[directory] = os.listdir(
            os.path.join(settings.BASE_DIR, "static", "images", "avatars", directory)
        )
        # remove hidden files
        for file in avatar_list[directory]:
            if file.startswith("."):
                avatar_list[directory].remove(file)
    return avatar_list


def highlight_code_blocks(code_block, language=None):
    # replace &nbsp; with space
    try:
        cb = code_block.string
    except:
        cb = code_block
    cb = cb.replace("\xa0", " ")

    # guess the language as there is no data-lang attribute
    if language:
        try:
            lexer = get_lexer_by_name(language.strip())
        except:
            lexer = get_lexer_by_name("text")
    else:
        try:
            lexer = guess_lexer(cb)
        except:
            lexer = get_lexer_by_name("text")
    # highlight the code
    formatter = HtmlFormatter(noclasses=True, style="native", wrapcode=True)
    highlighted_code = highlight(cb, lexer, formatter)

    return highlighted_code


def check_link_safety(link):
    api_key = os.getenv("GOOGLE_SAFE_BROWSING_API_KEY")
    api_url = "https://safebrowsing.googleapis.com/v4/threatMatches:find"
    cache_key = f"link_safety:{link}"
    cache_timeout = 60 * 60 * 24 * 7  # 7 days

    # Check if the result is already cached
    cached_result = cache.get(cache_key)
    if cached_result is not None:
        return cached_result

    payload = {
        "threatInfo": {
            "threatTypes": [
                "MALWARE",
                "SOCIAL_ENGINEERING",
                "UNWANTED_SOFTWARE",
                "POTENTIALLY_HARMFUL_APPLICATION",
            ],
            "platformTypes": ["ANY_PLATFORM"],
            "threatEntryTypes": ["URL"],
            "threatEntries": [{"url": link}],
        }
    }

    headers = {"Content-Type": "application/json"}

    params = {"key": api_key, "alt": "json"}

    response = requests.post(api_url, params=params, headers=headers, json=payload)
    if response.status_code == 200:
        # Successful API call
        matches = response.json().get("matches", [])
        # Cache the result
        cache.set(cache_key, len(matches) == 0, cache_timeout)
        return len(matches) == 0
    else:
        # Handle API error
        print(f"Safe Browsing API error: {response.content}")

    return False


def comment_processor(comment):
    # escape html tags
    comment = re.sub(r"<", "&lt;", comment)
    comment = re.sub(r">", "&gt;", comment)

    # any text between ``` and ``` must be highlighted as code
    code_blocks = re.findall(r"```(.+?)```", comment, re.DOTALL)
    for code_block in code_blocks:
        if code_block.startswith("lang-"):
            language = code_block.split("\n")[0].replace("lang-", "")
            code_block = code_block.replace("lang-" + language + "\n", "")
            # comment = highlight_code_blocks(code_block.replace('&lt;', '<').replace('&gt;', '>'), language)
            comment = comment.replace(
                "```lang-" + language + "\n" + code_block + "```",
                highlight_code_blocks(
                    code_block.replace("&lt;", "<").replace("&gt;", ">"), language
                ),
            )
        else:
            comment = comment.replace(
                "```" + code_block + "```",
                highlight_code_blocks(
                    code_block.replace("&lt;", "<").replace("&gt;", ">")
                ),
            )

    # any http or https links must be converted to anchor tags
    links = re.findall(r"(https?://[^\s]+)", comment)
    for link in links:
        # check if the link is safe
        if check_link_safety(link):
            comment = comment.replace(
                link, '<a href="' + link + '" target="_blank">' + link + "</a>"
            )
        else:
            # do not replace the link if it is not safe. Add a warning message after the link instead
            comment = comment.replace(
                link,
                link
                + '<span style="color: red"> (Seems unsafe! Proceed with caution)</span>',
            )

    # retain line breaks, for every newline character, add a <br> tag
    comment = comment.replace("\n", "<br>")

    # replace multiple <br> tags with a single <br> tag
    comment = re.sub(r"<br>(\s*<br>)+", "<br><br>", comment)

    comment = re.sub(r"\*\*(.+?)\*\*", r"<b>\1</b>", comment)
    comment = re.sub(r"__(.+?)__", r"<i>\1</i>", comment)
    comment = re.sub(r"~~(.+?)~~", r"<s>\1</s>", comment)

    # remove any br tags at the end of the comment
    comment = re.sub(r"<br>$", "", comment)

    return comment