Skip to content

Commit

Permalink
official retweet
Browse files Browse the repository at this point in the history
  • Loading branch information
bigeagle committed Feb 1, 2012
1 parent 01a6261 commit 85e504c
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.pyc
*.swp
*~
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#手动发Tweet的KEY
TWEET_KEY = 'V8Q6DvexTyCE3se47rtBX8em'
## 关键词
RT_REGEX="""xdtuxbot|linux|unix|android|meego|ubuntu|arch|gentoo|debian|firefox|chrome|chromium|python|geek|apache|nginx|sql|php|django|rails|ruby|GPL|vim|emacs|gnome|gnu|perl|freebsd|netbsd|openbsd|hack|html5| kde """
RT_REGEX="""xdtuxbot|linux|unix|android|meego|ubuntu|arch|gentoo|debian|firefox|chrome|chromium|python| geek |apache|nginx|sql|php|django|rails|ruby|GPL|vim|emacs|gnome|gnu|perl|freebsd|netbsd|openbsd|hack|html5| kde """

MGC="gfw|翻墙|中共|土共|共产党|当局|政府|#notrt"
MGC="gfw|翻墙|中共|土共|共产党|当局|政府|#notrt|#nort"
## 访问路径
URL_RT = '/RT' # 推群
URL_TIMELINE = '/timeline' # 时间线
Expand Down
59 changes: 30 additions & 29 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import logging
import re
from sets import Set
from datetime import datetime, timedelta
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
Expand Down Expand Up @@ -40,6 +39,11 @@ def url_expand( url ):
return l

def parse_content( content ):
content = content.encode("utf-8")
# 展开网址
content = re.sub(r'(https?://[!-z]*?)(\s|$)',
r'<a href="\1">\1</a>\2',
content)

# 处理@
content = re.sub(r'@(.*?)(\s|:|$)',
Expand All @@ -50,19 +54,14 @@ def parse_content( content ):
r'<a href="https://twitter.com/search/%23\1">#\1</a>\2',
content)

# 展开网址

content = re.sub(r'(http://.*?)(\s|$)',
r'<a href="\1">\1</a>\2',
content)

r_url = re.compile('<a.*>(http://t.co/.*?)</a>')
m = r_url.findall(content)
for s_url in m:
print s_url
logging.info(s_url)
if s_url != '':
l_url = url_expand( s_url )
print l_url
logging.info(l_url)
content += '<div class="long-url"><a href="%s">%s</a></div>' \
% ( l_url, l_url)

Expand Down Expand Up @@ -119,24 +118,26 @@ def get(self):
api = tweepy.API(auth)

user = self.request.get('user')
if user == 'xdlinux':
slug = 'rt-2'
elif user == 'xdlinuxbot':
slug = 'rt'
else:
user = 'xdlinux'
slug = 'rt-2'
page = self.request.get('page')
if page == '':
page = 1
else:
page = int(page)

RT=api.list_timeline(owner=user,slug=slug,per_page=count,page=page)


if user == 'xdlinux':
user = 'xdtuxbot'
RT=api.user_timeline(screen_name=user,count=count,page=page,include_rts=1)
else:
RT=api.list_timeline(owner="xdlinuxbot",slug='rt',per_page=count,page=page)

logging.info('Check list')
logging.info(RT[0].created_at)

for i,item in enumerate(RT):
if item.retweeted:
RT[i] = item.retweeted_status

#时区调整
for i in range(len(RT)):
Ttime=str(RT[i].created_at)
Expand All @@ -147,12 +148,13 @@ def get(self):
T[t]=int(T[t])
Ttime=datetime(T[0],T[1],T[2],T[3],T[4],T[5])+timedelta(hours=+8)
RT[i].created_at=Ttime.strftime('%Y-%m-%d %H:%M:%S')

#超链接和@
for i in range(len(RT)):
content = RT[i].text
#logging.info(content)
content = parse_content( content )
RT[i].text = content
#logging.info(content)

next="RT?user=%s&page=%d" % (user,(page+1))
if page > 1:
Expand All @@ -162,6 +164,7 @@ def get(self):

path = os.path.join(os.path.dirname(__file__), 'template/index.html')
self.response.out.write(template.render(path, { 'RT': RT ,'NEXT': next, 'PREV':prev, 'LIST':user}))

# Cron Job
class CronJobCheck(webapp.RequestHandler):
def get(self):
Expand All @@ -176,7 +179,7 @@ def get(self):
if (not Access_CronJob):
logging.debug('CronJobCheck() access denied!')
logging.critical('如果这个请求不是由你手动触发的话,这意味者你的CronJobKey已经泄漏!请立即修改CronJobKey以防被他人利用')
# return
return

