give bitbot a unique User-Agent

closes #206
This commit is contained in:
jesopo 2019-11-20 14:42:34 +00:00
parent 11c3104d02
commit 4d30263315
2 changed files with 6 additions and 8 deletions

View file

@ -7,7 +7,6 @@ LD_TYPE = ("application/ld+json; "
"profile=\"https://www.w3.org/ns/activitystreams\"") "profile=\"https://www.w3.org/ns/activitystreams\"")
JRD_TYPE = "application/jrd+json" JRD_TYPE = "application/jrd+json"
ACTIVITY_TYPE = "application/activity+json" ACTIVITY_TYPE = "application/activity+json"
USERAGENT = "BitBot (%s) Fediverse" % IRCBot.VERSION
def split_username(s): def split_username(s):
if s[0] == "@": if s[0] == "@":
@ -26,7 +25,7 @@ def activity_request(url, data=None, method="GET", type=ACTIVITY_TYPE,
else: else:
headers = {"Accept": type} headers = {"Accept": type}
request = utils.http.Request(url, headers=headers, useragent=USERAGENT, request = utils.http.Request(url, headers=headers,
content_type=content_type, post_data=data, method=method, json=True, content_type=content_type, post_data=data, method=method, json=True,
json_body=True, fallback_encoding="utf8") json_body=True, fallback_encoding="utf8")
return utils.http.request(request) return utils.http.request(request)

View file

@ -1,9 +1,8 @@
import asyncio, codecs, ipaddress, re, signal, socket, traceback, typing import asyncio, codecs, ipaddress, re, signal, socket, traceback, typing
import urllib.error, urllib.parse, uuid import urllib.error, urllib.parse, uuid
import json as _json import json as _json
import bs4, netifaces, requests import bs4, netifaces, requests, tornado.httpclient
import tornado.httpclient from src import IRCBot, utils
from src import utils
REGEX_URL = re.compile("https?://\S+", re.I) REGEX_URL = re.compile("https?://\S+", re.I)
@ -29,8 +28,8 @@ def url_sanitise(url: str):
url = url[:-1] url = url[:-1]
return url return url
DEFAULT_USERAGENT = ("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 " USERAGENT = "Mozilla/5.0 (compatible; BitBot/%s; +%s" % (
"(KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36") IRCBot.VERSION, IRCBot.URL)
RESPONSE_MAX = (1024*1024)*100 RESPONSE_MAX = (1024*1024)*100
SOUP_CONTENT_TYPES = ["text/html", "text/xml", "application/xml"] SOUP_CONTENT_TYPES = ["text/html", "text/xml", "application/xml"]
@ -113,7 +112,7 @@ class Request(object):
if not "Accept-Language" in headers: if not "Accept-Language" in headers:
headers["Accept-Language"] = "en-GB" headers["Accept-Language"] = "en-GB"
if not "User-Agent" in headers: if not "User-Agent" in headers:
headers["User-Agent"] = self.useragent or DEFAULT_USERAGENT headers["User-Agent"] = self.useragent or USERAGENT
if not "Content-Type" in headers and self.content_type: if not "Content-Type" in headers and self.content_type:
headers["Content-Type"] = self.content_type headers["Content-Type"] = self.content_type
return headers return headers