Repository: chapinb/chickadee

Issue:
VirusTotal rate limiter does not store last request time
**Describe the bug**

The sleeper function checks the last request time to determine how long to sleep for. This value is only set at initialization and does not update per-request.


**Version (please complete the following information):**
 - OS: Any (macOS)
 - Version: 20200802.0
 - Python version: 3.7.7

**Additional context**



Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 104b977..fc87bab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
 # Changelog
 
+## 20200805.0
+
+### Fixed
+
+* Addressed bug in rate limiting VirusTotal. [Issue-62](https://github.com/chapinb/chickadee/issues/62)
+
 ## 20200802.0
 
 ### Added
diff --git a/doc_src/source/conf.py b/doc_src/source/conf.py
index ad6c81a..31e098a 100644
--- a/doc_src/source/conf.py
+++ b/doc_src/source/conf.py
@@ -22,7 +22,7 @@ copyright = 'MIT 2020, Chapin Bryce'
 author = 'Chapin Bryce'
 
 # The full version, including alpha/beta/rc tags
-release = '20200802'
+release = '20200805'
 
 
 # -- General configuration ---------------------------------------------------
diff --git a/libchickadee/__init__.py b/libchickadee/__init__.py
index 202fa79..e7393a1 100644
--- a/libchickadee/__init__.py
+++ b/libchickadee/__init__.py
@@ -145,7 +145,7 @@ library from the command line.
 """
 
 __author__ = 'Chapin Bryce'
-__date__ = 20200802
-__version__ = 20200802.0
+__date__ = 20200805
+__version__ = 20200805.0
 __license__ = 'MIT Copyright 2020 Chapin Bryce'
 __desc__ = '''Yet another GeoIP resolution tool.'''
diff --git a/libchickadee/chickadee.py b/libchickadee/chickadee.py
index fd12d15..d43deac 100644
--- a/libchickadee/chickadee.py
+++ b/libchickadee/chickadee.py
@@ -65,7 +65,7 @@ Usage
       -V, --version         Displays version
       -l LOG, --log LOG     Path to log file (default: chickadee.log)
 
-    Built by Chapin Bryce, v.20200801.0
+    Built by Chapin Bryce, v.20200805.0
 
 
 .. _chickadee-examples:
@@ -171,7 +171,7 @@ from libchickadee.parsers.evtx import EVTXParser
 
 
 __author__ = 'Chapin Bryce'
-__date__ = 20200407.2
+__date__ = 20200805
 __license__ = 'GPLv3 Copyright 2019 Chapin Bryce'
 __desc__ = '''Yet another GeoIP resolution tool.
 
@@ -672,7 +672,7 @@ def arg_handling(args):
                         help='Include debug log messages')
     parser.add_argument('-V', '--version', action='version',
                         help='Displays version',
-                        version=str(__date__))
+                        version=str(__version__))
     parser.add_argument(
         '-l',
         '--log',
diff --git a/libchickadee/resolvers/virustotal.py b/libchickadee/resolvers/virustotal.py
index 1c21411..e538089 100644
--- a/libchickadee/resolvers/virustotal.py
+++ b/libchickadee/resolvers/virustotal.py
@@ -84,7 +84,7 @@ from . import ResolverBase
 logger = logging.getLogger(__name__)
 
 __author__ = 'Chapin Bryce'
-__date__ = 20200302
+__date__ = 20200805
 __license__ = 'MIT Copyright 2020 Chapin Bryce'
 __desc__ = 'Resolver for VirusTotal'
 
@@ -165,6 +165,7 @@ class ProResolver(ResolverBase):
             'ip': self.data
         }
 
+        self.last_request = datetime.now()
         rdata = requests.get(
             self.uri, params=params
         )
diff --git a/setup.py b/setup.py
index d4d126a..67ae1ca 100644
--- a/setup.py
+++ b/setup.py
@@ -1,6 +1,6 @@
 """Installer for chickadee"""
 import setuptools
-from libchickadee import __version__
+from libchickadee import __version__, __desc__, __author__
 
 with open('README.md') as fh:
     long_description = fh.read()
@@ -8,8 +8,8 @@ with open('README.md') as fh:
 setuptools.setup(
     name='chickadee',
     version=__version__,
-    description='Yet another GeoIP resolution tool.',
-    author='Chapin Bryce',
+    description=__desc__,
+    author=__author__,
     author_email='python@chapinb.com',
     url='https://github.com/chapinb/chickadee',
     long_description=long_description,


Test patch:
diff --git a/libchickadee/test/test_resolver_virustotal.py b/libchickadee/test/test_resolver_virustotal.py
index dfb54a1..c0dd7d4 100644
--- a/libchickadee/test/test_resolver_virustotal.py
+++ b/libchickadee/test/test_resolver_virustotal.py
@@ -1,5 +1,6 @@
 """VirusTotal Resolver Tests."""
 import datetime
+import time
 import unittest
 import json
 import os
@@ -8,7 +9,7 @@ from unittest.mock import patch, MagicMock
 from libchickadee.resolvers.virustotal import ProResolver
 
 __author__ = 'Chapin Bryce'
-__date__ = 20200114
+__date__ = 20200805
 __license__ = 'MIT Copyright 2020 Chapin Bryce'
 __desc__ = '''Yet another GeoIP resolution tool.'''
 
@@ -101,6 +102,16 @@ class IPAPITestCase(unittest.TestCase):
                 self.assertIsNone(actual)
                 self.assertEqual(mock_log.records[0].message, err_msg)
 
+    @patch("libchickadee.resolvers.virustotal.requests.get")
+    def test_sleeper(self, mock_requests):
+        initial_time = datetime.datetime.now()
+        self.resolver.last_request = initial_time
+        time.sleep(2)
+        mock_requests.return_value.status_code = 403
+
+        self.resolver.query(data='1.1.1.1')
+        self.assertGreaterEqual(self.resolver.last_request, initial_time + datetime.timedelta(seconds=2))
+
 
 if __name__ == "__main__":
     unittest.main()