當前位置: 首頁>>代碼示例>>Python>>正文


Python config.USER_AGENT屬性代碼示例

本文整理匯總了Python中config.USER_AGENT屬性的典型用法代碼示例。如果您正苦於以下問題:Python config.USER_AGENT屬性的具體用法?Python config.USER_AGENT怎麽用?Python config.USER_AGENT使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在config的用法示例。


在下文中一共展示了config.USER_AGENT屬性的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: main

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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) 
開發者ID:renfredxh,項目名稱:compilebot,代碼行數:22,代碼來源:delete_comments.py

示例2: log

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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) 
開發者ID:renfredxh,項目名稱:compilebot,代碼行數:20,代碼來源:compilebot.py

示例3: main

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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() 
開發者ID:renfredxh,項目名稱:compilebot,代碼行數:26,代碼來源:compilebot.py

示例4: start

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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) 
開發者ID:fterh,項目名稱:sneakpeek,代碼行數:21,代碼來源:main.py

示例5: populate_heroes

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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) 
開發者ID:Jonarzz,項目名稱:DotaResponsesRedditBot,代碼行數:23,代碼來源:css_parser.py

示例6: task_fetch_og_meta

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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 "" 
開發者ID:tsileo,項目名稱:microblog.pub,代碼行數:38,代碼來源:tasks.py

示例7: task_send_webmention

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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 "" 
開發者ID:tsileo,項目名稱:microblog.pub,代碼行數:34,代碼來源:tasks.py

示例8: get_account

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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) 
開發者ID:Jonarzz,項目名稱:DotaResponsesRedditBot,代碼行數:11,代碼來源:account.py

示例9: do_request

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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 
開發者ID:ReconInfoSec,項目名稱:web-traffic-generator,代碼行數:43,代碼來源:gen.py

示例10: get_headers

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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 
開發者ID:Eeyhan,項目名稱:get_jobs,代碼行數:12,代碼來源:headers.py

示例11: __init__

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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  # 免費代理網址 
開發者ID:Eeyhan,項目名稱:get_jobs,代碼行數:12,代碼來源:proxy.py

示例12: task_post_to_remote_inbox

# 需要導入模塊: import config [as 別名]
# 或者: from config import USER_AGENT [as 別名]
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 "" 
開發者ID:tsileo,項目名稱:microblog.pub,代碼行數:47,代碼來源:tasks.py


注:本文中的config.USER_AGENT屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。