def test_timestamp_to_local_datetime_pdtWorksFine(self):
     self.assertEqual(
         "2016-03-13T03:00:00-0700PDT",
         datetimeutil.timestamp_to_local_datetime(1457863200).strftime(
             datetimeutil.ISO_FORMAT_WITH_TZ_NAME
         ),
     )
 def test_timestamp_to_local_datetime_pstWorksFine(self):
     self.assertEqual(
         "2016-03-13T01:00:00-0800PST",
         datetimeutil.timestamp_to_local_datetime(1457859600).strftime(
             datetimeutil.ISO_FORMAT_WITH_TZ_NAME
         ),
     )
    def _print_entry(self, entry, indent=""):
        print(indent + "Title:", entry['title'].encode('utf8'))
        if 'updated' not in entry:
            entry['updated'] = entry['published']

        updated = datetimeutil.timestamp_to_local_datetime(
            entry['updated'] / 1000
        )
        print(
            indent + "Updated:",
            updated.strftime("%Y/%m/%d %H:%M"),
        )
        published = datetimeutil.timestamp_to_local_datetime(
            entry['published'] / 1000
        )
        print(
            indent + "Published:",
            published.strftime("%Y/%m/%d %H:%M"),
        )
        crawled = datetimeutil.timestamp_to_local_datetime(
            entry['crawled'] / 1000
        )
        print(
            indent + "Crawled:",
            crawled.strftime("%Y/%m/%d %H:%M"),
        )
        origin = self._get_null_safe(entry, 'origin', 'title')
        if origin:
            origin = origin.encode("utf8")
        print(indent + "Origin:", origin)
        print(
            indent + "Source:",
            self._get_null_safe(entry, 'alternate', 'href'),
        )
        print(indent + "Fingerprint:", entry['fingerprint'])
        print(indent + "Id:", entry['id'])
    def entries(self, do_cleanup=False):
        """Prints out all the entries for all the feeds"""
        _log.debug("Get all the category ids first")
        categories = self.get_categories()

        duplicated_entries = []
        seen_fingerprints = set()
        total_num_entries = 0

        endpoint = self._get_endpoint("/streams/contents")
        for category in categories:
            headers = self._get_headers()
            params = {
                'streamId': category['id'],
                'count': 1000,  # max is 1000
                'unreadOnly': "true"
            }
            resp = requests.get(endpoint, headers=headers, params=params)
            _log.debug("Response: %s", resp.__dict__)

            content = json.loads(resp.text)
            #from sorno import debuggingutil
            #debuggingutil.ipython_here()

            title = None
            if 'title' in content:
                title = content['title']
            else:
                title = content['id']
                if '/' in title:
                    title = title.split('/')[-1]

            print("Title:", title)
            updated = datetimeutil.timestamp_to_local_datetime(
                content['updated'] / 1000
            )
            print("Updated:", updated.strftime("%Y/%m/%d %H:%M"))
            print("Id:", content['id'])
            if 'items' in content:
                num_entries = len(content['items'])
                total_num_entries += num_entries
                for i, entry in enumerate(content['items']):
                    print(" " * 4 + "(%d/%d)" % (i + 1, num_entries))
                    self._print_entry(entry, indent=" " * 4)
                    print("")

                    if entry['fingerprint'] in seen_fingerprints:
                        duplicated_entries.append(entry)
                    else:
                        seen_fingerprints.add(entry['fingerprint'])

        print("\nTotal of %d entries\n" % total_num_entries)

        print("Duplicated entries:")
        num_entries = len(duplicated_entries)
        for i, duplicated_entry in enumerate(duplicated_entries):
            print(" " * 4 + "(%d/%d)" % (i + 1, num_entries))
            self._print_entry(duplicated_entry, indent=" " * 4)
            print("")

        if num_entries == 0:
            print("None")
        elif do_cleanup:
            ans = consoleutil.confirm(
                "Mark %d duplicated entries as read?" % num_entries)
            if ans:
                resp = self.mark_entries_as_read(
                    [entry['id'] for entry in duplicated_entries]
                )

                if resp.status_code == 200:
                    print("Done")
                else:
                    print(resp.status_code, resp.reason)