#coding=utf-8
"""
本应用主要功能
1.用户选择喜欢的标签加关注
2.获取用户粉丝中自己还没有关注的,->加关注,提高粉丝稳定性
3.获取用户关注列表中没有回粉的,并可以一键取消关注
2,3两个功能基本实现,有一缺点,数据量一大,很慢很慢
1功能不太好,主要是通过一个线程去搜索数据,把感兴趣的用户放入数据库,当用户选择加关注标签时,从数据库中取数据,
以前用sqlite3比较少,一用发现还是有好多值得研究的地方,主要是线程安全....,慢慢研究....
""" import os
import sys
import key
import web
import threading
import sqlite3
import time
from weibopy.auth import OAuthHandler
from weibopy.api import API
from jinja2 import Environment,FileSystemLoader """
url配置
"""
urls = (
'/', 'Login',
'/index','Index',
'/callback','CallBack',
'/logout','LogOut',
'/(.*)/', 'Redirect',
'/tag', 'Tag',
'/noattention','Noattention',
'/fensiattention','Fensiattention',
'/minusattention','Minusattention',
'/delattention','Delattention'
) app=web.application(urls,globals()) if web.config.get('_session') is None:
session = web.session.Session(app,web.session.DiskStore("sina"))
web.config._session = session
else:
session = web.config._session """
用jinja2模板渲染文件
"""
def render_template(template_name,**context):
extensions=context.pop('extensions',[])
globals=context.pop("globals",{})
jinja_env=Environment(
loader=FileSystemLoader(os.path.join(os.path.dirname(__file__),'templates')),
extensions=extensions)
jinja_env.globals.update(globals)
return jinja_env.get_template(template_name).render(context) """
定义404请求页面
"""
def notfound():
info="亲,您所请求的页面不存在,系统在3秒后自动返回..."
return web.notfound(render_template('error.html',info=info.decode('utf-8'))) db=web.database(dbn='sqlite',db='tag.s3db') """
要求tag
"""
tagdict={'水瓶座':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,'IT':32,'媒体':33,'营销':34}
systag=['水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座','吃','听歌','淘宝',
'网购','数码','摄影','睡觉','旅游','体育','动漫','游戏','股票','交友','宅女','宅男','时尚','浪漫','美女','创业','IT','媒体','营销'] conn=sqlite3.connect('tag.s3db',check_same_thread=False) #允许其它线程使用这个连接
conn.text_factory = str
cursor=conn.cursor() class Setag(threading.Thread):
"""
这个线程主要是用来搜索用户tag,如果满足tag要求就写入数据库中,
"""
def authorization(self):
"""
开发者认证
"""
auth = OAuthHandler(key.CONSUME_KEY, key.CONSUME_SECRET)
auth.setToken(key.TOKEN,key.TOKEN_SECRET)
self.api = API(auth)
self.cursor=cursor def adduser(self,uid):
"""
遍历uid用户的tag,满足条件加入数据库
"""
try:
fan=self.api.followers_ids(uid)
fanuid=fan.ids
for id in fanuid:
tags=self.api.tags(id)
tt=[]
for t in tags:
tagid=t.__getattribute__('id')
value=t.__getattribute__(tagid)
tt.append(value.encode('utf-8'))
"""
获取用户tag与要求标签的交集
"""
common=set(tt).intersection(set(systag))
if len(common)==0:
continue
else:
for t in common:
"""
获取tag对应的tagid
"""
tagindex=tagdict[t]
try:
self.cursor.execute("insert into taginfo(uid,tagid) values(%d,%d)" %(int(id),int(tagindex)))
conn.commit()
except:
continue
except:
time.sleep(120)
pass
finally:
time.sleep(60)
"""
将uid用户的第一个粉丝uid传给adduser
"""
return self.adduser(fanuid[0]) def run(self):
self.authorization()
me=self.api.verify_credentials()
"""
将我自己的uid给adduser
"""
self.adduser(me.id) """
定义404请求页面
"""
app.notfound= notfound
#首页
#首先从session中获取access_token,没有就转向新浪微博页面认证
#认证成功后将access_token保存在session中
"""
首页是登陆页面,通过新浪微博授权
"""
class Login:
def GET(self):
return render_template('login.html') """
新浪微博授权原理:
首先首页判断有无用户session信息,如果有则跳转到相应地址,
没有则引导用户跳转到授权uri,授权后自动跳转到永远自定义的回调地址,
回调地址保存用户session信息,跳转到首页,这时已有用户session信息,干坏事吧....
"""
class Index:
def GET(self):
access_token=session.get('access_token',None)
if not access_token:
"""
key.py中放置了开发者的信息
"""
auth = OAuthHandler(key.CONSUME_KEY, key.CONSUME_SECRET,web.ctx.get('homedomain')+'/callback')
#获取授权url
auth_url = auth.get_authorization_url()
session.request_token=auth.request_token
web.seeother(auth_url)
else:
auth = OAuthHandler(key.CONSUME_KEY, key.CONSUME_SECRET)
auth.access_token=access_token
api=API(auth)
user=api.verify_credentials()
return render_template('index.html',user=user) """
页面回调,新浪微博验证成功后会返回本页面
"""
class CallBack:
def GET(self):
try:
ins=web.input()
oauth_verifier=ins.get('oauth_verifier',None)
request_token=session.get('request_token',None)
auth=OAuthHandler(key.CONSUME_KEY, key.CONSUME_SECRET)
auth.request_token=request_token
access_token=auth.get_access_token(oauth_verifier)
session.access_token=access_token
web.seeother("/index")
except Exception:
info="亲,系统繁忙,请稍后再试......,系统在3秒后自动返回..."
return render_template('error.html',info=info.decode('utf-8')) """
重定向用户输入uri后的/
"""
class Redirect:
def GET(self,path):
web.seeother('/'+path) class Tag:
"""
获取用户选择加关注的tag
"""
def GET(self):
data=web.input()
try:
select=data.star
except:
try:
select=data.hobby
except:
try:
select=data.personality
except:
select=data.job
try:
auth = OAuthHandler(key.CONSUME_KEY, key.CONSUME_SECRET)
auth.access_token=session['access_token']
api=API(auth)
seuid=[]
nu=0
"""
这里写的很不好.....
"""
while True:
re=cursor.execute('select uid from taginfo where tagid=%d limit 20' %select).fetchall()
for r in re:
seuid.append(r[0])
for s in seuid:
try:
api.create_friendship(user_id=s)
nu+=1
except:
continue
if nu>=50:
break info="恭喜您已成功关注%d位用户....." %nu
return render_template('success.html',info=info.decode('utf-8'))
except:
info="亲,系统繁忙,请稍后再试......,系统在3秒后自动返回..."
return render_template('error.html',info=info.decode('utf-8')) class Noattention:
"""
获取我的粉丝中我没有加关注的,把数据传给noattention.html页面显示...
"""
def GET(self):
try:
auth=OAuthHandler(key.CONSUME_KEY,key.CONSUME_SECRET)
auth.access_token=session['access_token']
api=API(auth)
user=api.verify_credentials()
fan=[]
next_cursor=-1
while next_cursor!=0:
timeline=api.followers(user.id,'','','',next_cursor)
if isinstance(timeline,tuple):
next_cursor=timeline[1]
for line in timeline[0]:
fid=line.__getattribute__("id")
fname=line.__getattribute__("screen_name")
fan.append((fid,fname)) else:
next_cursor=0
for line in timeline:
fid=line.__getattribute__("id")
fname=line.__getattribute__("screen_name")
fan.append((fid,fname)) friend=[]
next_cursor=-1
while next_cursor!=0:
timeline=api.friends(user.id,'','','',next_cursor)
if isinstance(timeline,tuple):
next_cursor=timeline[1]
for line in timeline[0]:
frid=line.__getattribute__("id")
frname=line.__getattribute__("screen_name")
friend.append((frid,frname))
else:
next_cursor=0
for line in timeline:
frid=line.__getattribute__("id")
frname=line.__getattribute__("screen_name")
friend.append((frid,frname))
#获取我的粉丝中还不是我的关注对象
fanNotAttention=list(set(fan).difference(set(friend)))
nu=len(fanNotAttention)
if nu==0:
return render_template('noattentionok.html',nu=nu)
else:
return render_template('noattention.html',nu=nu,fanNotAttention=fanNotAttention) except:
info="亲,系统繁忙,请稍后再试......,系统在3秒后自动返回..."
return render_template('error.html',info=info.decode('utf-8')) class Fensiattention:
"""
对未加关注的粉丝加关注
"""
def GET(self):
#获取noattentionok.html传过来的数据
data=web.input()
on=[]
try:
auth=OAuthHandler(key.CONSUME_KEY,key.CONSUME_SECRET)
auth.access_token=session['access_token']
api=API(auth)
"""
获取noattention.html页面传过来的uid,通过checkbox,由于有一个全选按钮,如果点击,则去掉
"""
for x in data:
on.append(x)
try:
on.remove('checkbox2')
except:
pass
nu=len(on)
if nu==0:
pass
if nu>60:
on=on[:60]
nu=60
"""
一次最多加60次关注
"""
map(api.create_friendship,on)
info="恭喜您已成功关注%d位用户....." %nu
return render_template('success.html',info=info.decode('utf-8'))
except:
info="亲,系统繁忙,请稍后再试......,系统在3秒后自动返回..."
return render_template('error.html',info=info.decode('utf-8')) class Minusattention:
"""
获取我的关注中不是我粉丝的,即不回粉的家伙,把数据传给attentionnotfan.html显示...
"""
def GET(self):
try:
auth=OAuthHandler(key.CONSUME_KEY,key.CONSUME_SECRET)
auth.access_token=session['access_token']
api=API(auth)
user=api.verify_credentials()
fan=[]
next_cursor=-1
while next_cursor!=0:
timeline=api.followers(user.id,'','','',next_cursor)
if isinstance(timeline,tuple):
next_cursor=timeline[1]
for line in timeline[0]:
fid=line.__getattribute__("id")
fname=line.__getattribute__("screen_name")
fan.append((fid,fname)) else:
next_cursor=0
for line in timeline:
fid=line.__getattribute__("id")
fname=line.__getattribute__("screen_name")
fan.append((fid,fname)) friend=[]
next_cursor=-1
while next_cursor!=0:
timeline=api.friends(user.id,'','','',next_cursor)
if isinstance(timeline,tuple):
next_cursor=timeline[1]
for line in timeline[0]:
frid=line.__getattribute__("id")
frname=line.__getattribute__("screen_name")
friend.append((frid,frname))
else:
next_cursor=0
for line in timeline:
frid=line.__getattribute__("id")
frname=line.__getattribute__("screen_name")
friend.append((frid,frname))
attentionNotFan=list(set(friend).difference(set(fan)))
nu=len(attentionNotFan)
if nu==0:
return render_template('attentionnotfanok.html',nu=nu)
else:
return render_template('attentionnotfan.html',nu=nu,attentionNotFan=attentionNotFan) except:
info="亲,系统繁忙,请稍后再试......,系统在3秒后自动返回..."
return render_template('error.html',info=info.decode('utf-8')) class Delattention:
"""
获取attentionnotfan.html页面选择的用户,并一键取消关注
"""
def GET(self):
#获取attentionnotfan.html传过来的数据
data=web.input()
on=[]
try:
auth=OAuthHandler(key.CONSUME_KEY,key.CONSUME_SECRET)
auth.access_token=session['access_token']
api=API(auth)
for x in data:
on.append(x)
try:
#同理,由于有全选按钮.....
on.remove('checkbox2')
except:
pass
nu=len(on)
if nu==0:
pass
#取消关注
map(api.destroy_friendship,on)
info="恭喜您已成功取消关注%d位用户....." %nu
return render_template('success.html',info=info.decode('utf-8'))
except:
info="亲,系统繁忙,请稍后再试......,系统在3秒后自动返回..."
return render_template('error.html',info=info.decode('utf-8')) if __name__=='__main__':
"""
启动app,启动s线程去搜索数据
"""
s=Setag()
s.start()
app.run()

  

