<commit_before># -*- coding: utf-8 -*-
#!/usr/bin/env python
#
# This is an sample AppEngine application that shows how to 1) log in a user
# using the Twitter OAuth API and 2) extract their timeline.
#
# INSTRUCTIONS: 
#
# 1. Set up a new AppEngine application using this file, let's say on port 
# 8080. Rename this file to main.py, or alternatively modify your app.yaml 
# file.)
# 2. Fill in the application ("consumer") key and secret lines below.
# 3. Visit http://localhost:8080 and click the "login" link to be redirected
# to Twitter.com.
# 4. Once verified, you'll be redirected back to your app on localhost and
# you'll see some of your Twitter user info printed in the browser.
# 5. Copy and paste the token and secret info into this file, replacing the 
# default values for user_token and user_secret. You'll need the user's token 
# & secret info to interact with the Twitter API on their behalf from now on.
# 6. Finally, visit http://localhost:8080/timeline to see your twitter 
# timeline.
#

__author__ = "<NAME>"

import oauth
import keys
import urllib
import logging

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
from google.appengine.ext import db

try:
    import simplejson
except ImportError:
    try:
        import json as simplejson
    except ImportError:
        try:
            from django.utils import simplejson
        except:
            raise Exception("SimpleJson nao encontrado")

class LastTweet(db.Model):
    tweetId = db.IntegerProperty()
    

class MainHandler(webapp.RequestHandler):
        
    def get(self):
        callback_url = "%s/verify" % self.request.host_url
        client = oauth.TwitterClient(keys.application_key, keys.application_secret, 
            callback_url)
        
        lastTweet = db.GqlQuery("SELECT * FROM LastTweet").fetch(1)[0]
        params = {}
        if lastTweet:
            params["since_id"] = lastTweet.tweetId
            
        mentions_url = "http://api.twitter.com/statuses/mentions.json"
        result = client.make_request(url=mentions_url, token=keys.ondevende['user_token'], 
        secret=keys.ondevende['user_secret'], additional_params=params, method=urlfetch.GET)
        
        statuses = simplejson.loads(result.content)
        if statuses:
            if not lastTweet:
                lastTweet = LastTweet() 
 
        for status in statuses:
            id = status["id"]
            user = "@"+status["user"]["screen_name"]+" "
            msg = self.getBestPrice(status["text"])
            self.postTweet(id, user + msg)
            
        lastTweet.tweetId = statuses[0]["id"]
        lastTweet.put()
       
    def getBestPrice(self, status):
        logging.info("Processando: %s", status)
        tweet = "Desculpe, não consegui encontrar o produto"
        if status.find("@ondevende") == 0:
            status = status.replace("@ondevende", "").strip()
        else:
            return ""
        
        keyword = urllib.quote(status)
        buscapeUrl = "http://sandbox.buscape.com/service/findOfferList/%s/?keyword=%s&sort=price&format=json" % (keys.buscape_id, keyword)

        try: 
            result = urlfetch.fetch(url=buscapeUrl)
            buscapeData = simplejson.loads(result.content)
            
            price = buscapeData["offer"][0]["offer"]["price"]["value"].replace(".",",")
            store = buscapeData["offer"][0]["offer"]["seller"]["sellername"]
            
            tweet = "Compre em %s por R$%s" % (store, price)<commit_msg>fix: last tweet id update<commit_after># -*- coding: utf-8 -*-

__author__ = "<NAME>"

import oauth
import keys
import urllib
import logging

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
from google.appengine.ext import db

try:
    import simplejson
except ImportError:
    try:
        import json as simplejson
    except ImportError:
        try:
            from django.utils import simplejson
        except:
            raise Exception("SimpleJson nao encontrado")

class LastTweet(db.Model):
    tweetId = db.IntegerProperty()
    

class MainHandler(webapp.RequestHandler):
        
    def get(self):
        callback_url = "%s/verify" % self.request.host_url
        client = oauth.TwitterClient(keys.application_key, keys.application_secret, 
            callback_url)
        
        lastTweet = db.GqlQuery("SELECT * FROM LastTweet").fetch(1)[0]
        params = {}
        params["since_id"] = lastTweet.tweetId
            
        mentions_url = "http://api.twitter.com/statuses/mentions.json"
        result = client.make_request(url=mentions_url, token=keys.ondevende['user_token'], 
        secret=keys.ondevende['user_secret'], additional_params=params, method=urlfetch.GET)
        
        statuses = simplejson.loads(result.content)
 
        for status in statuses:
            id = status["id"]
            user = "@"+status["user"]["screen_name"]+" "
            msg = self.getBestPrice(status["text"])
            self.postTweet(id, user + msg)
            
        if statuses:
            lastTweet.tweetId = statuses[0]["id"]
            lastTweet.put()
       
    def getBestPrice(self, status):
        logging.info("Processando: %s", status)
        tweet = "Desculpe, não consegui encontrar o produto"
        if status.find("@ondevende") == 0:
            status = status.replace("@ondevende", "").strip()
        else:
            return ""
        
        keyword = urllib.quote(status)
        buscapeUrl = "http://sandbox.buscape.com/service/findOfferList/%s/?keyword=%s&sort=price&format=json" % (keys.buscape_id, keyword)

        try: 
            result = urlfetch.fetch(url=buscapeUrl)
            buscapeData = simplejson.loads(result.content)
            
            price = buscapeData["offer"][0]["offer"]["price"]["value"].replace(".",",")
            store = buscapeData["offer"][0]["offer"]["seller"]["sellername"]
            
            tweet = "Compre em %s por R$%s" % (store, price)
{'fraction_non_alphanumeric': 0.11604374081932431, 'fraction_numerical': 0.00554920842173984, 'mean_word_length': 2.5462962962962963, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6366757', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1827, 'n_words': 451}