Gwibber 中國區登陸twitter,facabook,sina的修改
最近才換成ubuntu 12.04 。默認界麵中有個廣播功能,就是gwibber。試了一下,發現twitter,facebook,sina好像都有問題。於是就研究了下代碼,python寫的很好修改
關於twitter和facebook都是個https的問題,打開源代碼把所有http換成https即可
關於sina就比較蛋疼了,因為Gwibber裏的插件是俺Oauth1.0寫的,現在sina換成Oauth2.0了,所以所有認證都要變,所幸api密鑰還可以用
沒仔細研究oauth那個庫的用法,就直接用requests模擬oauth2.0的認證了,因為自己用,所以刪了很多容錯的代碼
目前就寫了下認證的,api調用的還沒寫,最近沒時間,要趕緊訓練了,不然就等虐吧……
文件位置:
/usr/share/gwibber/plugins/sina/gtk/sina
注:按ctrl+L就可以切換到路徑編輯模式,Esc退出
from gi.repository import Gdk, Gtk, WebKit, Pango
from gi.repository.Gtk import Builder
import urllib, urllib2, json, urlparse, uuid,requests
from oauth import oauth
from gwibber.microblog.util import resources
from gwibber.microblog.util.keyring import get_from_keyring
import gettext
from gettext import gettext as _
if hasattr(gettext, 'bind_textdomain_codeset'):
gettext.bind_textdomain_codeset('gwibber','UTF-8')
gettext.textdomain('gwibber-service-sina')
import sina.utils
Gdk.threads_init()
sigmeth = oauth.OAuthSignatureMethod_HMAC_SHA1()
class AccountWidget(Gtk.VBox):
"""AccountWidget: A widget that provides a user interface for configuring sina accounts in Gwibber
"""
def __init__(self, account=None, dialog=None):
"""Creates the account pane for configuring Sina accounts"""
Gtk.VBox.__init__( self, False, 20 )
self.ui = Gtk.Builder()
self.ui.set_translation_domain ("gwibber")
self.ui.add_from_file (resources.get_ui_asset("gwibber-accounts-sina.ui"))
self.ui.connect_signals(self)
self.vbox_settings = self.ui.get_object("vbox_settings")
self.pack_start(self.vbox_settings, False, False, 0)
self.show_all()
self.account = account or {}
self.dialog = dialog
self.window = dialog.dialog
has_access_token = False
has_secret_key = False
if self.account.has_key("id"):
has_secret_key = get_from_keyring(self.account['id'], 'secret_token') is not None
try:
if self.account.has_key("access_token") and self.account.has_key("username") and has_secret_key and not self.dialog.condition:
self.ui.get_object("hbox_sina_auth").hide()
self.ui.get_object("sina_auth_done_label").set_label(_("%s has been authorized by Sina") % self.account["username"])
self.ui.get_object("hbox_sina_auth_done").show()
else:
self.ui.get_object("hbox_sina_auth_done").hide()
if self.dialog.ui:
self.dialog.ui.get_object('vbox_create').hide()
except:
self.ui.get_object("hbox_sina_auth_done").hide()
if self.dialog.ui:
self.dialog.ui.get_object("vbox_create").hide()
def on_sina_auth_clicked(self, widget, data=None):
self.winsize = self.window.get_size()
web = WebKit.WebView()
web.get_settings().set_property("enable-plugins", False)
web.get_settings().set_property("enable-developer-extras", False)
web.load_html_string(_("<p>Please wait...</p>"), "file:///")
self.consumer = oauth.OAuthConsumer(*sina.utils.get_sina_keys())
url ="https://api.weibo.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=https://gwibber.com/0/auth.html"%self.consumer.key
web.load_uri(url)
web.set_size_request(550, 400)
web.connect("title-changed", self.on_sina_auth_title_change)
self.scroll = Gtk.ScrolledWindow()
self.scroll.add(web)
self.scroll.set_size_request(550, 400)
self.pack_start(self.scroll, True, True, 0)
self.show_all()
self.dialog.infobar.hide()
self.ui.get_object("vbox1").hide()
self.ui.get_object("vbox_advanced").hide()
self.dialog.infobar.set_message_type(Gtk.MessageType.INFO)
def on_sina_auth_title_change(self, web=None, title=None, data=None):
saved = False
if title.get_title() == "Success":
if hasattr(self.dialog, "infobar_content_area"):
for child in self.dialog.infobar_content_area.get_children(): child.destroy()
self.dialog.infobar_content_area = self.dialog.infobar.get_content_area()
self.dialog.infobar_content_area.show()
self.dialog.infobar.show()
url = web.get_main_frame().get_uri()
code = url.split("=")[1]
message_label = Gtk.Label(_("Verifying"))
message_label.set_use_markup(True)
message_label.set_ellipsize(Pango.EllipsizeMode.END)
self.dialog.infobar_content_area.add(message_label)
self.dialog.infobar.show_all()
self.scroll.destroy()
# self.ui.get_object("vbox1").show()
# self.ui.get_object("vbox_advanced").show()
request=requests.session()
data=request.post("https://api.weibo.com/oauth2/access_token?client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=https://gwibber.com/0/auth.html&code=%s"%(self.consumer.key,self.consumer.secret,code)).content
data = json.loads(data)
self.account["user_id"] = data["uid"]
self.account["access_token"] = data["access_token"]
s=request.get('https://api.weibo.com/2/users/show.json?access_token=%s&uid=%s'%(data['access_token'],data['uid']))
account_data=json.loads(s.content)
self.account['username']=account_data['screen_name'].encode('utf-8')
self.dialog.on_edit_account_save()
else:
self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
message_label = Gtk.Label (_("Authorization failed. Please try again."))
message_label.set_use_markup(True)
message_label.set_ellipsize(Pango.EllipsizeMode.END)
self.dialog.infobar_content_area.add(message_label)
self.dialog.infobar.show_all()
self.ui.get_object("vbox1").show()
self.ui.get_object("vbox_advanced").show()
self.scroll.destroy()
self.window.resize(*self.winsize)
self.dialog.select_account ()
最後更新:2017-04-03 18:51:50
上一篇:
poj 3259 Wormholes 最短路
下一篇:
javascript中encodeURI和decodeURI方法
利用Socket提交文件到web server
【雲棲大會精華匯】2017杭州雲棲大會主論壇、分論壇在內的100+視頻分享
半年盤點:2017年的10大安全收購
linux中出錯處理
How to Install and Configure Zabbix on Ubuntu 16.04
android SeekBar 樣式設置
'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection leve
甲骨文宣布正式介入存儲業務 超融合架構須基於對業務的深刻了解
阿裏雲助力中國企業出海中東
MySQL鎖係列(二)之 鎖解讀