Python config.USER_AGENT Examples

The following are 12 code examples of config.USER_AGENT(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module config , or try the search function .
Example #1
Source File: delete_comments.py    From compilebot with Apache License 2.0 6 votes vote down vote up
def main():
    match_text = "INSERT TEXT HERE"

    r = praw.Reddit(
        user_agent=config.USER_AGENT,
        client_id=config.R_CLIENT_ID,
        client_secret=config.R_CLIENT_SECRET,
        username=config.R_USERNAME,
        password=config.R_PASSWORD,
    )

    user = r.redditor(config.R_USERNAME)
    comments = list(user.comments.new())

    count = 0
    for c in comments:
        if match_text in c.body:
            c.delete()
            count += 1
    print "Comments deleted: {}".format(count) 
Example #2
Source File: compilebot.py    From compilebot with Apache License 2.0 6 votes vote down vote up
def log(message, alert=False):
    """Log messages along with a timestamp in a log file. If the alert
    option is set to true, send a message to the admin's reddit inbox.
    """
    t = time.strftime('%y-%m-%d %H:%M:%S', time.localtime())
    message = "{}: {}\n".format(t, message)
    message = message.encode('utf8', 'replace')
    if config.LOG_FILE:
        with open(config.LOG_FILE, 'a') as f:
            f.write(message)
    else:
        print(message, end='')
    if alert and config.ADMIN:
        r = praw.Reddit(config.USER_AGENT)
        r.login(config.R_USERNAME, config.R_PASSWORD)
        admin_alert = message
        subject = "CompileBot Alert"
        r.redditor(config.ADMIN).message(subject, admin_alert) 
Example #3
Source File: compilebot.py    From compilebot with Apache License 2.0 6 votes vote down vote up
def main():
    r = praw.Reddit(
        user_agent=config.USER_AGENT,
        client_id=config.R_CLIENT_ID,
        client_secret=config.R_CLIENT_SECRET,
        username=config.R_USERNAME,
        password=config.R_PASSWORD,
    )
    if config.SUBREDDIT:
        config.BANNED_USERS
        config.BANNED_USERS = get_banned(r)
    # Iterate though each new comment/message in the inbox and
    # process it appropriately.
    inbox = r.inbox.unread()
    for new in inbox:
        try:
            process_unread(new, r)
        except:
            tb = traceback.format_exc()
            # Notify admin of any errors
            log("Error processing comment {c.id}\n"
                "{traceback}".format(c=new, traceback=code_block(tb)), alert=True)
        finally:
            new.mark_read() 
Example #4
Source File: main.py    From sneakpeek with MIT License 6 votes vote down vote up
def start():
    """Start the sneakpeek application."""
    logging.info("Starting application")
    logging.info("Instantiating Reddit instance")
    reddit = praw.Reddit(
        client_id=config.CLIENT["ID"],
        client_secret=config.CLIENT["SECRET"],
        user_agent=config.USER_AGENT,
        username=config.USERNAME,
        password=config.PASSWORD)

    try:
        scan(reddit.subreddit(config.SUBREDDIT))
    except Exception as exception:
        # This should never happen,
        # because it breaks the infinite subreddit monitoring
        # provided by subreddit.stream.submissions()
        logging.critical("Exception occurred while scanning. This should never happen.")
        logging.critical(exception) 
Example #5
Source File: css_parser.py    From DotaResponsesRedditBot with MIT License 6 votes vote down vote up
def populate_heroes():
    """Method to update heroes in the Heroes table with hero names and proper css classes names as
    taken from the DotA2 subreddit and hero flair images from the reddit directory.

    Uses rapidfuzz for fuzzy matching of hero names to name found in `.flair-name` property in css.
    """
    hero_names = db_api.get_all_hero_names()

    response = requests.get(STYLESHEET_URL, headers={'User-Agent': USER_AGENT})
    r = json.loads(response.text)
    stylesheet = r['data']['stylesheet']

    r = re.compile(FLAIR_REGEX)
    for flair in r.finditer(stylesheet):
        flair_css = flair['css_class']
        img_path = flair['img_path']
        flair_hero = img_path[6:]

        match, confidence = process.extractOne(flair_hero, hero_names)
        if confidence >= 90:
            db_api.update_hero(hero_name=match, img_path=img_path, flair_css=flair_css) 
Example #6
Source File: tasks.py    From microblog.pub with GNU Affero General Public License v3.0 5 votes vote down vote up
def task_fetch_og_meta() -> _Response:
    task = p.parse(flask.request)
    app.logger.info(f"task={task!r}")
    iri = task.payload
    try:
        activity = ap.fetch_remote_activity(iri)
        app.logger.info(f"activity={activity!r}")
        if activity.has_type(ap.ActivityType.CREATE):
            note = activity.get_object()
            links = opengraph.links_from_note(note.to_dict())
            og_metadata = opengraph.fetch_og_metadata(config.USER_AGENT, links)
            for og in og_metadata:
                if not og.get("image"):
                    continue
                config.MEDIA_CACHE.cache_og_image(og["image"], iri)

            app.logger.debug(f"OG metadata {og_metadata!r}")
            DB.activities.update_one(
                {"remote_id": iri}, {"$set": {"meta.og_metadata": og_metadata}}
            )

        app.logger.info(f"OG metadata fetched for {iri}: {og_metadata}")
    except (ActivityGoneError, ActivityNotFoundError):
        app.logger.exception(f"dropping activity {iri}, skip OG metedata")
        return ""
    except requests.exceptions.HTTPError as http_err:
        if 400 <= http_err.response.status_code < 500:
            app.logger.exception("bad request, no retry")
            return ""
        app.logger.exception("failed to fetch OG metadata")
        raise TaskError() from http_err
    except Exception as err:
        app.logger.exception(f"failed to fetch OG metadata for {iri}")
        raise TaskError() from err

    return "" 
Example #7
Source File: tasks.py    From microblog.pub with GNU Affero General Public License v3.0 5 votes vote down vote up
def task_send_webmention() -> _Response:
    task = p.parse(flask.request)
    app.logger.info(f"task={task!r}")
    note_url = task.payload["note_url"]
    link = task.payload["link"]
    remote_id = task.payload["remote_id"]
    try:
        app.logger.info(f"trying to send webmention source={note_url} target={link}")
        webmention_endpoint = discover_webmention_endpoint(link)
        if not webmention_endpoint:
            app.logger.info("no webmention endpoint")
            return ""

        resp = requests.post(
            webmention_endpoint,
            data={"source": note_url, "target": link},
            headers={"User-Agent": config.USER_AGENT},
        )
        app.logger.info(f"webmention endpoint resp={resp}/{resp.text}")
        resp.raise_for_status()
    except HTTPError as err:
        app.logger.exception("request failed")
        if 400 <= err.response.status_code <= 499:
            app.logger.info("client error, no retry")
            return ""

        raise TaskError() from err
    except Exception as err:
        app.logger.exception(f"failed to cache actor for {link}/{remote_id}/{note_url}")
        raise TaskError() from err

    return "" 
Example #8
Source File: account.py    From DotaResponsesRedditBot with MIT License 5 votes vote down vote up
def get_account():
    """Method that provides the connection to Reddit API using OAuth.
        :return: Reddit instance.
    """
    return praw.Reddit(client_id=config.CLIENT_ID,
                       client_secret=config.CLIENT_SECRET,
                       user_agent=config.USER_AGENT,
                       username=config.USERNAME,
                       password=config.PASSWORD) 
Example #9
Source File: gen.py    From web-traffic-generator with MIT License 5 votes vote down vote up
def do_request(url):
    """ A method which loads a page """

    global data_meter
    global good_requests
    global bad_requests

    debug_print("  Requesting page...".format(url))

    headers = {'user-agent': config.USER_AGENT}

    try:
        r = requests.get(url, headers=headers, timeout=5)
    except:
        # Prevent 100% CPU loop in a net down situation
        time.sleep(30)
        return False

    page_size = len(r.content)
    data_meter += page_size

    debug_print("  Page size: {}".format(hr_bytes(page_size)))
    debug_print("  Data meter: {}".format(hr_bytes(data_meter)))

    status = r.status_code

    if (status != 200):
        bad_requests += 1
        debug_print("  Response status: {}".format(r.status_code), Colors.RED)
        if (status == 429):
            debug_print(
                "  We're making requests too frequently... sleeping longer...")
            config.MIN_WAIT += 10
            config.MAX_WAIT += 10
    else:
        good_requests += 1

    debug_print("  Good requests: {}".format(good_requests))
    debug_print("  Bad reqeusts: {}".format(bad_requests))

    return r 
Example #10
Source File: headers.py    From get_jobs with MIT License 5 votes vote down vote up
def get_headers():
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'User-Agent': random.choice(USER_AGENT),
        'Accept-Language': 'en-US,en;q=0.5',
        'Connection': 'keep-alive',
        'Accept-Encoding': 'gzip, deflate',
        'Upgrade-Insecure-Requests': '1'
    }
    return headers 
