|
| 1 | +#!/usr/bin/python |
| 2 | +import md5 |
| 3 | +import os |
| 4 | +import re |
| 5 | +import tempfile |
| 6 | + |
| 7 | +class _FileCacheError(Exception): |
| 8 | + '''Base exception class for FileCache related errors''' |
| 9 | + |
| 10 | +class _FileCache(object): |
| 11 | + |
| 12 | + DEPTH = 3 |
| 13 | + |
| 14 | + def __init__(self,root_directory=None): |
| 15 | + self._InitializeRootDirectory(root_directory) |
| 16 | + |
| 17 | + def Get(self, key): |
| 18 | + path = self._GetPath(key) |
| 19 | + if os.path.exists(path): |
| 20 | + return open(path).read() |
| 21 | + else: |
| 22 | + return None |
| 23 | + |
| 24 | + def Set(self, key, data): |
| 25 | + path = self._GetPath(key) |
| 26 | + directory = os.path.dirname(path) |
| 27 | + if not os.path.exists(directory): |
| 28 | + os.makedirs(directory) |
| 29 | + if not os.path.isdir(directory): |
| 30 | + raise _FileCacheError('%s exists but is not a directory' % directory) |
| 31 | + temp_fd, temp_path = tempfile.mkstemp() |
| 32 | + temp_fp = os.fdopen(temp_fd, 'w') |
| 33 | + temp_fp.write(data) |
| 34 | + temp_fp.close() |
| 35 | + if not path.startswith(self._root_directory): |
| 36 | + raise _FileCacheError('%s does not appear to live under %s' % |
| 37 | + (path, self._root_directory)) |
| 38 | + if os.path.exists(path): |
| 39 | + os.remove(path) |
| 40 | + os.rename(temp_path, path) |
| 41 | + |
| 42 | + def Remove(self, key): |
| 43 | + path = self._GetPath(key) |
| 44 | + if not path.startswith(self._root_directory): |
| 45 | + raise _FileCacheError('%s does not appear to live under %s' % |
| 46 | + (path, self._root_directory )) |
| 47 | + if os.path.exists(path): |
| 48 | + os.remove(path) |
| 49 | + |
| 50 | + def GetCachedTime(self, key): |
| 51 | + path = self._GetPath(key) |
| 52 | + if os.path.exists(path): |
| 53 | + return os.path.getmtime(path) |
| 54 | + else: |
| 55 | + return None |
| 56 | + |
| 57 | + def _GetUsername(self): |
| 58 | + '''Attempt to find the username in a cross-platform fashion.''' |
| 59 | + try: |
| 60 | + return os.getenv('USER') or \ |
| 61 | + os.getenv('LOGNAME') or \ |
| 62 | + os.getenv('USERNAME') or \ |
| 63 | + os.getlogin() or \ |
| 64 | + 'nobody' |
| 65 | + except (AttributeError, IOError, OSError), e: |
| 66 | + return 'nobody' |
| 67 | + |
| 68 | + def _GetTmpCachePath(self): |
| 69 | + username = self._GetUsername() |
| 70 | + cache_directory = 'python.cache_' + username |
| 71 | + return os.path.join(tempfile.gettempdir(), cache_directory) |
| 72 | + |
| 73 | + def _InitializeRootDirectory(self, root_directory): |
| 74 | + if not root_directory: |
| 75 | + root_directory = self._GetTmpCachePath() |
| 76 | + root_directory = os.path.abspath(root_directory) |
| 77 | + if not os.path.exists(root_directory): |
| 78 | + os.mkdir(root_directory) |
| 79 | + if not os.path.isdir(root_directory): |
| 80 | + raise _FileCacheError('%s exists but is not a directory' % |
| 81 | + root_directory) |
| 82 | + self._root_directory = root_directory |
| 83 | + |
| 84 | + def _GetPath(self, key): |
| 85 | + try: |
| 86 | + hashed_key = md5(key).hexdigest() |
| 87 | + except TypeError: |
| 88 | + hashed_key = md5.new(key).hexdigest() |
| 89 | + |
| 90 | + return os.path.join(self._root_directory, |
| 91 | + self._GetPrefix(hashed_key), |
| 92 | + hashed_key) |
| 93 | + |
| 94 | + def _GetPrefix(self, hashed_key): |
| 95 | + return os.path.sep.join(hashed_key[0:_FileCache.DEPTH]) |
| 96 | + |
| 97 | +class ParseTweet: |
| 98 | + # compile once on import |
| 99 | + regexp = { "RT": "^RT", "MT":r"^MT", "ALNUM": r"(@[a-zA-Z0-9_]+)", |
| 100 | + "HASHTAG": r"(#[\w\d]+)", "URL": r"([http://]?[a-zA-Z\d\/]+[\.]+[a-zA-Z\d\/\.]+)" } |
| 101 | + regexp = dict((key,re.compile(value)) for key,value in regexp.items()) |
| 102 | + |
| 103 | + def __init__(self,timeline_owner,tweet): |
| 104 | + ''' timeline_owner : twitter handle of user account. tweet - 140 chars from feed; object does all computation on construction |
| 105 | + properties: |
| 106 | + RT, MT - boolean |
| 107 | + URLs - list of URL |
| 108 | + Hashtags - list of tags |
| 109 | + ''' |
| 110 | + self.Owner = timeline_owner |
| 111 | + self.tweet = tweet |
| 112 | + self.UserHandles = ParseTweet.getUserHandles(tweet) |
| 113 | + self.Hashtags = ParseTweet.getHashtags(tweet) |
| 114 | + self.URLs = ParseTweet.getURLs(tweet) |
| 115 | + self.RT = ParseTweet.getAttributeRT(tweet) |
| 116 | + self.MT = ParseTweet.getAttributeMT(tweet) |
| 117 | + |
| 118 | + # additional intelligence |
| 119 | + if ( self.RT and len(self.UserHandles) > 0 ): #change the owner of tweet? |
| 120 | + self.Owner = self.UserHandles[0] |
| 121 | + return |
| 122 | + |
| 123 | + def __str__(self): |
| 124 | + ''' for display method ''' |
| 125 | + return "owner %s, urls: %d, hashtags %d, user_handles %d, len_tweet %d, RT = %s, MT = %s"%(self.Owner,len(self.URLs),len(self.Hashtags),len(self.UserHandles), len(self.tweet), self.RT,self.MT) |
| 126 | + |
| 127 | + @staticmethod |
| 128 | + def getAttributeRT( tweet ): |
| 129 | + """ see if tweet is a RT """ |
| 130 | + return re.search(ParseTweet.regexp["RT"],tweet.strip()) != None |
| 131 | + |
| 132 | + @staticmethod |
| 133 | + def getAttributeMT( tweet ): |
| 134 | + """ see if tweet is a MT """ |
| 135 | + return re.search(ParseTweet.regexp["MT"],tweet.strip()) != None |
| 136 | + |
| 137 | + @staticmethod |
| 138 | + def getUserHandles( tweet ): |
| 139 | + """ given a tweet we try and extract all user handles in order of occurrence""" |
| 140 | + return re.findall(ParseTweet.regexp["ALNUM"],tweet) |
| 141 | + |
| 142 | + @staticmethod |
| 143 | + def getHashtags( tweet ): |
| 144 | + """ return all hashtags""" |
| 145 | + return re.findall(ParseTweet.regexp["HASHTAG"],tweet) |
| 146 | + |
| 147 | + @staticmethod |
| 148 | + def getURLs( tweet ): |
| 149 | + """ URL : [http://]?[\w\.?/]+""" |
| 150 | + return re.findall(ParseTweet.regexp["URL"],tweet) |
0 commit comments