[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Adjust example of writing to disk



commit 1aa6117bcbda629727e7351b2abb88b7d5f1a697
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Jul 5 12:37:37 2019 -0700

    Adjust example of writing to disk
    
    On reflection I won't add special functions for downloading descriptors to
    disk. That's uncommon, and takes a couple lines for users to do themselves.
    Adjusting our example to show how.
---
 stem/descriptor/collector.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/stem/descriptor/collector.py b/stem/descriptor/collector.py
index 0bbf4251..746e4b1d 100644
--- a/stem/descriptor/collector.py
+++ b/stem/descriptor/collector.py
@@ -29,17 +29,20 @@ With this you can either download and read directly from CollecTor...
 ::
 
   import datetime
+  import os
   import stem.descriptor
   import stem.descriptor.collector
 
   yesterday = datetime.date.today() - datetime.timedelta(1)
+  path = os.path.expanduser('~/descriptor_cache/server_desc_today')
 
-  stem.descriptor.collector.download_server_descriptors(
-    destination = '~/descriptor_cache',
-    start = yesterday,
-  ).join()
+  with open(path, 'wb') as cache_file:
+    for desc in stem.descriptor.collector.get_server_descriptors(start = yesterday):
+      cache_file.write(desc.get_bytes())
 
-  for desc in stem.descriptor.parse_file('~/descriptor_cache', descriptor_type = 'server-descriptor 1.0'):
+  # then later...
+
+  for desc in stem.descriptor.parse_file(path, descriptor_type = 'server-descriptor 1.0'):
     if desc.exit_policy.is_exiting_allowed():
       print('  %s (%s)' % (desc.nickname, desc.fingerprint))
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits