from google.appengine.api import memcache
import webapp2
from twitterUser import TwitterUser
import oauth
import config
import logging
import urlparse
try:
    import json
except:
    from django.utils import simplejson as json
    
import xmltodict

class FeedHandler(webapp2.RequestHandler):

    def get(self, username, accessCode, apiEndpoint=None):
        try:
            self.response.headers['Content-Type'] = 'application/rss+xml'
        
            if not apiEndpoint:
                apiEndpoint = "statuses/home_timeline.json"
            
            twitRes = self.makeTwitterRequest(
                user = self.getUser(username, accessCode), 
                api = apiEndpoint, 
                params = self.getParametersDict())
            
            outDict = self.getRSSDictFromTwitterResponse(
                    twitRes, 
                    username + apiEndpoint + urlparse.urlparse(self.request.url).query)
            
            self.response.out.write(xmltodict.unparse(outDict))
            
        except Exception as e:
            logging.exception(e)
            self.abort(500, detail=e)

    def handle_exception(self, exception, debug):
        # If the exception is a HTTPException, use its error code.
        # Otherwise use a generic 500 error code.
        if isinstance(exception, webapp2.HTTPException):
            self.response.out.write(exception.detail)
            self.response.set_status(exception.code)
        else:
            self.response.out.write("An error has occurred")
            self.response.set_status(500)
            
    def getUser(self, username, accessCode):
        # First try memcache
        result = memcache.get(username)

        if result is not None:
            # We got a cached result
            return result
        
        # Nothing in cache or expired, fetch from datastore then cache that
        query = TwitterUser.all()
        query.filter("username =", username)
        query.filter("accessCode =", accessCode)
        
        result = query.get()
        
        if not result:
            # Query did not match any access tokens we have, oops
            self.abort(401, detail="No Authorized Twitter User found")

        # Add it to the cache now
        memcache.set(username, result, 60 * 30)
        
        return result
    
    def getParametersDict(self):
        urlInfo = urlparse.urlparse(self.request.url)
        
        params = urlparse.parse_qs(urlInfo.query)
        
        # get rid of the pesky lists that parse_qs produces
        params.update((key, val[0]) for key,val in params.items())
        
        if 'count' not in params:
            params['count'] = 10 
        
        return params
    
    def makeTwitterRequest(self, user, api, params):

        # Check if user has been disabled
        if user.accessSecret[:8] == 'DISABLED':
            self.abort(403, detail="Your account has been disabled due to quota restrictions. Please contact @Tweet_2_RSS to get reinstated")
        
        url = "https://api.twitter.com/1.1/{0}".format(api)
        
        client = oauth.TwitterClient(config.CONSUMER_KEY, config.CONSUMER_SECRET, config.CALLBACK)
            
        resp = client.make_request(url, user.accessToken, user.accessSecret, additional_params=params, protected=True)
        
        if resp.status_code == 200:
            return json.loads(resp.content)
        
    def getRSSDictFromTwitterResponse(self, twit, limitKey=None):
        
        limitId = memcache.get(limitKey) if limitKey else None

        outDict = {
            'rss' : {
                '@version' : '2.0',
                'channel' : {
                    'title' : 'Tweet 2 RSS Feed',
                    'description' : 'Converting Twitter API Queries to RSS',
                    'link' : 'http://tweet-2-rss.appspot.com',
                    'item' : []
                }
            }
        }
        
        outItems = outDict['rss']['channel']['item']
        if not twit:
            logging.warning("We obviously didn't get a valid dictionary")
            # TODO : Probably should do some Twitter error checking here
            twit = []
            
        if 'statuses' in twit:
            twit = twit['statuses']
        
        if not isinstance(twit, list):
            logging.warning("Twitter output should be a list...uh oh")
            twit = []

        # Keep track of the first id, so we can cache it for later
        firstId = twit[0]['id_str'] if len(twit) > 0 and 'id_str' in twit[0] else None

        for tweet in twit:
            try:
                tweet['title'] = tweet['text']
                tweet['description'] = tweet['text']
                tweet['author'] = tweet['user']['screen_name']
                tweet['link'] = 'http://twitter.com/{0}/status/{1}'.format(tweet['author'], tweet['id_str'])
                tweet['guid'] = tweet['id_str']
                tweet['pubDate'] = tweet['created_at']
                
                outItems.append(tweet)

                # Check if we have reached one we've seen before, then kill it
                if limitId and str(limitId) == tweet['id_str']:
                    break

            except Exception as e:
                logging.error("Error processing tweet")
                logging.error(tweet)
                logging.exception(e)
                continue

        if limitKey and firstId and firstId != limitId:
            # Cache the first id we got for this limit key, keep it for 15 mins
            memcache.set(limitKey, firstId, 15 * 60)

        return outDict

