def test_from_json(self): json_dict = { 'id': 47891, 'to': 'alleged', 'from_lang': 'es', 'to_lang': 'en', 'title' : '', 'url': 'http://www.eluniversal.com.mx/mundo/homenajean-en-los-angeles-actriz-porno-que-enfrenta-trump', 'origin_importance': 0, 'starred': False, 'origin_rank':1000, 'learned_datetime':'2017-10-10T10:10:10', 'created_day': '2017-10-10T10:10:10', 'from': 'supuesta', 'context': 'Hurpy durpy durp' } bm = Bookmark.from_json(json_dict) assert bm.id == 47891 assert bm.to == 'alleged' assert bm.from_lang == 'es' assert bm.to_lang == 'en' assert bm.url.title == '' assert bm.url.url == 'http://www.eluniversal.com.mx/mundo/homenajean-en-los-angeles-actriz-porno-que-enfrenta-trump' assert bm.origin_importance == '' assert bm.starred == False assert bm.from_ == 'supuesta' assert bm.context == 'Hurpy durpy durp'
def get_learned_bookmarks(count=100): resp = get(f"{LEARNED_BOOKMARKS}/{count}", session_needed=True) _json = json.loads(resp.content) bookmarks = [] for data in _json: bm = Bookmark.from_json(data) bookmarks.append(bm) return bookmarks
def get_starred_bookmarks(): resp = get(STARRED_BOOKMARKS + "/50", session_needed=True) _json = json.loads(resp.content) bookmarks = [] for data in _json: bm = Bookmark.from_json(data) bookmarks.append(bm) return bookmarks
def get_bookmarks_by_date(date=None): _payload = {"with_context": True, "with_title": True} if date: _date_string = date.strftime('%Y-%m-%dT%H:%M:%S') _payload["after_date"] = _date_string resp = post(BOOKMARKS_BY_DATE, payload=_payload, session_needed=True) _json = json.loads(resp.content) sorted_dates = [] urls_for_date = {} contexts_for_url = {} bookmarks_for_context = {} bookmark_counts_by_date = {} article_ids_for_urls = {} for data in _json: each_date = datetime.datetime.strptime(data["date"], "%A, %d %B %Y") sorted_dates.append(each_date) urls_for_date.setdefault(each_date, []) for bookmark_json in data["bookmarks"]: # try: each_bookmark = Bookmark.from_json(bookmark_json) each_bookmark.set_date(each_date) each_context = each_bookmark.context if each_bookmark.url not in urls_for_date[each_date]: urls_for_date[each_date].append(each_bookmark.url) contexts_for_url.setdefault(each_bookmark.url, []) if not each_context in contexts_for_url.get(each_bookmark.url): contexts_for_url[each_bookmark.url].append(each_context) bookmarks_for_context.setdefault(each_context, []) bookmarks_for_context[each_context].append(each_bookmark) article_ids_for_urls[each_bookmark.url] = each_bookmark.article_id # except Exception: # print("Parsing bookmark failed") bookmark_counts_by_date.setdefault(each_date, set()).add(len(data["bookmarks"])) return { "sorted_dates": sorted_dates, "urls_for_date": urls_for_date, "contexts_for_url": contexts_for_url, "bookmarks_for_context": bookmarks_for_context, "bookmark_counts_by_date": bookmark_counts_by_date, "article_ids_for_urls": article_ids_for_urls }
def get_bookmarks_for_article(article_id: int, user_id: int = None): _payload = {"with_context": True, "with_title": True} if user_id: resp = post(BOOKMARKS_FOR_ARTICLE + f'{article_id}/{user_id}', payload=_payload, session_needed=True) else: resp = post(BOOKMARKS_FOR_ARTICLE + f'{article_id}', payload=_payload, session_needed=True) _json = json.loads(resp.content) print(_json) bookmarks = [Bookmark.from_json(each) for each in _json['bookmarks']] dates = [] contexts_for_date = OrderedDict() bookmarks_for_context = {} for each in bookmarks: if each.date not in dates: dates.append(each.date) if each.date not in contexts_for_date: contexts_for_date[each.date] = [] if each.context not in contexts_for_date[each.date]: contexts_for_date[each.date].append(each.context) if each.context not in bookmarks_for_context: bookmarks_for_context[each.context] = [] bookmarks_for_context[each.context].append(each) return { "sorted_dates": dates[::-1], "contexts_for_date": contexts_for_date, "bookmarks_for_context": bookmarks_for_context, "article_title": _json['article_title'], "article_id": article_id }
def test_importance_below_1(self): text = Bookmark.string_representation_of_importance(0.1) assert text == ''
def test_importance_float(self): text = Bookmark.string_representation_of_importance(2.31) assert text == "<span style='font-size:8pt'>|</span><span style='font-size:7pt'>|</span>"
def test_first_N_words_of_context_longer(self): text = '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45.' shortened_text = '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42' context = Bookmark.get_first_N_words_of_context(text) assert context == shortened_text
def test_first_N_words_of_context_shorter(self): text = '1 2 3 4 5 6 7' context = Bookmark.get_first_N_words_of_context(text) assert context == text