Example #11
Source File: proxy.py    From get_jobs with MIT License 5 votes vote down vote up
def __init__(self):
        self.header = None
        self.proxy_list = []  # 代理IP列表
        self.user_agent = USER_AGENT  # 请求的UA
        self.user_agent_66ip = random.choice(USER_AGENT)  # 66ip代理使用
        self.header = self.get_header  # 请求头

        # 节省创建对象的资源
        self.test_proxy_urls = self.get_test_site  # 测试代理IP的网址
        self.proxy_urls = self.get_proxy_site  # 免费代理网址 
Example #12
Source File: tasks.py    From microblog.pub with GNU Affero General Public License v3.0 4 votes vote down vote up
def task_post_to_remote_inbox() -> _Response:
    """Post an activity to a remote inbox."""
    task = p.parse(flask.request)
    app.logger.info(f"task={task!r}")
    payload, to = task.payload["payload"], task.payload["to"]
    try:
        app.logger.info("payload=%s", payload)
        app.logger.info("generating sig")
        signed_payload = json.loads(payload)

        app.logger.info("to=%s", to)
        resp = requests.post(
            to,
            data=json.dumps(signed_payload),
            auth=SIG_AUTH,
            headers={
                "Content-Type": config.HEADERS[1],
                "Accept": config.HEADERS[1],
                "User-Agent": config.USER_AGENT,
            },
        )
        app.logger.info("resp=%s", resp)
        app.logger.info("resp_body=%s", resp.text)
        resp.raise_for_status()
    except HTTPError as err:
        track_failed_send(to)

        app.logger.exception("request failed")
        if 400 <= err.response.status_code <= 499:
            app.logger.info("client error, no retry")
            return ""

        raise TaskError() from err
    except requests.RequestException:
        track_failed_send(to)

        app.logger.exception("request failed")

    except Exception as err:
        app.logger.exception("task failed")
        raise TaskError() from err

    track_successful_send(to)

    return ""