Socialify

Folder ..

Viewing views.py
197 lines (166 loc) • 7.0 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
import json
from io import BytesIO

import requests
from captcha.image import ImageCaptcha
from django.core.files.base import ContentFile
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from PIL import Image

from blog.models import Post
from users.tokens import CaptchaTokenGenerator

from .models import PostImage, RepositoryTitle

# from .github import get_cover

# Create your views here.
@csrf_exempt
def tex(request):
    # get expression from request query
    expression = request.GET.get('expr').replace('"', '').strip()
    if not expression:
        return HttpResponse('No expression provided!', status=400)

    import requests

    image = requests.get('https://latex.codecogs.com/png.image?%5Cinline%20%5Clarge%20%5Cdpi%7B200%7D%5Cbg%7Btransparent%7D' + expression).content

    # Image is a transparent GIF with black text. Invert the colors.
    image = Image.open(BytesIO(image))
    image = image.convert('RGBA')
    image = Image.eval(image, lambda x: 255 - x)

    # Convert back to gif and return
    output = BytesIO()
    image.save(output, format='GIF')
    return HttpResponse(output.getvalue(), content_type='image/gif')

@csrf_exempt
def post_image(request, size, post_id):
    post_id = post_id.replace('.gif', '')
    pi = Post.objects.get(id=post_id)
    if not pi:
        return HttpResponse('No image found!', status=404)
    
    # open image and return
    image = pi.post_image
    with open(image.path, 'rb') as f:
        # resize image
        size = int(size)
        if size != 0:

            # set min and max size
            if size < 100:
                size = 100
            elif size > 1000:
                size = 1000
            
            image = Image.open(f)
            # resize width to size, compute height
            width, height = image.size
            height = int(height * (size / width))
            width = size

            # resize image
            image = image.resize((width, height), Image.ANTIALIAS)
            output = BytesIO()
            image.save(output, format='GIF')
            return HttpResponse(output.getvalue(), content_type='image/gif')
        else:
            return HttpResponse(f.read(), content_type='image/gif')

@csrf_exempt
def get_image(request, post_id, image_name):
    # get image from post_id
    pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image_name)
    if not pi:
        return HttpResponse('No image found!', status=404)
    
    # open image and return
    image = pi[0].image
    with open(image.path, 'rb') as f:
        image_file = f.read()
        # convert to gif
        image = Image.open(BytesIO(image_file))
        # check image format
        if image.format != 'GIF':
            image = image.convert('RGBA')
            output = BytesIO()
            image.save(output, format='GIF')
            image_file = output.getvalue()
        return HttpResponse(image_file, content_type='image/gif')

@csrf_exempt
def cover_image(request, repository):
    force_reload = request.GET.get('force_reload')
    repository = repository.replace('.gif', '')
    # check if the image is in RepositoryTitles
    try:
        if force_reload:
            raise Exception('Force reload')
        repository_title = RepositoryTitle.objects.get(repository=repository)
        image = repository_title.image
    except:
        # image is not in RepositoryTitles
        # get image
        url = 'https://socialify.thatcomputerscientist.com/luciferreeves/{}/png?font=KoHo&language=1&language2=1&name=1&theme=Dark&pattern=Solid'.format(repository)
        image = requests.get(url).content

        # reduce image size to 320x160
        image = Image.open(BytesIO(image))
        image = image.resize((320, 160), Image.ANTIALIAS)

        # remove black background
        image = image.convert('RGBA').getdata()
        new_data = []
        for item in image:
            if item[0] == 0 and item[1] == 0 and item[2] == 0:
                new_data.append((255, 255, 255, 0))
            else:
                new_data.append(item)

        # Convert back to png and return
        output = BytesIO()
        image = Image.new('RGBA', (320, 160))
        image.putdata(new_data)
        image.save(output, format='GIF')
        image = output.getvalue()

        # save image to RepositoryTitles
        image = ContentFile(image, name='{}.png'.format(repository))
        repository_title = RepositoryTitle(repository=repository, image=image)
        repository_title.save()

    return HttpResponse(image, content_type='image/gif')


def upload_image(request):
    if request.method == 'POST':
        if not request.user.is_authenticated and not request.user.is_staff:
            return HttpResponse('Unauthorized', status=401)
        if not request.FILES.get('image'):
            return HttpResponse('No image provided!', status=400)
        if not request.POST.get('id'):
            return HttpResponse('No id provided!', status=400)
        
        # upload image to PostImage model
        image = request.FILES['image']
        post_id = request.POST['id']
        # check if image already exists
        pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image.name)
        if pi:
            # image already exists, delete it
            pi[0].delete()
        # save image to post_id
        pi = PostImage(image=image, post=Post.objects.get(id=post_id), name=image.name)
        pi.save()
        response = {
            'url': '/ignis/image/{}/{}'.format(post_id, pi.name)
        }
        return HttpResponse(json.dumps(response), content_type='application/json')
    return HttpResponse('Method not allowed', status=405)

def captcha_image(request, captcha_string):
    captcha = CaptchaTokenGenerator().decrypt(captcha_string)
    imgcaptcha = ImageCaptcha()
    data = imgcaptcha.generate(captcha)
    return HttpResponse(data, content_type='image/png')

def socialify(request):
    repo = request.GET.get('repo')
    theme = request.GET.get('theme')
    font = request.GET.get('font')
    pattern = request.GET.get('pattern')
    name = request.GET.get('name')
    description = request.GET.get('description')
    language_1 = request.GET.get('language_1')
    language_2 = request.GET.get('language_2')
    stargazers = request.GET.get('stargazers')
    forks = request.GET.get('forks')
    issues = request.GET.get('issues')
    pulls = request.GET.get('pulls')

    url = 'https://socialify.thatcomputerscientist.com/{}/png?description={}&font={}&forks={}&issues={}&language={}&language2={}&name={}&owner=1&pattern={}&pulls={}&stargazers={}&theme={}'.format(repo, description, font, forks, issues, language_1, language_2, name, pattern, pulls, stargazers, theme)

    req = requests.get(url)
    image = req.content
    status = req.status_code

    if status == 200:
        return HttpResponse(image, content_type='image/png')
    else:
        with open('static/images/site/utgi.gif', 'rb') as f:
            image = f.read()
            return HttpResponse(image, content_type='image/gif')