def _import_bookmarks(username, password, token='.deliciousbackup'): """ Import bookmarks from delicious. Returns a list of dict objects representing posts """ token_path = os.path.join(os.path.abspath(os.path.expanduser('~')), token) if os.path.exists(token_path): with open(token_path) as f: last_run = datetime.fromtimestamp(float(f.read())) else: last_run = time.time() with open(token_path, 'w') as f: f.write(str(last_run)) last_run = datetime.fromtimestamp(last_run) dapi = DeliciousAPI(username, password) api_date = dapi.posts_update() api_date = api_date['update']['time'] last_update = datetime(api_date.tm_year, api_date.tm_mon, api_date.tm_mday, hour=api_date.tm_hour, minute=api_date.tm_min, second=api_date.tm_sec) if last_update > last_run: #last_run.microsecond=0 posts = dapi.posts_all(fromdt=last_run.isoformat()) else: posts = dapi.posts_all() return posts['posts']
def update_links(self): api = DeliciousAPI(self.username, self.password) # get the latest update according to delicious latest_update_utc = api.posts_update() latest_update_utc = utc_to_epoch(latest_update_utc['update']['time']) if self.last_known_update_utc < latest_update_utc: posts = api.posts_all(fromdt=self._last_update_timestring()) for p in posts['posts']: link = LinkPost() link.blog_id = self.blog_id link.title = p['description'] link.link = p['href'] link.description_raw = p['extended'] link.publish(publish_time = self._parse_datetime(p['time'])) link.tags = p['tag'] link.save() self.last_known_update_utc = self._parse_time(posts['update']) + 1 self.save()
class DeliciousSyncDB: def __init__(self, username, password): """ Initializes the DeliciousSyncDB object. This functions accepts two arguments username and password. Both are required. """ self.api = DeliciousAPI(username, password) def _cleanTags(self, tags): """ Utility function that sets tags to lower case, removes commas and double quotes and removes all duplicate tags. Returns a unicode string. """ tags = tags.lower().replace('\"', '').replace(',', '').split(' ') tags = set(tags) tags = ' '.join(tags) return u'%s' % tags def _syncPost(self, post): """ Utility function that saves bookmarks to the local database. In the case a bookmark already exists it will be updated instead. """ time_obj = time.strptime(post['time'], "%Y-%m-%dT%H:%M:%SZ") save_date = datetime.datetime(*time_obj[0:7]) try: shared = post['shared'] except KeyError: shared = True finally: if shared == 'no': shared = False tags = self._cleanTags(post['tag']) d = { 'title': post['description'], 'url': post['href'], 'tags': tags, 'notes': post['extended'], 'post_hash': post['hash'], 'post_meta': post['meta'], 'save_date': save_date, 'shared': shared } b, created = Bookmark.objects.get_or_create(url = d['url'], defaults = d) if created == False: if not b.post_meta == unicode(d['post_meta']): b = Bookmark( id=b.id, title=d['title'], url=d['url'], tags=d['tags'], notes=d['notes'], post_hash=d['post_hash'], post_meta=d['post_meta'], save_date=d['save_date'], shared=d['shared'] ) b.save(syncAPI=True) def syncRecent(self, results=15): """ Syncronizes recent Delicious boomarks to a local database. This uses the posts/all api call instead of using the posts/recent call. Doing so provides the meta attribute in order to update previously saved bookmarks with modified data from Delicious. syncRecent takes one argument for results. If not specified the default number of results returned is 15. """ posts = self.api.posts_all(results = str(results)) for post in posts['posts']: self._syncPost(post) def syncAll(self): """ Syncronizes all Delicious boomarks to a local database. Using the meta attribute previously saved bookmarks will also be updated as well. """ posts = self.api.posts_all() for post in posts['posts']: self._syncPost(post) def processQueue(self): """ Queue processor to process local bookmarks that are going to be pushed to Delicious using posts/add. Bookmarks saved locally are flagged as queued and unflagged as they have been processed. If a boomark is unsucessfully updated the program will fail silently and move on to the next one until the entire queue has been processed once through. """ bookmarks = Bookmark.objects.filter(queued=True) for bookmark in bookmarks: time.sleep(1.5) try: if bookmark.shared == False: shared = 'no' else: shared = 'yes' self.api.posts_add( bookmark.url, bookmark.title, extended=bookmark.notes, tags=bookmark.tags, shared=shared, replace='yes') bookmark.queued = False bookmark.save(syncAPI=True) except: pass
from pydelicious import DeliciousAPI from getpass import getpass # logon to delicious account = DeliciousAPI('igniteflow', getpass('Password:'******'posts'] # inspect your data for post in posts: print post['description'], post['tag'], post['href']
print 'Update existing bookmark: ', entry['uri'] meta = getBookmarkMeta(entry['uri']) own = deliciousApi.posts_get(url=entry['uri']) result = {'url': meta['url'], 'description': own['posts'][0]['description'], 'extended': own['posts'][0]['extended'] if '' != own['posts'][0]['extended'] else meta['extended'], 'tags': ' '.join(tags + meta['tags']) + ' ' + own['posts'][0]['tag']} deliciousApi.posts_add(result['url'], result['description'], extended=result['extended'], tags=result['tags'], replace=True, shared=False) # # # # ## ## ## # # # # # # # # # # # ## # # # # # # # # # # # # # # ###### # # # # # # # # # # # ## # # # # # # # # # Init delicious api deliciousApi = DeliciousAPI(deliciousUser, deliciousPassword) allPosts = deliciousApi.posts_all() # Read and process JSON Data jsonDataFile = open(importFile) jsonData = json.load(jsonDataFile) jsonDataFile.close() processEntry(jsonData) print 'All done ... have a nice day!'
import getpass from pydelicious import DeliciousAPI, DeliciousItemExistsError sourceUser = raw_input("Username [source]: ") sourcePasswd = getpass.getpass("Password [source]: ") sourceDlcs = DeliciousAPI(sourceUser, sourcePasswd) print "Getting all bookmarks from source:" + sourceUser sourceBkmrks = sourceDlcs.posts_all()['posts'] print "Done getting bookmarks" fromTag = ' from:' + sourceUser destUser = raw_input("Username [destination]: ") destPasswd = getpass.getpass("Password [destination]: ") destDlcs = DeliciousAPI(destUser, destPasswd) sourceSize = len(sourceBkmrks) for i in range(46,sourceSize): bkmrk = sourceBkmrks[i] href = bkmrk['href'] desc = bkmrk['description'] ext = bkmrk['extended'] if 'extended' in bkmrk else '' share = bkmrk['shared'] if 'shared' in bkmrk else 'yes' date = bkmrk['time'] tag = bkmrk['tag'] if 'tag' in bkmrk else '' tag += fromTag print 'Copying (' + str(i+1) + '/' + str(sourceSize) + ') '+ desc + ' (' + href + ')' try : destDlcs.posts_add(href, desc, extended = ext, tags = tag, dt= date, shared = share) except DeliciousItemExistsError: desc + ' (' + href + ') already exists'