Esempio n. 1
0
def twitterTweet(twitter_id):
    if 'user_id' in session:
        currentNet = NetworkModel.Twitter.get(NetworkModel.Twitter.twitter_id == twitter_id)
        form = FormEdit(request.form)

        if request.method == 'GET':
            return render_template('twitter/tweet.html', form=form, network=currentNet)
        else:
            currentTweet = NetworkModel.Tweet()
            currentTweet.user_id = session['user_id']
            currentTweet.twitter_id = twitter_id
            currentTweet.text = request.form['tweet']
            # URLENCODEAR EL TWEET
            params = urllib.parse.urlencode({ 'status': currentTweet.text })

            home_timeline = oauth_req('https://api.twitter.com/1.1/statuses/update.json?' + params, currentNet.access_token, currentNet.access_token_secret, currentNet.consumer_key, currentNet.consumer_secret, http_method='POST')

            data = home_timeline.decode('utf-8', 'replace')
            obj = json.loads(data)
            currentTweet.tweet_uuid = str(obj['id'])
            currentTweet.save()

            return redirect ('/twitter/tweets/' + str(twitter_id) + '?result=ok')
    else:
        return redirect ('/logout')
Esempio n. 2
0
def tweetList(twitter_id):
    if 'user_id' in session:
        form = FormList(request.form)
        currentNet = NetworkModel.Twitter.get(NetworkModel.Twitter.twitter_id == twitter_id)

        #import new tweets
        home_timeline = oauth_req('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' + currentNet.username, currentNet.access_token, currentNet.access_token_secret, currentNet.consumer_key, currentNet.consumer_secret)

        data = home_timeline.decode('utf-8', 'replace')
        obj = json.loads(data)
        #print(obj.encode('ascii', 'ignore'))

        for tweet in obj:
            selectTweet = NetworkModel.Tweet.select().where(NetworkModel.Tweet.tweet_uuid == tweet['id_str'])

            if selectTweet.count() == 0:
                newTweet = NetworkModel.Tweet()
                newTweet.user_id = session['user_id']
                newTweet.twitter_id = twitter_id
                newTweet.text = str(tweet['text'].encode('ascii', 'ignore').decode('utf-8', 'replace'))
                newTweet.tweet_uuid = tweet['id_str']

                newTweet.date = parser.parse(tweet['created_at'])
                newTweet.save()

        
        networkList = NetworkModel.Tweet.select().where(NetworkModel.Tweet.twitter_id == twitter_id).order_by(NetworkModel.Tweet.date.desc())
        
        if session['is_admin'] == 1:
            networkList = NetworkModel.Tweet.select().where(NetworkModel.Tweet.twitter_id == twitter_id).order_by(NetworkModel.Tweet.date.desc())

        show_message_css = 'hide'
        show_message_text = ''
        show_message_type = 'success'
        if 'result' in request.args:
            show_message_css = ''
            if request.args['result'] == 'ok':
                show_message_text = 'La información se guardo correctamente'
            else:
                show_message_type = 'danger'
                show_message_text = 'Hubo un problema al realizar la acción'

        return render_template('twitter/tweets.html', form=form, twitter=currentNet, networks=networkList, message_css=show_message_css, message_text=show_message_text, message_type=show_message_type)
    else:
        return redirect ('/logout')