mydate = datetime.utcnow() + timedelta(hours=+8)
ts_hour = mydate.time().hour
Expand All @@ -185,7 +188,6 @@ def get(self):
dbug = self.request.get('debug')
logging.debug(dbug)


# 7:00早安世界
if (((ts_hour == 7) and ( 0 <= ts_min <= 2)) or (dbug=='morning')): # 7:00
error = False
Expand All @@ -201,14 +203,14 @@ def get(self):
msg = '%s 今天西安的天气是:%s %s' % \
(config.MSG_GET_UP[msg_idx], wther[0], config.BOT_HASHTAG)

resp=OAuth_UpdateTweet(msg) # 早安世界
OAuth_UpdateTweet(msg) # 早安世界
logging.info("%s:%d" % (msg,wther[1]))

# 23:30 晚安世界
elif ((ts_hour == 23) and (30 <= ts_min <=32)): # 23:30
msg_idx=random.randint(0,len(config.MSG_SLEEP)-1)
msg = '%s%s' % (config.MSG_SLEEP[msg_idx], config.BOT_HASHTAG)
resp=OAuth_UpdateTweet(msg) # 晚安世界
OAuth_UpdateTweet(msg) # 晚安世界
logging.info(msg)

# 每小时一条命令
Expand All @@ -219,7 +221,7 @@ def get(self):
msg = '%s %s' % ( "叮咚!小bot教CLI时间到了!", msg[:-1])
msg +="#commandlinefu"
logging.info(msg)
resp = OAuth_UpdateTweet(msg)
OAuth_UpdateTweet(msg)

# 扫TL,转推
auth = tweepy.OAuthHandler(config.CONSUMER_KEY, config.CONSUMER_SECRET)
Expand Down Expand Up @@ -249,22 +251,21 @@ def get(self):
user = tweet.user.screen_name
if user == 'xdtuxbot':
continue

text = tweet.text
m = regx.search(text)
if m == None:
continue
n = mgc.search(text)
if n != None:
continue



msg = 'RT @%s:%s' % (user,text)
#logging.info(msg)

try:
resp=OAuth_UpdateTweet(msg) # 发送到Twitter
logging.info('Send Tweet: %s, rtn %s' % (msg, resp))
#resp=OAuth_UpdateTweet(msg) # 发送到Twitter
api.retweet(tweet.id)
logging.info('Send Tweet: %s' % (msg))
except tweepy.TweepError, e:
msg = 'Tweepy Error:%s' % e
logging.error(msg)
Expand Down
2 changes: 1 addition & 1 deletion template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div class="logo"><img src ="/logo.png"></div>
<div class="nav-main">
<ul>
<li class={% ifequal LIST 'xdlinux' %}"now"{%else%}"notnow"{%endifequal%}> <a href="/RT?user=xdlinux&page=1">生活圈</a> </li>
<li class={% ifequal LIST 'xdtuxbot' %}"now"{%else%}"notnow"{%endifequal%}> <a href="/RT?user=xdlinux&page=1">生活圈</a> </li>
<li class={% ifequal LIST 'xdlinuxbot' %}"now"{%else%}"notnow"{%endifequal%}><a href="/RT?user=xdlinuxbot&page=1">实时</a> </li>
</ul>
</div>
Expand Down
6 changes: 3 additions & 3 deletions template/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ a, a:visited{
color: #999;
}
.long-url {
margin:4px 4px 4px 4px;
padding:2px 2px 2px 2px;
margin:.8em .8em;
padding:.2em .2em;
border:1px solid #eee;
background:#eef;
background:#f8f8f8;
}
Binary file removed tweepy/__init__.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion tweepy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, auth_handler=None,
path = '/statuses/user_timeline.json',
payload_type = 'status', payload_list = True,
allowed_param = ['id', 'user_id', 'screen_name', 'since_id',
'max_id', 'count', 'page']
'max_id', 'count', 'page','include_rts']
)

""" statuses/mentions """
Expand Down
Binary file removed tweepy/api.pyc
Binary file not shown.
Binary file removed tweepy/auth.pyc
Binary file not shown.
Binary file removed tweepy/binder.pyc
Binary file not shown.
Binary file removed tweepy/cache.pyc
Binary file not shown.
Binary file removed tweepy/cursor.pyc
Binary file not shown.
Binary file removed tweepy/error.pyc
Binary file not shown.
Binary file removed tweepy/models.pyc
Binary file not shown.
Binary file removed tweepy/oauth.pyc
Binary file not shown.
Binary file removed tweepy/parsers.pyc
Binary file not shown.
Binary file removed tweepy/streaming.pyc
Binary file not shown.
Binary file removed tweepy/utils.pyc
Binary file not shown.

0 comments on commit 85e504c

Please sign in to comment.