Commits

Ki-baek Lee committed 6cbca41

Completed Bit.ly support and tested. Working on Twitpic.

Comments (0)

Files changed (7)

m2tadv/linkage/imgupload/__init__.py

+from abc import ABCMeta, abstractmethod
+import urllib2
+from oauth import oauth
+import priv_settings as settings
+
+class ImgUploader:
+    __metaclass__ = ABCMeta
+    
+    @abstractmethod
+    def upload(self, photourl):
+        pass
+    
+class Twitpic(object):
+    
+    TWITTER_SERVER = 'http://api.twitter.com'
+    VERIFY_CREDENTIALS_URL = TWITTER_SERVER + '/1/account/verify_credentials.json'
+    
+    def __init__(self, consumer_key = settings.TWITTER_CONSUMER_KEY,
+                        consumer_secret = settings.TWITTER_CONSUMER_SECRET,
+                        access_token_key,
+                        access_token_secret,
+                        api_key = settings.TWITPIC_APIKEY):
+        self.consumer_key = consumer_key
+        self.consumer_secret = consumer_secret
+        self.access_token = oauth.OAuthToken(access_token_key, access_token_secret)
+        self.api_key = api_key
+        self.consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
+        
+    def upload(self, photourl, message):
+        filedata = self.get_filedata(photourl)
+    
+    def get_filedata(self, url):
+        return urllib2.urlopen(url)
+        
+ImgUploader.register(Twitpic)

m2tadv/linkage/models.py

 class Preference(models.Model):
     LINK_METHOD_CHOICES = (
         ('N', 'Don\'t include link'),
-        ('I', 'After each link text'),
         ('O', 'At the end of the text'),
     )
     user = models.ForeignKey(User)
     link_method = models.CharField(max_length=1, choices=LINK_METHOD_CHOICES,
-                                    default='N')
+                                    default='O')
     tag = models.CharField(max_length=255)
     last_updated = models.DateTimeField(default=datetime.now())

m2tadv/linkage/update.py

         self.me2day = Me2dayLinkage.objects.get(user=user)
         self.twitter = TwitterLinkage.objects.get(user=user)
     