python实战(开发新浪微博应用)的更多相关文章

  1. 配置Python实战开发环境

    一.安装Python和easy_install 和pip 新版本的linux下面应该带有这些环境,没有自带的话可以查找google配置. 二.配置python运行的虚拟化环境: 好处:Python的库 ...

  2. 《Python高效开发实战》实战演练——内置Web服务器4

    <Python高效开发实战>实战演练——开发Django站点1 <Python高效开发实战>实战演练——建立应用2 <Python高效开发实战>实战演练——基本视图 ...

  3. Python爬虫开发与项目实战

    Python爬虫开发与项目实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1MFexF6S4No_FtC5U2GCKqQ 提取码:gtz1 复制这段内容后打开百度 ...

  4. 《Python高效开发实战》实战演练——基本视图3

    在完成Django项目和应用的建立后,即可以开始编写网站应用代码,这里通过为注册页面显示一个欢迎标题,来演示Django的路由映射功能. 1)首先在djangosite/app/views.py中建立 ...

  5. 《Python高效开发实战》实战演练——建立应用2

    为了在项目中开发符合MVC架构的实际应用程序,需要在项目中建立Django应用.每个Django项目可以包含多个Django应用.建立应用的语法为: #python manage.pystartapp ...

  6. 关于测试驱动的开发模式以及实战部分,建议看《Python Web开发测试驱动方法》这本书

    关于测试驱动的开发模式以及实战部分,建议看<Python Web开发测试驱动方法>这本书

  7. 《自动化平台测试开发-Python测试开发实战》新书出版了

    首先 第一本书,当初在百度阅读初步写了个电子版,刚一上线不久即收到了数百位读者朋友阅读收藏购买,于是顺利成章就出版了纸质书. <软件自动化测试开发>认真看过的读者应该都知道,介绍的主要是自 ...

  8. 《Python Web开发实战》|百度网盘免费下载|Python Web开发

    <Python Web开发实战>|百度网盘免费下载|Python Web开发 提取码:rnz4 内容简介 这本书涵盖了Web开发的方方面面,可以分为如下部分: 1. 使用最新的Flask ...

  9. Python爬虫开发与项目实战pdf电子书|网盘链接带提取码直接提取|

    Python爬虫开发与项目实战从基本的爬虫原理开始讲解,通过介绍Pthyon编程语言与HTML基础知识引领读者入门,之后根据当前风起云涌的云计算.大数据热潮,重点讲述了云计算的相关内容及其在爬虫中的应 ...

  10. Python实战:美女图片下载器,海量图片任你下载

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

