Created
October 1, 2015 14:21
-
-
Save grzhan/be0ec8a8d010f2236de0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from pyquery import PyQuery as pq | |
import requests | |
from PIL import Image | |
from StringIO import StringIO | |
import time | |
import os | |
import stat | |
import datetime | |
import operator | |
SAVE_DIR = '.' | |
DUPLI_SAVE_DIR = '.' | |
FILENAME = 'KanColleStaffAvatar' | |
THUMBNAME = 'KanColleStaffAvatarThumb' | |
def diff(h1, h2): | |
return reduce(operator.add, map(lambda a, b: (a-b) ** 2, h1, h2)) | |
def save(image, dir, name, suffix): | |
path = ''.join([dir, '/', name, '.', suffix]) | |
if os.path.exists(path): | |
os.remove(path) | |
image.save(path) | |
os.chmod(path, 0644) | |
def fetch(): | |
content = requests.get('https://twitter.com/KanColle_STAFF' | |
).content | |
dom = pq(content)('.ProfileAvatar-image') | |
domThumb = pq(content)('.stream-item-header .avatar')[0] | |
print dom.attr('src') | |
if dom: | |
image = Image.open(StringIO(requests.get(dom.attr('src') | |
).content)) | |
image_thumb = Image.open( | |
StringIO(requests.get(domThumb.get('src')).content)) | |
suffix = dom.attr('src').split('.')[-1] | |
oriname = dom.attr('src').split('/')[-1].split('.')[0] | |
today = datetime.datetime.now().strftime('%Y%m%d%H%M') | |
path = ''.join([SAVE_DIR, '/', FILENAME, '.', suffix]) | |
if os.path.exists(path): | |
h1 = image.histogram() | |
h2 = Image.open(path).histogram() | |
if not diff(h1, h2): | |
print "Current avatar is new." | |
return | |
print "Saving avatar..." | |
save(image, SAVE_DIR, FILENAME, suffix) | |
save(image, DUPLI_SAVE_DIR, FILENAME + today, suffix) | |
save(image_thumb, SAVE_DIR, THUMBNAME, suffix) | |
save(image, DUPLI_SAVE_DIR, oriname, suffix) | |
else: | |
print "Can not find kancolle_staff avatar" | |
if __name__ == '__main__': | |
fetch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment