コード例 #1
0
ファイル: main.py プロジェクト: iizukak/PyQt4_tumblr
 def __init__(self, parent=None):
     super(mainWindow, self).__init__(parent)
     self.ui = Ui_MainWindow()
     self.ui.setupUi(self)
 
     QtCore.QObject.connect(self.ui.actionNext, QtCore.SIGNAL('triggered()'), self.next)
     QtCore.QObject.connect(self.ui.actionBack, QtCore.SIGNAL('triggered()'), self.back)
     QtCore.QObject.connect(self.ui.actionReblog, QtCore.SIGNAL('triggered()'), self.reblog)
     QtCore.QObject.connect(self.ui.actionBrowser, QtCore.SIGNAL('triggered()'), self.viewBrowser)
     QtCore.QObject.connect(self.ui.actionReload, QtCore.SIGNAL('triggered()'), self.reload)
     QtCore.QObject.connect(self.ui.actionUser, QtCore.SIGNAL('triggered()'), self.viewUser)
     QtCore.QObject.connect(self.ui.actionAll, QtCore.SIGNAL('triggered()'), self.setAll)
     QtCore.QObject.connect(self.ui.actionPhoto, QtCore.SIGNAL('triggered()'), self.setPhoto)
     QtCore.QObject.connect(self.ui.actionQuote, QtCore.SIGNAL('triggered()'), self.setQuote)
     QtCore.QObject.connect(self.ui.actionText, QtCore.SIGNAL('triggered()'), self.setText)        
     QtCore.QObject.connect(self.ui.actionAuth, QtCore.SIGNAL('triggered()'), self.showAuthWindow)        
コード例 #2
0
ファイル: main.py プロジェクト: iizukak/PyQt4_tumblr
class mainWindow(QtGui.QMainWindow):
    posts = []
    loadcount = 0
    postnumber = 0
    loadtype = ''
    dsbdapiurl = 'http://api.tumblr.com/v2/user/dashboard?offset=' + str(loadcount * 20)

    def __init__(self, parent=None):
        super(mainWindow, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
    
        QtCore.QObject.connect(self.ui.actionNext, QtCore.SIGNAL('triggered()'), self.next)
        QtCore.QObject.connect(self.ui.actionBack, QtCore.SIGNAL('triggered()'), self.back)
        QtCore.QObject.connect(self.ui.actionReblog, QtCore.SIGNAL('triggered()'), self.reblog)
        QtCore.QObject.connect(self.ui.actionBrowser, QtCore.SIGNAL('triggered()'), self.viewBrowser)
        QtCore.QObject.connect(self.ui.actionReload, QtCore.SIGNAL('triggered()'), self.reload)
        QtCore.QObject.connect(self.ui.actionUser, QtCore.SIGNAL('triggered()'), self.viewUser)
        QtCore.QObject.connect(self.ui.actionAll, QtCore.SIGNAL('triggered()'), self.setAll)
        QtCore.QObject.connect(self.ui.actionPhoto, QtCore.SIGNAL('triggered()'), self.setPhoto)
        QtCore.QObject.connect(self.ui.actionQuote, QtCore.SIGNAL('triggered()'), self.setQuote)
        QtCore.QObject.connect(self.ui.actionText, QtCore.SIGNAL('triggered()'), self.setText)        
        QtCore.QObject.connect(self.ui.actionAuth, QtCore.SIGNAL('triggered()'), self.showAuthWindow)        
        
        #self.extposts()        
        #self.next()
    def setClient(self):
        token = oauth.Token(str(settings.value('oauth_token').toString()),
                                 str(settings.value('oauth_token_secret').toString()))
        consumer = oauth.Consumer(xauth.consumer_key, xauth.consumer_secret)
        print 'token is', settings.value('oauth_token').toString()
        self.client = oauth.Client(consumer, token)

    def showAuthWindow(self):
        self.authWindow = authWindow()
        self.authWindow.show()

    def next(self):
        print self.postnumber
        if (len(self.posts) - self.postnumber) < 10 :
            self.extposts()
        self.displaypost(self.postnumber)
        self.postnumber += 1
        
    def reload(self):
        self.posts = []
        self.loadcount = 0
        self.postnumber = 0  
        self.extposts()
        self.next()  
    
    def back(self):
        if self.postnumber <= 1 :
            pass
        else :
            self.displaypost(self.postnumber -2)
            self.postnumber -= 1
        
    def extposts(self):
        if self.loadtype != '':
            self.dsbdapiurl = 'http://api.tumblr.com/v2/user/dashboard?offset=' + str(self.loadcount * 20) \
                + '&type=' + self.loadtype
            print 'Loading', self.loadtype
        else:
            self.dsbdapiurl = 'http://api.tumblr.com/v2/user/dashboard?offset=' + str(self.loadcount * 20)

        print self.loadcount, 'times loaded'
        resp, content = self.client.request(self.dsbdapiurl, method='GET')
        obj = json.loads(content)
        print 'loaded', len(obj['response']['posts']), 'posts'
                
        self.posts.extend(obj['response']['posts'])
        thread.start_new_thread(self.load, ())
        self.loadcount += 1
        
    def load(self):
        for post in self.posts[-20:]:
            avatarurl = 'http://api.tumblr.com/v2/blog/'+  post['blog_name'] + '.tumblr.com/avatar'
            print avatarurl
            avatarresp = urllib.urlopen(avatarurl)
            avatardata = QtCore.QByteArray(avatarresp.read())
            post['avatardata'] = avatardata
            
            blogurl = 'http://api.tumblr.com/v2/blog/'+  post['blog_name'] + '.tumblr.com/info?api_key=' + xauth.consumer_key
            content = urllib.urlopen(blogurl).read()
            bloginfo = json.loads(content)
            blogdescription = bloginfo['response']['blog']['description']
            post['description'] = blogdescription
            post['totalpost'] = str(bloginfo['response']['blog']['posts'])
            
            posttype =  post['type']
            if posttype == 'photo':
                posturl = post["photos"][0]['alt_sizes'][0]['url']
                resp = urllib.urlopen(posturl)
                data = QtCore.QByteArray(resp.read())
                mime = resp.headers.type
                print 'post data type =', mime
                post['data'] = data
                post['datatype'] = mime
                
    def displaypost(self, num):
        self.statusBar().showMessage(str(self.posts[num]['note_count'])  +' notes. posted at ' + str(self.posts[num]['date']))

        
        if self.posts[num].get('avatardata') == None:
            url = 'http://api.tumblr.com/v2/blog/'+  self.posts[num]['blog_name'] + '.tumblr.com/avatar'
            self.ui.avatar.setUrl(QtCore.QUrl(url))
        else:
            print 'avatar type is', type(self.posts[num].get('avatardata'))
            self.ui.avatar.setContent(self.posts[num].get('avatardata'), 'image/png')
       
        if self.posts[num].get('description') == None:
            print 'description is none'
            blogurl = 'http://api.tumblr.com/v2/blog/'+  self.posts[num]['blog_name'] + '.tumblr.com/info?api_key=' + xauth.consumer_key
            content = urllib.urlopen(blogurl).read()
            bloginfo = json.loads(content)
            print 'description type is ' , type(bloginfo['response']['blog']['description'])
            blogdescription = bloginfo['response']['blog']['description']
            totalpost = str(bloginfo['response']['blog']['posts'])
        else:
            blogdescription = self.posts[num].get('description')
            totalpost = self.posts[num].get('totalpost')
        
        self.ui.postType.setText(self.posts[num]['type'] + ':')
        blog_description = 'Blog: ' + self.posts[num]['blog_name']
        self.ui.about.setHtml('<strong>NAME: </strong>' \
                            + self.posts[num]['blog_name'] + '<br><strong>TOTAL POST: </strong>' + totalpost\
                            + '<br>'\
                            + '<strong>BIO: </strong>' \
                            + blogdescription
                            )
        self.scene=QtGui.QGraphicsScene()
        
        if self.posts[num]['type'] == 'photo' :
            self.ui.caption.setHtml(unicode(self.posts[num].get('caption', 'No Caption Available')))
            if self.posts[num].get('caption') == '' :
                self.ui.caption.setHtml('No Caption')
            if self.posts[num].get('data') == None:
                self.ui.webView.setUrl(QtCore.QUrl(self.posts[num]["photos"][0]['alt_sizes'][0]['url']))
            else:
                self.ui.webView.setContent(self.posts[num].get('data'),self.posts[num].get('datatype'))
                print 'mime = ', self.posts[num].get('datatype')
        elif self.posts[num]['type'] == 'quote' :
            self.ui.caption.setHtml(unicode(self.posts[num].get('caption', 'No Caption')))
            print '[Quote]'
            self.ui.webView.setHtml(unicode(self.posts[num]['text']))
        elif self.posts[num]['type'] == 'text' :
            print '[Text]'
            self.ui.webView.setHtml(unicode(self.posts[num]['body']))
            self.ui.caption.setHtml('No Caption')
        else :
            self.postnumber += 1
            self.next()
    
    def reblog(self):
        self.statusBar().showMessage('reblogging...')
        rblgapiurl = 'http://api.tumblr.com/v2/blog/'  \
        +  str(settings.value('username').toString()) \
        + '.tumblr.com/post/reblog'  
        print(rblgapiurl)
    
        params = {}
        params['id'] = self.posts[self.postnumber - 1]['id']
        params['reblog_key'] = self.posts[self.postnumber - 1]['reblog_key']
        print params

        resp, content = self.client.request(rblgapiurl, method='POST',
                                          body=urllib.urlencode(params))
        self.statusBar().showMessage('reblog done!')
        
        print resp
        
    def viewBrowser(self):
        webbrowser.open(self.posts[self.postnumber - 1]['post_url'])
        
    def viewUser(self):
        webbrowser.open('http://' +  self.posts[self.postnumber - 1]['blog_name'] + '.tumblr.com/')

    def setPhoto(self):
        self.loadtype = 'photo'
        self.reload()
    def setText(self):
        self.loadtype = 'text'
        self.reload()
    def setQuote(self):
        self.loadtype = 'quote'
        self.reload()
    def setAll(self):
        self.loadtype = ''
        self.reload()