+    def _get_twitter(self):
+        t_auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY,
+                                     settings.TWITTER_CONSUMER_SECRET)
+        t_auth.set_access_token(self.twitter.key, self.twitter.secret)
+        t_api = tweepy.API(t_auth)
+        
+        return t_api
+    
     def fetch(self, debug):
         '''
         Fetch from me2DAY
         for link in links:
             if re.match("http://me2day.net/(.*)$", link[0]) == None:
                 if not url_shortener == None:
-                    link[0] = url_shortener.get_shortened_url(link[0])
+                    link += url_shortener.shorten(link[0]),
+                else:
+                    link += None,
                 result.append(link)
         
         return result
         photourl = item['iconUrl'].replace('me2daythumb', 'me2day') \
                     .replace('?type=s44', '')
         if not img_upload_service == None:
-            photourl = img_upload_service.upload_and_geturl()
+            photourl = img_upload_service.upload(photourl)
             
         return photourl
         
         '''
         Make body text for Twitter
         '''
-        pass
+        necessary_length = 0
+        for link in links:
+            if link[2] == None:
+                necessary_length += len(link[0]) + 1
+            else:
+                necessary_length += len(link[2]) + 1
+        
+        if photo != None:
+            necessary_length += len(photo) + 1
+        
+        #item['tBody'] = item['body']
+        
+        #for link in links:
+        #    item['tBody'] = item['tBody'].replace("<a href='" + link[0] + "'>"
+        #                                             + link[1] + "</a>",
+        #                                             link[1])
+        
+        item['tBody'] = item['textBody'][:140 - necessary_length]
+        
+        for link in links:
+            if link[2] != None:
+                item['tBody'] += ' ' + link[2]
+            else:
+                item['tBody'] += ' ' + link[0]
+                
+        if photo:
+            item['tBody'] += ' ' + photo
+        
+        return item['tBody']
     
-    def push(self, items):
+    def push(self, item, t_api = None):
         '''
         Tweet it!
         '''
-        t_auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY,
-                                   settings.TWITTER_CONSUMER_SECRET)
-        t_auth.set_access_token(self.twitter.key, self.twitter.secret)
-        t_api = tweepy.API(t_auth)
+        if t_api == None:
+            t_api = self._get_twitter()
+        
+        t_api.update_status(item)
+            
+    def push_all(self, items, debug):
+        t_api = self._get_twitter()
         
         for item in items:
-            t_api.update_status(item)
+            if debug:
+                print item
+            else:
+                self.push(item, t_api)
             
     def finish(self):
-        self.preferences.last_updated = datetime.now()
+        self.preference.last_updated = datetime.now()

m2tadv/linkage/urlshortener/__init__.py

 from abc import ABCMeta, abstractmethod
+import json
+import urllib2
+import priv_settings as settings
 
 class URLShortener:
     __metaclass__ = ABCMeta
     
     @abstractmethod
     def get_shortened_url(self, long_url):
-        pass
+        pass
+    
+class Bitly(object):
+    '''
+    Bit.ly URL Shortener Service
+    '''
+
+
+    def __init__(self, x_login = None, x_apikey = None):
+        '''
+        Constructor
+        '''
+        self.x_login = x_login
+        self.x_apikey = x_apikey
+    
+    def shorten(self, long_url):
+        request_url = 'http://api.bit.ly/v3/shorten?login=' \
+                        + settings.BITLY_USERNAME \
+                        + '&apiKey=' \
+                        + settings.BITLY_APIKEY \
+                        + '&longUrl=' \
+                        + long_url
+        if self.x_apiname and self.x_apikey:
+            request_url += '&x_login=' \
+                            + self.x_login \
+                            + '&x_apiKey=' \
+                            + self.x_apikey
+                            
+        result = json.load(urllib2.urlopen(request_url))
+        return result['data']['url']
+        
+URLShortener.register(Bitly)

m2tadv/linkage/urlshortener/bitly.py

-'''
-Created on 2010. 9. 17.
-
-@author: Administrator
-'''
-import urllib2
-from linkage.urlshortener import URLShortener
-
-class Bitly(object):
-    '''
-    Bit.ly URL Shortener Service
-    '''
-
-
-    def __init__(self, apiname = None, apikey = None):
-        '''
-        Constructor
-        '''
-        self.apiname = apiname
-        self.apikey = apikey
-    
-    def get_shortened_url(self, long_url):
-        pass
-        
-URLShortener.register(Bitly)

m2tadv/linkage/views.py

 from linkage.models import Me2dayLinkage, TwitterLinkage, Preference
 from linkage.update import Update
 from linkage.forms import PreferenceForm
+from linkage.urlshortener import *
+from linkage.imgupload import *
 import settings
 import tweepy
 
                 me2day = Me2dayLinkage.objects.get(user=user)
                 twitter = TwitterLinkage.objects.get(user=user)
                 update = Update(user)
-                newitems = update.fetch(True)
+                newitems = update.fetch(debug=True)
                 if len(newitems) <= 0:
                     continue
+                tbody = []
+                url_shortener = Bitly()
                 for item in newitems:
-                    links = update.parse_link(item)
+                    links = update.parse_link(item, url_shortener)
                     photo = None
                     if item['contentType'] == 'me2photo':
                         photo = update.parse_photo(item)
-                    update.push(update.make_twitterbody(item, links, photo))
+                    tbody.append(update.make_twitterbody(item, links, photo))
+                if len(tbody) > 0:
+                    update.push_all(tbody,debug=True)
                 update.finish()
             except Me2dayLinkage.DoesNotExist:
                 pass

m2tadv/priv_settings.py.example

 ME2DAY_APP_KEY = ''
 TWITTER_CONSUMER_KEY = ''
 TWITTER_CONSUMER_SECRET = ''
+BITLY_USERNAME = ''
+BITLY_APIKEY = ''
+TWITPIC_APIKEY = ''