随机推荐

  1. php调试工具——XDebug使用

    下面以windows平台和Aptana Studio为例,介绍XDdebug的使用. 1.安装XDebug 1)下载php的XDebug扩展.dll文件,官网下载地址是https://xdebug.o ...

  2. 1、Python基本概念

    1.数 python中有4种类型的数--整数.长整数.浮点数和复数 2.字符串 单引号.双引号或者三引号包含的字符序列,如: 'char' #单引号 "char" #双引号 ''' ...

  3. 如何决解项目中hibernate中多对多关系中对象转换json死循环

    先写一下原因吧!我是写的SSH项目,在项目中我遇到的问题是把分页对象(也就是pageBean对象)转化为json数据,下面为代码: public class PageBean <T>{// ...

  4. Quartz —— Spring 环境下的使用

    一.在 Spring 环境下 Quartz 的使用超级简单. 二.具体使用 1.添加对应的 spring-quartz 的配置文件. 2.新建要执行定时任务的目标类和目标方法,不需要继承 Job 接口 ...

  5. .NET Core 2.0版本预计于2017年春季发布

    英文原文: NET Core 2.0 Planned for Spring 2017 微软项目经理 Immo Landwerth 公布了即将推出的 .NET Core 2.0 版本的细节,该版本预计于 ...

  6. Struts2框架深入详解版

    一.认识Struts2 1. 什么是Web框架? 1.1  模型1 1.2  模型2 和MVC 1.3   Web框架的诞生 2. Struts1 到Struts2 2.1 其他 Web框架 2.2 ...

  7. MySQL动态字符串处理DYNAMIC_STRING

    MySQL中,常常会看到一些关于动态字符串的处理,列如:DYNAMIC_STRING. 为了记录动态字符串的实际长度,缓冲区的最大长度,以及每次字符串需要调整时,及时分配新的内存,以及调整长度.MyS ...

  8. 深化管理、提升IT的数据平台建设方案

    谈到信息化,每个企业有每个企业的业务模式,每个企业有每个企业不同的思考.落地有效的信息化建设一定紧跟着企业的发展,围绕业务和管理,来提升效率,创造价值. 对于企业如何在发展的不同阶段提升信息化建设,这 ...

  9. iOS从零开始学习直播之音频3.歌曲切换

      上周迟到了,周末去参加OSC源创会了,还是有点启发的.但这不是重点,重点是 上一篇我只是实现了一首歌曲的在线播放,这肯定是不够的.这一篇博客主要是实现了多首歌曲的顺序播放以及上一首和下一首切换. ...

  10. dyld 加载 Mach-O

    ➠更多技术干货请戳:听云博客 前言 最近看 ObjC的runtime 是怎么实现 +load 钩子函数的实现.进而引申分析了 dyld 处理 Mach-O 的这部分机制. 1.简单分析 Mach-O ...