app = webapp2.WSGIApplication([('/feed/([a-zA-Z0-9_]{1,15})/([a-zA-Z0-9]+)/?(.*)', FeedHandler)], debug=True)

30f1c5951871be85b05f056df2a4c5ac2227980a
<mask token>


class FeedHandler(webapp2.RequestHandler):

    def get(self, username, accessCode, apiEndpoint=None):
        try:
            self.response.headers['Content-Type'] = 'application/rss+xml'
            if not apiEndpoint:
                apiEndpoint = 'statuses/home_timeline.json'
            twitRes = self.makeTwitterRequest(user=self.getUser(username,
                accessCode), api=apiEndpoint, params=self.getParametersDict())
            outDict = self.getRSSDictFromTwitterResponse(twitRes, username +
                apiEndpoint + urlparse.urlparse(self.request.url).query)
            self.response.out.write(xmltodict.unparse(outDict))
        except Exception as e:
            logging.exception(e)
            self.abort(500, detail=e)

    def handle_exception(self, exception, debug):
        if isinstance(exception, webapp2.HTTPException):
            self.response.out.write(exception.detail)
            self.response.set_status(exception.code)
        else:
            self.response.out.write('An error has occurred')
            self.response.set_status(500)

    def getUser(self, username, accessCode):
        result = memcache.get(username)
        if result is not None:
            return result
        query = TwitterUser.all()
        query.filter('username =', username)
        query.filter('accessCode =', accessCode)
        result = query.get()
        if not result:
            self.abort(401, detail='No Authorized Twitter User found')
        memcache.set(username, result, 60 * 30)
        return result

    def getParametersDict(self):
        urlInfo = urlparse.urlparse(self.request.url)
        params = urlparse.parse_qs(urlInfo.query)
        params.update((key, val[0]) for key, val in params.items())
        if 'count' not in params:
            params['count'] = 10
        return params

    def makeTwitterRequest(self, user, api, params):
        if user.accessSecret[:8] == 'DISABLED':
            self.abort(403, detail=
                'Your account has been disabled due to quota restrictions. Please contact @Tweet_2_RSS to get reinstated'
                )
        url = 'https://api.twitter.com/1.1/{0}'.format(api)
        client = oauth.TwitterClient(config.CONSUMER_KEY, config.
            CONSUMER_SECRET, config.CALLBACK)
        resp = client.make_request(url, user.accessToken, user.accessSecret,
            additional_params=params, protected=True)
        if resp.status_code == 200:
            return json.loads(resp.content)
    <mask token>


<mask token>

<mask token>


