Skip to content

Instantly share code, notes, and snippets.

@mckelvin
Last active October 4, 2015 01:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckelvin/2558321 to your computer and use it in GitHub Desktop.
Save mckelvin/2558321 to your computer and use it in GitHub Desktop.
One Day One Phrase since March 30,2012
config.py
*.pyc
*.swp
#!/usr/bin/env python2
# coding: UTF-8
import os
import time
import datetime
import logging
import pytz
import weibo
from keyword_card import KeywordCard
from config import APP_KEY, APP_SECRET, REDIR_URL,\
ACCESS_TOKEN, EXPIRES_AT
from mail import send_mail
kdb = {}
index_dir = os.path.dirname(os.path.abspath(__file__))
logging.basicConfig(filename=os.path.join(index_dir, 'odop2weibo.log'),
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s [%(levelname)s]: %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
logging.info("running ODOP helper")
def load_keywords():
with open(os.path.join(index_dir, 'odop.txt')) as fh:
lines = fh.readlines()
lines = map(lambda x: x.strip(), lines)
dates = lines[::4]
keywords = lines[2::4]
for date, keyword in zip(dates, keywords):
kdb[date] = keyword
def main():
#init
load_keywords()
client = weibo.Client(APP_KEY,APP_SECRET,
REDIR_URL,access_token=ACCESS_TOKEN,
expires_at=EXPIRES_AT)
while not client.alive:
send_mail("ODOP: weibo session expired")
logging.info("weibo session expired")
print client.authorize_url
code = raw_input('code:')
at = client.token_info.get('access_token')
exp = client.token_info.get('expires_at')
if at and exp:
with open('config.py') as fh:
fh.write("ACCESS_TOKEN = '%s'\n" % at)
fh.write("EXPIRES_AT= '%s'\n" % exp)
time.sleep(3600)
#
today = pytz.datetime.datetime.now(pytz.timezone(u'Asia/Shanghai')).date()
try:
last_year = datetime.date(today.year - 1, today.month, today.day)
except ValueError, e:
logging.info(e)
return
last_year_str = last_year.strftime("%Y-%m-%d")
keyword = kdb.get(last_year_str)
if keyword is None:
logging.info('no record on %s' % last_year_str)
return
kd = KeywordCard(keyword, last_year)
tmp_file = '/tmp/weibo_odop.png'
kd.image.save(tmp_file)
try:
res = client.post('statuses/upload', status="#odop# %s" % last_year_str, pic=open(tmp_file,'r'))
except RuntimeError, e:
logging.info('ODOP: %s' % e)
send_mail('ODOP: %s' % e)
else:
logging.info('done![%s]' % last_year_str)
finally:
os.remove(tmp_file)
if __name__ == '__main__':
main()
# coding: UTF-8
import os
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import datetime
from functools import partial
X_LARGE = 4 # to anti alias
FONT_STDSONG_PATH = os.path.expanduser('~/.fonts/AdobeSongStd-Light.otf')
class KeywordCard(object):
img_size = (250, 404)
fore_color = (0xEE,) * 3
back_color = (0x2C, 0x38, 0x46)
font_size_date = 13
font_max_size_content = 160
def __init__(self, content=u"空白", date=datetime.date.today()):
self.img = Image.new('RGB', (self.img_size[0] * X_LARGE, self.img_size[1] * X_LARGE), self.back_color)
self.font = partial(ImageFont.truetype, FONT_STDSONG_PATH)
self.draw = ImageDraw.Draw(self.img)
if type(content) != 'unicode':
content = unicode(content, 'utf-8')
self.draw_content(content)
self.draw_date(date)
self.img = self.img.resize(self.img_size, Image.ANTIALIAS)
def draw_content(self, content_text):
font_size = self.font_max_size_content
while True:
content_font = self.font(font_size * X_LARGE)
content_size = self.draw.textsize(content_text, content_font)
if (self.img.size[0] - content_size[0]) > (X_LARGE * 20):
break
font_size -= 1
content_color = self.fore_color
content_pos = (self.img.size[0]/2 - content_size[0]/2, self.img.size[1]/2 - content_size[1] / 4)
self.draw.text(content_pos, content_text, content_color, content_font)
def draw_date(self, date=datetime.date.today()):
date_text = unicode(datetime.date.strftime(date, '%Y/%m/%d').decode('utf-8'))
date_font= self.font(self.font_size_date * X_LARGE)
date_size = self.draw.textsize(date_text, date_font)
date_color = self.fore_color
date_pos = (self.img.size[0]/2 - date_size[0]/2, self.img.size[1] - date_size[1] - 10)
self.draw.text(date_pos, date_text, date_color, date_font)
@property
def image(self):
return self.img
def __del__(self):
del self.draw
def main():
test_content = u"3.14159165358979323846264338327950288"
k = KeywordCard(test_content)
k.image.show()
if __name__ == '__main__':
main()
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from email.header import Header
from email.mime.text import MIMEText
from getpass import getpass
from smtplib import SMTP_SSL
from config import GMAIL_LOGIN, GMAIL_PASSWORD, MAINTAINER
def send_mail(subject, body="RT"):
msg = MIMEText(body, _charset='utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = GMAIL_LOGIN
msg['To'] = MAINTAINER
s = SMTP_SSL('smtp.gmail.com', 465, timeout=10)
s.set_debuglevel(0)
try:
s.login(GMAIL_LOGIN, GMAIL_PASSWORD)
s.sendmail(msg['From'], msg['To'], msg.as_string())
finally:
s.quit()
2012-03-30
阿Z
2012-03-31
archlinux
2012-04-01
PANINI
2012-04-02
SQL Join
2012-04-03
远山和叶
2012-04-04
四次分手
2012-04-05
KMP
2012-04-07
古典与浪漫
2012-04-08
小斜方截半二十面体
2012-04-09
分奴与真爱
2012-04-11
老大
2012-04-13
瓣豆
2012-04-14
妈妈
2012-04-15
六度
2012-04-16
光明与磊落
2012-04-17
无法剪辑的电影
2012-04-18
gnome3 ext.
2012-04-19
时光机
2012-04-20
生成器
2012-04-21
节操
2012-04-22
跨省
2012-04-23
信息焦虑
2012-04-24
Wiki
2012-04-25
豆瓣offer
2012-04-26
無監督學習
2012-04-27
许嵩演唱会
2012-04-28
三观冲突
2012-04-29
雷雨
2012-04-30
张美丽
2012-05-01
去哪儿
2012-05-02
IP Failure
2012-05-03
欧式距离
2012-05-04
国家的需要
2012-05-05
正确的选择
2012-05-06
负能量
2012-05-07
六度
2012-05-08
往南开
2012-05-09
偏执狂住院
2012-05-10
温岭话
2012-05-11
心率过低
2012-05-12
肛门期女孩
2012-05-13
看病难
2012-05-14
腾讯备胎
2012-05-15
¥644.30
2012-05-16
清心寡欲
2012-05-17
手术延期
2012-05-18
手术完成
2012-05-19
2012-05-21
MU5457
2012-05-22
一鸡巴
2012-05-23
弹吉他
2012-05-24
miserable
2012-05-25
时间刚刚好
2012-05-26
听李志
2012-05-27
时间混乱
2012-05-28
黑客
2012-05-29
static
2012-05-30
没效率
2012-05-31
魔戒
2012-06-01
最后一课
2012-06-02
身手钥钱节操
2012-06-03
孤独患者
2012-06-04
见世面
2012-06-05
魔戒
2012-06-09
modimm3
2012-06-10
整理
2012-06-11
平衡
2012-06-13
神卉
2012-06-14
2012-06-15
湿身
2012-06-16
PEK
2012-06-17
CPyUG
2012-06-18
入职
2012-06-19
marsyas
2012-06-20
Shire
2012-06-21
二少将的梦想法
2012-06-23
天安门
2012-06-24
2012-06-25
外面的世界
2012-06-27
三千行
2012-06-29
模样
2012-06-30
Facemash
2012-07-02
伪球迷
2012-07-03
Dropbox
2012-07-04
Old Friends
2012-07-06
少年杀人案
2012-07-07
清华北大
2012-07-08
吉他
2012-07-09
TrueSkill
2012-07-10
公平混搭
2012-07-11
大烟囱
2012-07-12
城里的女人
2012-07-13
远古感性
2012-07-18
鸡婆
2012-07-19
BDD
2012-07-22
Last Grand
2012-07-23
RFG
2012-07-24
轻网络
2012-07-26
RIP
2012-07-27
良质
2012-07-29
2012-07-31
refactor
2012-08-01
徐晨
2012-08-02
正则
2012-08-03
MIR
2012-08-04
single-serving friend
2012-08-05
国图
2012-08-06
pcache
2012-08-07
5G新东方
2012-08-08
Archlinux
2012-08-09
presentation
2012-08-11
米店
2012-08-13
聲紋
2012-08-14
索引与内容
2012-08-15
landmarks
2012-08-18
講故事
2012-08-19
INTJ
2012-08-20
說文解字
2012-08-22
毛球
2012-08-24
筋疲力竭
2012-08-25
樂隊演出
2012-08-27
Hash
2012-08-28
SimHash
2012-08-29
羽球
2012-08-30
殺人
2012-08-31
False Positive
2012-09-01
天空之鏡
2012-09-02
依然愛你
2012-09-03
ALG
2012-09-04
評估
2012-09-05
口腔潰瘍
2012-09-08
孤獨是
2012-09-09
証明
2012-09-10
2012-09-11
神秘會議
2012-09-13
進程池
2012-09-14
DPark
2012-09-15
思想廣場
2012-09-16
眼鏡兒
2012-09-18
白鹿原
2012-09-19
豆瓣Offer
2012-09-20
結婚
2012-09-22
输入错误
2012-09-23
两广路
2012-09-24
Mesos
2012-09-25
export
2012-09-26
Queue
2012-09-27
集群
2012-09-28
病态
2012-09-29
35D 京-哈
2012-09-30
哈尔滨
2012-10-01
中央大街
2012-10-02
行业
2012-10-03
冲突
2012-10-04
吃撑
2012-10-05
2012-10-06
方苑姐姐
2012-10-07
清洗
2012-10-08
节后疲惫
2012-10-09
阿里巴巴
2012-10-10
clickjacking
2012-10-11
哼唱
2012-10-12
青旅
2012-10-13
烟花
2012-10-14
北海公园
2012-10-15
吉他
2012-10-16
黏湿
2012-10-18
EMD
2012-10-19
翻译
2012-10-20
馨香
2012-10-22
MAD
2012-10-23
停电
2012-10-24
2012-10-25
66A
2012-10-26
上瘾
2012-10-27
推荐系统
2012-10-28
规律
2012-10-29
新号码
2012-10-30
感冒
2012-10-31
毕设
2012-11-01
三小时
2012-11-02
yaafe
2012-11-03
冻死骨
2012-11-04
六线谱
2012-11-05
没重点
2012-11-06
狗来富
2012-11-07
AQVA
2012-11-08
羽球
2012-11-09
出发
2012-11-10
米修
2012-11-11
du
2012-11-12
Spectra
2012-11-13
Chroma
2012-11-14
11361.57
2012-11-15
杜酱
2012-11-17
师姐
2012-11-18
阅读
2012-11-19
吵架
2012-11-20
回滚关系
2012-11-21
窗函数
2012-11-22
音纹移植
2012-11-23
蕃茄闹钟
2012-11-24
Latex
2012-11-25
5.0.1
2012-11-26
learn filter
2012-11-27
learn filter
2012-11-28
神经官能症
2012-11-29
Adaboost
2012-11-30
冷冻傻逼
2012-12-01
整理
2012-12-02
XeTeX
2012-12-03
打包
2012-12-04
切糕
2012-12-05
JS - Landing
2012-12-06
新训练集
2012-12-07
muggy
2012-12-08
xlxtra
2012-12-09
XeLaTeX
2012-12-10
太囧
2012-12-11
重构
2012-12-12
小雪
2012-12-13
用以致学
2012-12-14
翔职
2012-12-15
MU5458
2012-12-16
top10
2012-12-17
呵呵竞争力
2012-12-18
长谈
2012-12-19
你妈妈逼的
2012-12-20
我爱杜娘 杜娘爱我
2012-12-21
幸福'圣诞Party
2012-12-23
下厨房
2012-12-24
婚礼
2012-12-25
赖人的病人
2012-12-28
科普
2012-12-29
万象初雪
2012-12-30
六个蛋挞
2012-12-31
面向对象
2013-01-01
杯具控
2013-01-02
暖气与生产力
2013-01-03
兰州蛋挞
2013-01-04
openwRT
2013-01-05
null
2013-01-06
初次难过
2013-01-07
思想
2013-01-08
讲MIR
2013-01-09
丢失
2013-01-10
2013-01-11
D5587
2013-01-12
丫丫声
2013-01-13
大定
2013-01-14
道德绑架
2013-01-15
兴旺阁嵌糕
2013-01-16
四十岁与理性
2013-01-17
七星蕉王
2013-01-18
劳资与制造
2013-01-19
拆分下线
2013-01-20
大寒
2013-01-21
成功与卓越
2013-01-22
怨比恨深
2013-01-23
挖煤
2013-01-24
大日子
2013-01-25
做人总要信
2013-01-26
完工
2013-01-27
幸福感
2013-01-28
执场头
2013-01-29
暖房夜
2013-01-30
姐姐大婚
2013-01-31
落厨夜
2013-02-01
大吐
2013-02-02
彻夜未眠
2013-02-03
十年长跑
2013-02-04
路由砌成砖
2013-02-05
七年之痒
2013-02-06
好久不贱
2013-02-07
归期已定
2013-02-08
刮痧
2013-02-09
翻译
2013-02-10
the Smiths
2013-02-11
人肉
2013-02-12
被汰
2013-02-13
吃喝养生
2013-02-14
方山
2013-02-15
时间失调
2013-02-16
inspiration
2013-02-17
emoi密闭圈
2013-02-18
D3112
2013-02-19
Python1024
2013-02-20
Sweet Disposition
2013-02-21
Autumn?
2013-02-22
肌肉哈欠
2013-02-23
两个世界
2013-02-24
逆向学习
2013-02-25
重五斤
2013-02-26
节操兄
2013-02-27
暂住证
2013-02-28
河坊街
2013-03-01
Somersault
2013-03-02
扁桃体
2013-03-03
Guitar
2013-03-04
hacker game
2013-03-05
考点
2013-03-06
异次元
2013-03-07
言多必失
2013-03-08
拉杆箱与书包
2013-03-09
五月天
2013-03-10
时时刻刻
2013-03-11
爱情的模样
2013-03-12
Skype Mood
2013-03-13
变冷
2013-03-14
1234567
2013-03-15
科目一
2013-03-16
一生所爱
2013-03-17
统计学习
2013-03-18
传动装置
2013-03-19
荷尔蒙
2013-03-20
Last Order
2013-03-21
朴素贝叶斯
2013-03-22
睡眠与幸福感
2013-03-23
AV BARCODE
2013-03-24
强迫症
2013-03-25
浮光掠影
2013-03-26
Haskell
2013-03-27
smartisan
2013-03-28
旧数据
2013-03-29
xmonad
2013-03-30
特征工程
2013-03-31
Project Dinner
2013-04-01
勿忘初心
2013-04-02
虚弱臆想
2013-04-03
南星桥
2013-04-04
The blower´s daughter
2013-04-05
做媒
2013-04-06
天下无双
2013-04-07
啤酒与灵感
2013-04-08
助幸福
2013-04-09
13 ml/h
#!/bin/sh
# 00 07 * * * /home/mckelvin/scripts/odop/run.sh
python /home/mckelvin/scripts/odop/helper.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment