#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. C#测试题

    阅读下面的程序,程序运行后hovertree值为( ) int x = 3, y = 4, z = 5;String s = "xyz";string hovertree = s ...

  2. ASP.net MVC 文件下载的几种方法(欢迎讨论)

    在ASP.net MVC 中有几种下载文件的方法 前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的. 第一种:最简单的超链接方法,&l ...

  3. Asp.Net MVC 从数据库生成代码(包括页面)

    项目需要,数据库已经设计完成,需要生成相关的数据访问代码和页面. 参考:http://www.tracefact.net/asp-net/aspnetmvc-model-part1.aspx http ...

  4. CssStats – 分析和优化网站 CSS 代码的利器

    CssStats 是一个在线的 CSS 代码分析工具,你只需要输入网址或者直接 CSS 地址即可进行 CSS 代码的全方位分析,是前端开发人员和网页设计师分析网站 CSS 代码的利器,可以统计出 CS ...

  5. 使用Kotlin对ViewGroup的视图进行函数使操作

    原文标题:Functional operations over Views in ViewGroup using Kotlin 原文链接:http://antonioleiva.com/functio ...

  6. Android Studio使用时源码到处报红色警告,运行时又没错

    转载地址:http://www.07net01.com/program/2016/04/1452749.html [摘要:正在AS上开辟时,碰到那个题目,翻开全部的Java源文件,右边一起标赤色,找没 ...

  7. java 执行 jar 包中的 main 方法

    java 执行 jar 包中的 main 方法 通过 OneJar 或 Maven 打包后 jar 文件,用命令: java -jar ****.jar执行后总是运行指定的主方法,如果 jar 中有多 ...

  8. [JAVA]定时任务之-Quartz使用篇

    Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...

  9. java中 String StringBuffer StringBuilder的区别

    * String类是不可变类,只要对String进行修改,都会导致新的对象生成. * StringBuffer和StringBuilder都是可变类,任何对字符串的改变都不会产生新的对象. 在实际使用 ...

  10. spool命令

    最近工作中,需对数据进行比对.在此之前,则需将数据导出.想到以前用过的spool命令,实验一番,分享如下: 需建SQL执行脚本,内容如下: set feedback off   --关掉行数显示set ...