class FeedHandler(webapp2.RequestHandler):

    def get(self, username, accessCode, apiEndpoint=None):
        try:
            self.response.headers['Content-Type'] = 'application/rss+xml'
            if not apiEndpoint:
                apiEndpoint = 'statuses/home_timeline.json'
            twitRes = self.makeTwitterRequest(user=self.getUser(username,
                accessCode), api=apiEndpoint, params=self.getParametersDict())
            outDict = self.getRSSDictFromTwitterResponse(twitRes, username +
                apiEndpoint + urlparse.urlparse(self.request.url).query)
            self.response.out.write(xmltodict.unparse(outDict))
        except Exception as e:
            logging.exception(e)
            self.abort(500, detail=e)

    def handle_exception(self, exception, debug):
        if isinstance(exception, webapp2.HTTPException):
            self.response.out.write(exception.detail)
            self.response.set_status(exception.code)
        else:
            self.response.out.write('An error has occurred')
            self.response.set_status(500)

    def getUser(self, username, accessCode):
        result = memcache.get(username)
        if result is not None:
            return result
        query = TwitterUser.all()
        query.filter('username =', username)
        query.filter('accessCode =', accessCode)
        result = query.get()
        if not result:
            self.abort(401, detail='No Authorized Twitter User found')
        memcache.set(username, result, 60 * 30)
        return result

    def getParametersDict(self):
        urlInfo = urlparse.urlparse(self.request.url)
        params = urlparse.parse_qs(urlInfo.query)
        params.update((key, val[0]) for key, val in params.items())
        if 'count' not in params:
            params['count'] = 10
        return params

    def makeTwitterRequest(self, user, api, params):
        if user.accessSecret[:8] == 'DISABLED':
            self.abort(403, detail=
                'Your account has been disabled due to quota restrictions. Please contact @Tweet_2_RSS to get reinstated'
                )
        url = 'https://api.twitter.com/1.1/{0}'.format(api)
        client = oauth.TwitterClient(config.CONSUMER_KEY, config.
            CONSUMER_SECRET, config.CALLBACK)
        resp = client.make_request(url, user.accessToken, user.accessSecret,
            additional_params=params, protected=True)
        if resp.status_code == 200:
            return json.loads(resp.content)

    def getRSSDictFromTwitterResponse(self, twit, limitKey=None):
        limitId = memcache.get(limitKey) if limitKey else None
        outDict = {'rss': {'@version': '2.0', 'channel': {'title':
            'Tweet 2 RSS Feed', 'description':
            'Converting Twitter API Queries to RSS', 'link':
            'http://tweet-2-rss.appspot.com', 'item': []}}}
        outItems = outDict['rss']['channel']['item']
        if not twit:
            logging.warning("We obviously didn't get a valid dictionary")
            twit = []
        if 'statuses' in twit:
            twit = twit['statuses']
        if not isinstance(twit, list):
            logging.warning('Twitter output should be a list...uh oh')
            twit = []
        firstId = twit[0]['id_str'] if len(twit) > 0 and 'id_str' in twit[0
            ] else None
        for tweet in twit:
            try:
                tweet['title'] = tweet['text']
                tweet['description'] = tweet['text']
                tweet['author'] = tweet['user']['screen_name']
                tweet['link'] = 'http://twitter.com/{0}/status/{1}'.format(
                    tweet['author'], tweet['id_str'])
                tweet['guid'] = tweet['id_str']
                tweet['pubDate'] = tweet['created_at']
                outItems.append(tweet)
                if limitId and str(limitId) == tweet['id_str']:
                    break
            except Exception as e:
                logging.error('Error processing tweet')
                logging.error(tweet)
                logging.exception(e)
                continue
        if limitKey and firstId and firstId != limitId:
            memcache.set(limitKey, firstId, 15 * 60)
        return outDict


<mask token>

<mask token>
try:
    import json
except:
    from django.utils import simplejson as json
<mask token>


class FeedHandler(webapp2.RequestHandler):

    def get(self, username, accessCode, apiEndpoint=None):
        try:
            self.response.headers['Content-Type'] = 'application/rss+xml'
            if not apiEndpoint:
                apiEndpoint = 'statuses/home_timeline.json'
            twitRes = self.makeTwitterRequest(user=self.getUser(username,
                accessCode), api=apiEndpoint, params=self.getParametersDict())
            outDict = self.getRSSDictFromTwitterResponse(twitRes, username +
                apiEndpoint + urlparse.urlparse(self.request.url).query)
            self.response.out.write(xmltodict.unparse(outDict))
        except Exception as e:
            logging.exception(e)
            self.abort(500, detail=e)

    def handle_exception(self, exception, debug):
        if isinstance(exception, webapp2.HTTPException):
            self.response.out.write(exception.detail)
            self.response.set_status(exception.code)
        else:
            self.response.out.write('An error has occurred')
            self.response.set_status(500)

    def getUser(self, username, accessCode):
        result = memcache.get(username)
        if result is not None:
            return result
        query = TwitterUser.all()
        query.filter('username =', username)
        query.filter('accessCode =', accessCode)
        result = query.get()
        if not result:
            self.abort(401, detail='No Authorized Twitter User found')
        memcache.set(username, result, 60 * 30)
        return result

    def getParametersDict(self):
        urlInfo = urlparse.urlparse(self.request.url)
        params = urlparse.parse_qs(urlInfo.query)
        params.update((key, val[0]) for key, val in params.items())
        if 'count' not in params:
            params['count'] = 10
        return params

    def makeTwitterRequest(self, user, api, params):
        if user.accessSecret[:8] == 'DISABLED':
            self.abort(403, detail=
                'Your account has been disabled due to quota restrictions. Please contact @Tweet_2_RSS to get reinstated'
                )
        url = 'https://api.twitter.com/1.1/{0}'.format(api)
        client = oauth.TwitterClient(config.CONSUMER_KEY, config.
            CONSUMER_SECRET, config.CALLBACK)
        resp = client.make_request(url, user.accessToken, user.accessSecret,
            additional_params=params, protected=True)
        if resp.status_code == 200:
            return json.loads(resp.content)

    def getRSSDictFromTwitterResponse(self, twit, limitKey=None):
        limitId = memcache.get(limitKey) if limitKey else None
        outDict = {'rss': {'@version': '2.0', 'channel': {'title':
            'Tweet 2 RSS Feed', 'description':
            'Converting Twitter API Queries to RSS', 'link':
            'http://tweet-2-rss.appspot.com', 'item': []}}}
        outItems = outDict['rss']['channel']['item']
        if not twit:
            logging.warning("We obviously didn't get a valid dictionary")
            twit = []
        if 'statuses' in twit:
            twit = twit['statuses']
        if not isinstance(twit, list):
            logging.warning('Twitter output should be a list...uh oh')
            twit = []
        firstId = twit[0]['id_str'] if len(twit) > 0 and 'id_str' in twit[0
            ] else None
        for tweet in twit:
            try:
                tweet['title'] = tweet['text']
                tweet['description'] = tweet['text']
                tweet['author'] = tweet['user']['screen_name']
                tweet['link'] = 'http://twitter.com/{0}/status/{1}'.format(
                    tweet['author'], tweet['id_str'])
                tweet['guid'] = tweet['id_str']
                tweet['pubDate'] = tweet['created_at']
                outItems.append(tweet)
                if limitId and str(limitId) == tweet['id_str']:
                    break
            except Exception as e:
                logging.error('Error processing tweet')
                logging.error(tweet)
                logging.exception(e)
                continue
        if limitKey and firstId and firstId != limitId:
            memcache.set(limitKey, firstId, 15 * 60)
        return outDict


<mask token>

from google.appengine.api import memcache
import webapp2
from twitterUser import TwitterUser
import oauth
import config
import logging
import urlparse
try:
    import json
except:
    from django.utils import simplejson as json
import xmltodict


