77import logging
88import mimetypes
99import os
10+ from platform import python_version
1011import sys
1112import time
1213from urllib .parse import urlencode
1314
1415import requests
1516
17+ import tweepy
1618from tweepy .errors import (
1719 BadRequest , Forbidden , HTTPException , NotFound , TooManyRequests ,
1820 TweepyException , TwitterServerError , Unauthorized
@@ -76,7 +78,8 @@ class API:
7678 def __init__ (
7779 self , auth = None , * , cache = None , host = 'api.twitter.com' , parser = None ,
7880 proxy = None , retry_count = 0 , retry_delay = 0 , retry_errors = None ,
79- timeout = 60 , upload_host = 'upload.twitter.com' , wait_on_rate_limit = False
81+ timeout = 60 , upload_host = 'upload.twitter.com' , user_agent = None ,
82+ wait_on_rate_limit = False
8083 ):
8184 self .auth = auth
8285 self .cache = cache
@@ -95,6 +98,15 @@ def __init__(
9598 self .retry_errors = retry_errors
9699 self .timeout = timeout
97100 self .upload_host = upload_host
101+
102+ if user_agent is None :
103+ user_agent = (
104+ f"Python/{ python_version ()} "
105+ f"Requests/{ requests .__version__ } "
106+ f"Tweepy/{ tweepy .__version__ } "
107+ )
108+ self .user_agent = user_agent
109+
98110 self .wait_on_rate_limit = wait_on_rate_limit
99111
100112 # Attempt to explain more clearly the parser argument requirements
@@ -121,6 +133,10 @@ def request(
121133
122134 self .cached_result = False
123135
136+ if headers is None :
137+ headers = {}
138+ headers ["User-Agent" ] = self .user_agent
139+
124140 # Build the request URL
125141 path = f'/1.1/{ endpoint } .json'
126142 if upload_api :
0 commit comments