Proper base64 encode/decode

This commit is contained in:
Samuel Clay 2020-07-01 16:59:21 -04:00
parent a8b69dd2b8
commit 6f14455115
7 changed files with 35 additions and 15 deletions

View file

@ -1,3 +1,10 @@
{
"python.pythonPath": "venv/bin/python3.7"
"python.pythonPath": "/usr/local/bin/python3",
"python.linting.enabled": true,
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.pylamaEnabled": false,
"python.linting.flake8Args": [
"--ignore=E501,W293,W503,W504,E302,E722,E226,E221"
]
}

View file

@ -21,3 +21,12 @@ pip install -r requirements.txt
pip install -r requirements.txt
./manage.py migrate
## Django 1.11
pip install -r requirements.txt
./manage.py migrate
## Django 2.0 (python 3)
mkvirutalenv -p python3 newsblur3

View file

@ -80,8 +80,8 @@ def logout(request):
def add_site_load_script(request, token):
code = 0
usf = None
profile = None;
user_profile = None;
profile = None
user_profile = None
def image_base64(image_name, path='icons/circular/'):
image_file = open(os.path.join(settings.MEDIA_ROOT, 'img/%s%s' % (path, image_name)))
return base64.b64encode(image_file.read())

View file

@ -1,4 +1,6 @@
import urllib.request, urllib.error, urllib.parse
import urllib.request
import urllib.error
import urllib.parse
import lxml.html
import numpy
import scipy
@ -9,7 +11,7 @@ import operator
import gzip
import datetime
import requests
import codecs
import base64
import http.client
from PIL import BmpImagePlugin, PngImagePlugin, Image
from socket import error as SocketError
@ -107,7 +109,7 @@ class IconImporter(object):
k.key = self.feed.s3_icons_key
k.set_metadata('Content-Type', 'image/png')
k.set_metadata('Expires', expires)
k.set_contents_from_string(image_str.decode('base64'))
k.set_contents_from_string(base64.b64decode(image_str))
k.set_acl('public-read')
self.feed.s3_icon = True
@ -127,7 +129,7 @@ class IconImporter(object):
try:
image_file.seek(0)
header = struct.unpack('<3H', image_file.read(6))
except Exception as e:
except Exception:
return
# Check magic
@ -382,7 +384,7 @@ class IconImporter(object):
index_max = scipy.argmax(counts)
peak = codes.astype(int)[index_max]
print(f" ---> Color: {peak}")
color = codecs.decode(''.join(chr(c) for c in peak), 'hex')
color = "{:02x}{:02x}{:02x}".format(peak[0], peak[1], peak[2])
print(f" ---> Color: {color} {peak}")
return color[:6]
@ -392,4 +394,4 @@ class IconImporter(object):
image.save(output, 'png', quality=95)
contents = output.getvalue()
output.close()
return contents.encode('base64')
return base64.b64encode(contents)

View file

@ -9,6 +9,7 @@ import mongoengine as mongo
import zlib
import hashlib
import redis
import base64
import pymongo
import html.parser
import urllib.parse
@ -286,7 +287,7 @@ class Feed(models.Model):
last_pk = cls.objects.latest('pk').pk
for f in xrange(offset, last_pk, 1000):
print(f" ---> {f} / {last_pk} ({str(float(f)/last_pk*100)[:2]}%")
print(" ---> {f} / {last_pk} ({pct}%)".format(f=f, last_pk=last_pk, pct=str(float(f)/last_pk*100)[:2]))
feeds = Feed.objects.filter(pk__in=range(f, f+1000),
active=True,
active_subscribers__gte=subscribers)\
@ -885,7 +886,7 @@ class Feed(models.Model):
if verbose:
if self.num_subscribers <= 1:
print('.', end=' ')
print(".", end=" ")
else:
print("\n %s> %s subscriber%s: %s" % (
'-' * min(self.num_subscribers, 20),
@ -1854,7 +1855,7 @@ class Feed(models.Model):
def format_story(cls, story_db, feed_id=None, text=False, include_permalinks=False,
show_changes=False):
if isinstance(story_db.story_content_z, str):
story_db.story_content_z = story_db.story_content_z.decode('base64')
story_db.story_content_z = base64.b64decode(story_db.story_content_z)
story_content = ''
latest_story_content = None

View file

@ -1,4 +1,5 @@
import datetime
import base64
from urllib.parse import urlparse
from utils import log as logging
from django.shortcuts import get_object_or_404, render
@ -81,7 +82,7 @@ def load_feed_favicon(request, feed_id):
if not_found or not feed_icon.data:
return HttpResponseRedirect(settings.MEDIA_URL + 'img/icons/circular/world.png')
icon_data = feed_icon.data.decode('base64')
icon_data = base64.b64decode(feed_icon.data)
return HttpResponse(icon_data, content_type='image/png')
@json.json_view

View file

@ -9,7 +9,7 @@ import re
import mongoengine as mongo
import random
import requests
import html.parser
import html.parser as html_parser
import tweepy
from collections import defaultdict
from bs4 import BeautifulSoup
@ -1492,7 +1492,7 @@ class MSharedStory(mongo.DynamicDocument):
@property
def decoded_story_title(self):
h = html.parser.HTMLParser()
h = html_parser.HTMLParser()
return h.unescape(self.story_title)
def canonical(self):