class FeedHandler(webapp2.RequestHandler):

    def get(self, username, accessCode, apiEndpoint=None):
        try:
            self.response.headers['Content-Type'] = 'application/rss+xml'
            if not apiEndpoint:
                apiEndpoint = 'statuses/home_timeline.json'
            twitRes = self.makeTwitterRequest(user=self.getUser(username,
                accessCode), api=apiEndpoint, params=self.getParametersDict())
            outDict = self.getRSSDictFromTwitterResponse(twitRes, username +
                apiEndpoint + urlparse.urlparse(self.request.url).query)
            self.response.out.write(xmltodict.unparse(outDict))
        except Exception as e:
            logging.exception(e)
            self.abort(500, detail=e)

    def handle_exception(self, exception, debug):
        if isinstance(exception, webapp2.HTTPException):
            self.response.out.write(exception.detail)
            self.response.set_status(exception.code)
        else:
            self.response.out.write('An error has occurred')
            self.response.set_status(500)

    def getUser(self, username, accessCode):
        result = memcache.get(username)
        if result is not None:
            return result
        query = TwitterUser.all()
        query.filter('username =', username)
        query.filter('accessCode =', accessCode)
        result = query.get()
        if not result:
            self.abort(401, detail='No Authorized Twitter User found')
        memcache.set(username, result, 60 * 30)
        return result

    def getParametersDict(self):
        urlInfo = urlparse.urlparse(self.request.url)
        params = urlparse.parse_qs(urlInfo.query)
        params.update((key, val[0]) for key, val in params.items())
        if 'count' not in params:
            params['count'] = 10
        return params

    def makeTwitterRequest(self, user, api, params):
        if user.accessSecret[:8] == 'DISABLED':
            self.abort(403, detail=
                'Your account has been disabled due to quota restrictions. Please contact @Tweet_2_RSS to get reinstated'
                )
        url = 'https://api.twitter.com/1.1/{0}'.format(api)
        client = oauth.TwitterClient(config.CONSUMER_KEY, config.
            CONSUMER_SECRET, config.CALLBACK)
        resp = client.make_request(url, user.accessToken, user.accessSecret,
            additional_params=params, protected=True)
        if resp.status_code == 200:
            return json.loads(resp.content)

    def getRSSDictFromTwitterResponse(self, twit, limitKey=None):
        limitId = memcache.get(limitKey) if limitKey else None
        outDict = {'rss': {'@version': '2.0', 'channel': {'title':
            'Tweet 2 RSS Feed', 'description':
            'Converting Twitter API Queries to RSS', 'link':
            'http://tweet-2-rss.appspot.com', 'item': []}}}
        outItems = outDict['rss']['channel']['item']
        if not twit:
            logging.warning("We obviously didn't get a valid dictionary")
            twit = []
        if 'statuses' in twit:
            twit = twit['statuses']
        if not isinstance(twit, list):
            logging.warning('Twitter output should be a list...uh oh')
            twit = []
        firstId = twit[0]['id_str'] if len(twit) > 0 and 'id_str' in twit[0
            ] else None
        for tweet in twit:
            try:
                tweet['title'] = tweet['text']
                tweet['description'] = tweet['text']
                tweet['author'] = tweet['user']['screen_name']
                tweet['link'] = 'http://twitter.com/{0}/status/{1}'.format(
                    tweet['author'], tweet['id_str'])
                tweet['guid'] = tweet['id_str']
                tweet['pubDate'] = tweet['created_at']
                outItems.append(tweet)
                if limitId and str(limitId) == tweet['id_str']:
                    break
            except Exception as e:
                logging.error('Error processing tweet')
                logging.error(tweet)
                logging.exception(e)
                continue
        if limitKey and firstId and firstId != limitId:
            memcache.set(limitKey, firstId, 15 * 60)
        return outDict


app = webapp2.WSGIApplication([(
    '/feed/([a-zA-Z0-9_]{1,15})/([a-zA-Z0-9]+)/?(.*)', FeedHandler)], debug
    =True)

from google.appengine.api import memcache
import webapp2
from twitterUser import TwitterUser
import oauth
import config
import logging
import urlparse
try:
    import json
except:
    from django.utils import simplejson as json
    
import xmltodict

class FeedHandler(webapp2.RequestHandler):

    def get(self, username, accessCode, apiEndpoint=None):
        try:
            self.response.headers['Content-Type'] = 'application/rss+xml'
        
            if not apiEndpoint:
                apiEndpoint = "statuses/home_timeline.json"
            
            twitRes = self.makeTwitterRequest(
                user = self.getUser(username, accessCode), 
                api = apiEndpoint, 
                params = self.getParametersDict())
            
            outDict = self.getRSSDictFromTwitterResponse(
                    twitRes, 
                    username + apiEndpoint + urlparse.urlparse(self.request.url).query)
            
            self.response.out.write(xmltodict.unparse(outDict))
            
        except Exception as e:
            logging.exception(e)
            self.abort(500, detail=e)

    def handle_exception(self, exception, debug):
        # If the exception is a HTTPException, use its error code.
        # Otherwise use a generic 500 error code.
        if isinstance(exception, webapp2.HTTPException):
            self.response.out.write(exception.detail)
            self.response.set_status(exception.code)
        else:
            self.response.out.write("An error has occurred")
            self.response.set_status(500)
            
    def getUser(self, username, accessCode):
        # First try memcache
        result = memcache.get(username)

        if result is not None:
            # We got a cached result
            return result
        
        # Nothing in cache or expired, fetch from datastore then cache that
        query = TwitterUser.all()
        query.filter("username =", username)
        query.filter("accessCode =", accessCode)
        
        result = query.get()
        
        if not result:
            # Query did not match any access tokens we have, oops
            self.abort(401, detail="No Authorized Twitter User found")

        # Add it to the cache now
        memcache.set(username, result, 60 * 30)
        
        return result
    
    def getParametersDict(self):
        urlInfo = urlparse.urlparse(self.request.url)
        
        params = urlparse.parse_qs(urlInfo.query)
        
        # get rid of the pesky lists that parse_qs produces
        params.update((key, val[0]) for key,val in params.items())
        
        if 'count' not in params:
            params['count'] = 10 
        
        return params
    
    def makeTwitterRequest(self, user, api, params):

        # Check if user has been disabled
        if user.accessSecret[:8] == 'DISABLED':
            self.abort(403, detail="Your account has been disabled due to quota restrictions. Please contact @Tweet_2_RSS to get reinstated")
        
        url = "https://api.twitter.com/1.1/{0}".format(api)
        
        client = oauth.TwitterClient(config.CONSUMER_KEY, config.CONSUMER_SECRET, config.CALLBACK)
            
        resp = client.make_request(url, user.accessToken, user.accessSecret, additional_params=params, protected=True)
        
        if resp.status_code == 200:
            return json.loads(resp.content)
        
    def getRSSDictFromTwitterResponse(self, twit, limitKey=None):
        
        limitId = memcache.get(limitKey) if limitKey else None

        outDict = {
            'rss' : {
                '@version' : '2.0',
                'channel' : {
                    'title' : 'Tweet 2 RSS Feed',
                    'description' : 'Converting Twitter API Queries to RSS',
                    'link' : 'http://tweet-2-rss.appspot.com',
                    'item' : []
                }
            }
        }
        
        outItems = outDict['rss']['channel']['item']
        if not twit:
            logging.warning("We obviously didn't get a valid dictionary")
            # TODO : Probably should do some Twitter error checking here
            twit = []
            
        if 'statuses' in twit:
            twit = twit['statuses']
        
        if not isinstance(twit, list):
            logging.warning("Twitter output should be a list...uh oh")
            twit = []

        # Keep track of the first id, so we can cache it for later
        firstId = twit[0]['id_str'] if len(twit) > 0 and 'id_str' in twit[0] else None

        for tweet in twit:
            try:
                tweet['title'] = tweet['text']
                tweet['description'] = tweet['text']
                tweet['author'] = tweet['user']['screen_name']
                tweet['link'] = 'http://twitter.com/{0}/status/{1}'.format(tweet['author'], tweet['id_str'])
                tweet['guid'] = tweet['id_str']
                tweet['pubDate'] = tweet['created_at']
                
                outItems.append(tweet)

                # Check if we have reached one we've seen before, then kill it
                if limitId and str(limitId) == tweet['id_str']:
                    break

            except Exception as e:
                logging.error("Error processing tweet")
                logging.error(tweet)
                logging.exception(e)
                continue

        if limitKey and firstId and firstId != limitId:
            # Cache the first id we got for this limit key, keep it for 15 mins
            memcache.set(limitKey, firstId, 15 * 60)

        return outDict

app = webapp2.WSGIApplication([('/feed/([a-zA-Z0-9_]{1,15})/([a-zA-Z0-9]+)/?(.*)', FeedHandler)], debug=True)
