Пример #1
0
def search_tweets(self, search_terms):

    raw_list = []  # initiate raw list with a value.
    my_id_counter = 0

    try:
        my_id = self.api.me().id
    except tweepy.TweepError as e:
        log_tweepy_error_message(
            self, 'Unable to get bot info from twitter in search tweets call',
            e)
        raise tweepy.TweepError("search_tweets my_id error")

    try:
        for status in tweepy.Cursor(self.api.search, q=search_terms,
                                    lang='en').items(self.max_tweets_search):
            if my_id != status.author.id:  #exclude my tweets from the search results
                tweet_data = [
                    status.id, status.author.id, status.text
                ]  # tweet data with fields required for downstream processing
                raw_list.append(tweet_data)
            else:
                my_id_counter += 1
    except tweepy.TweepError as e:
        log_tweepy_error_message(self, 'Tweepy error in search_tweets()', e)
        raise tweepy.TweepError("search_tweets tweepy_cursor error")

    self.lgr.info('# of tweets searched:, ' + str(self.max_tweets_search))
    self.lgr.info('# of raw tweets tweeted by me:, ' + str(my_id_counter))
    self.lgr.info('# of tweets returned from search_tweets():, ' +
                  str(len(raw_list)))

    return raw_list
Пример #2
0
 def judge(self, *args, **kwargs):
     if not self.api:
         raise tweepy.TweepError('Twitter api NOT ready!')
     if not self.user:
         raise tweepy.TweepError('Twitter user NOT ready!')
     method(self, *args, **kwargs)
     return self
Пример #3
0
def check_shop():
    try:
        with open('Python/Cache/shop.json', 'r') as file:
            Cached = json.load(file)
        data = requests.get('https://api.peely.de/v1/shop')
        new = data.json()
        if data.status_code != 200:
            return
    except:
        return
    if new != Cached:
        url = new["discordurl"]
        if SETTINGS.shopimagedate is True:
            SETTINGS.shopimagetext = "Item Shop from " + str(
                datetime.utcnow().__format__('%d.%m.%Y'))
        if SETTINGS.shopimageurl or SETTINGS.shopimagetext != "":
            lang = "en"
            if SETTINGS.lang:
                lang = SETTINGS.lang
            url = f"https://api.peely.de/v1/shop/custom?background={SETTINGS.shopimageurl}&text={SETTINGS.shopimagetext}&lang={lang}"
        try:
            print("NEW Shop")
            print(url)
            MODULES.tweet_image(url=url, message=get_text("shop"))
        except Exception as ex:
            raise tweepy.TweepError(ex)
        with open('Cache/shop.json', 'w') as file:
            json.dump(new, file, indent=3)
Пример #4
0
        def user_timeline(self, screen_name, include_rts, count):
            """
            Get a user's timeline
            :return: list
            """
            class Box:
                def __init__(self):
                    self.coordinates = [[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0],
                                         [7.0, 8.0]]]

            class Place:
                def __init__(self, place_name):
                    self.full_name = place_name
                    self.bounding_box = Box()

            class Tweet:
                def __init__(self):
                    if screen_name == 'nocoords':
                        self.coordinates = None
                        self.place = Place('cool place')
                    else:
                        self.coordinates = {'coordinates': [1, 2]}
                        self.place = Place('test')

            if screen_name == 'error':
                raise tweepy.TweepError('uh oh')
            elif screen_name != 'no tweets':
                return [Tweet()]
            else:
                return []
Пример #5
0
 def test_error_reporting(self):
     with unittest.mock.patch('tweepy.Cursor.items') as mock:
         mock.side_effect = tweepy.TweepError('', Response(500))
         error_callback = unittest.mock.Mock()
         api = twitter.TwitterAPI(self.credentials, on_error=error_callback)
         api.search_authors('hello', max_tweets=5)
         self.assertEqual(error_callback.call_count, 1)
Пример #6
0
    def API(auth_yaml, create_on_demand=False):
        ''':return: The authenticated api.API instance'''

        if create_on_demand:
            tweepyx.create_auth_yaml_on_demand(auth_yaml)

        if not os.path.isfile(auth_yaml):
            raise FileNotFoundError(
                'Please create file "{}" with contents:\n{}'.format(
                    auth_yaml, tweepyx.YAML_EXAMPLE))

        with open(auth_yaml, 'r') as yaml_file:
            try:
                read_yaml = yaml.safe_load(yaml_file)
                credentials = (read_yaml['twitter']['consumer']['key'],
                               read_yaml['twitter']['consumer']['secret'],
                               read_yaml['twitter']['access']['key'],
                               read_yaml['twitter']['access']['secret'])
            except:
                # pylint: disable=raise-missing-from
                raise tweepy.TweepError(
                    'Please check file "{0}" for proper contents:\n{1}'.format(
                        auth_yaml, tweepyx.YAML_EXAMPLE))

        consumer_key, consumer_secret, access_key, access_secret = credentials

        oauth_handler = tweepy.OAuthHandler(consumer_key, consumer_secret)
        oauth_handler.set_access_token(access_key, access_secret)

        return tweepy.API(auth_handler=oauth_handler,
                          compression=True,
                          wait_on_rate_limit=True,
                          wait_on_rate_limit_notify=True)
Пример #7
0
 def api_show_friendship_error(self, api, source_id=5091001, target_id=None):
     self.api_calls += 1
     resp = requests.Response()
     resp.status_code = 429
     setattr(api.base_api, 'last_response', resp)
     twitter_rate_limit_parsed = [{"code": 88, "message": "Rate limit exceeded"}]
     raise tweepy.TweepError(twitter_rate_limit_parsed, resp)
Пример #8
0
def check_leaks():
    try:
        with open('Cache/leaks.json', 'r') as file:
            Cached = json.load(file)
        data = requests.get('https://api.peely.de/v1/leaks')
        new = data.json()
        if data.status_code != 200:
            return
    except Exception as ex:
        print(ex, "leaks")
        return
    if new != Cached:
        url = "https://peely.de/leaks"
        if SETTINGS.leaksimageurl or SETTINGS.leaksimagetext != "":
            lang = "en"
            if SETTINGS.lang:
                lang = SETTINGS.lang
            url = f"https://api.peely.de/v1/leaks/custom?background={SETTINGS.leaksimageurl}&text={SETTINGS.leaksimagetext}&lang={lang}"
        try:
            print("NEW Leaks")
            MODULES.tweet_image(url=url, message=get_text("leaks"))
        except Exception as ex:
            raise tweepy.TweepError(ex)
        with open('Cache/leaks.json', 'w') as file:
            json.dump(new, file, indent=3)
Пример #9
0
def get_user_tweets(api=None, user=None, collection=None, n_tweets=5000):
    """Fetches a user tweets into database.

    Keyword Arguments:
        api {Tweepy} -- wrapper for the API as provided by Twitter
        (default: {None})
        user {str} -- twitter username or ID (default: {None})
        collection {str} -- database collection name (default: {None})
        n_tweets{int} -- number of tweets to fetch (default: {5000})

    Returns:
        int -- number of user tweets
    """

    logging.info("Fetching {}'s tweets....".format(user))

    tweet_count = 0

    bar = progressbar.ProgressBar()
    cursor = tweepy.Cursor(api.user_timeline, id=user,
                           tweet_mode='extended').items(n_tweets)
    for status in bar(cursor):
        tweet_count += 1
        tweet = status._json
        id_ = {"_id": tweet['id_str']}
        new_document = {**id_, **tweet}
        try:
            collection.insert_one(new_document)
        except DuplicateKeyError:
            logging.info(f"found duplicate key: {tweet['id_str']}")
            continue

    if not tweet_count:
        raise tweepy.TweepError('User has no tweet.')
Пример #10
0
    def __get_user_details(self, api):
        user = api.verify_credentials()

        if user:
            return {'screen_name': user.screen_name,
                    'user_id': user.id_str}
        else:
            raise tweepy.TweepError('Unable to get the user details, invalid oauth token!')
Пример #11
0
    def on_error(self, status_code):
        """Called when a non-200 status code is returned"""

        logging.warning("status code={}".format(status_code))
        if status_code == 401:
            # UNAUTHORIZED
            raise tweepy.TweepError(
                reason="Missing or incorrect authentication credentials",
                api_code=status_code)
        elif status_code == 420:
            # RATE LIMIT
            raise tweepy.RateLimitError(
                reason="The request limit for this resource has been reached")
        else:
            raise tweepy.TweepError(
                "Unhandled exception: {}".format(status_code),
                api_code=status_code)
Пример #12
0
def login(request):
    auth = tweepy.OAuthHandler(settings.CONSUMER_KEY, settings.CONSUMER_SECRET,
                               settings.CALLBACK_URL)
    try:
        redirect_url = auth.get_authorization_url()
    except tweepy.TweepError:
        raise tweepy.TweepError("Error! Failed to get request token")
    request.session["request_token"] = auth.request_token
    return redirect(redirect_url)
Пример #13
0
def authenicate_oauth(auth: Any) -> str:
    try:
        redirect_url = auth.get_authorization_url()
    except Exception as e:
        raise tweepy.TweepError(e)

    if webbrowser.open_new_tab(redirect_url):
        return input("Verifier: ")
    else:
        raise ValueError()
Пример #14
0
def fake_send_dm_rate_limit_headers_zero_remains(api_proxy,
                                                 id_or_username,
                                                 text=None):
    resp = requests.Response()
    setattr(api_proxy.base_api, 'last_response', resp)
    resp.headers['X-Rate-Limit-Limit'] = '15'
    resp.headers['X-Rate-Limit-Remaining'] = '0'
    resp.headers['X-Rate-Limit-Reset'] = str(
        datetime_to_timestamp(now()) + QUARTER_HOUR_SEC)
    raise tweepy.TweepError('Test rate limits (from headers) hit.', resp)
Пример #15
0
 def _get_request_token(self, access_type=None):
     try:
         url = self._get_oauth_url('request_token')
         if access_type:
             url += '?x_auth_access_type=%s' % access_type
         return self.oauth.fetch_request_token(url, proxies=self.proxies)
     except Exception as e:
         # TODO: handle bad authorization exception (config.ini not set)
         # Error: Token request failed with code 400, response was '{"errors":[{"code":215,"message":"Bad Authentication data."}]}'.
         raise tweepy.TweepError(e)
Пример #16
0
 def tw(self) -> None:
     flag = "n"
     while flag != "y":
         text = input("Text: ")
         print(text)
         flag = input("Tweet? (y or n): ")
     try:
         self.api.update_status(text)
     except Exception as e:
         raise tweepy.TweepError(e)
     print("success")
Пример #17
0
def fake_send_dm_rate_limit_in_err_body(api_proxy,
                                        id_or_username,
                                        text=None,
                                        code=RATE_LIMIT_CODE):
    resp = requests.Response()
    resp.status_code = 429
    setattr(api_proxy.base_api, 'last_response', resp)
    twitter_rate_limit_parsed = [{
        "code": code,
        "message": "Rate limit exceeded"
    }]
    raise tweepy.TweepError(twitter_rate_limit_parsed, resp)
Пример #18
0
    def __init__(self):
        ctoken, csecret = FileImport().read_app_key_file()
        auth = tp.OAuthHandler(ctoken, csecret)

        try:
            redirect_url = auth.get_authorization_url()
        except tp.TweepError as e:
            if '"code":32' in e.reason:
                raise tp.TweepError(
                    """Failed to get the request token. Perhaps the Consumer Key
                and / or secret in your 'keys.json' is incorrect?""")
            else:
                raise e

        webbrowser.open(redirect_url)
        token = auth.request_token["oauth_token"]
        verifier = input("Please enter Verifier Code: ")
        auth.request_token = {
            'oauth_token': token,
            'oauth_token_secret': verifier
        }
        try:
            auth.get_access_token(verifier)
        except tp.TweepError as e:
            if "Invalid oauth_verifier parameter" in e.reason:
                raise tp.TweepError("""Failed to get access token! Perhaps the
                                    verifier you've entered is wrong.""")
            else:
                raise e

        if not os.path.isfile('tokens.csv'):
            with open('tokens.csv', 'a', newline='') as f:
                writer = csv.writer(f)
                writer.writerow(["token", "secret"])
            f.close()

        with open('tokens.csv', 'a', newline='') as f:
            writer = csv.writer(f)
            writer.writerow([auth.access_token, auth.access_token_secret])
        f.close()
Пример #19
0
    def decode_tweet(self, content):
        try:
            info = [json.loads(content)]
            if self.exception_handlder.is_tweet_exception(info):
                logger.error(info)
                raise tweepy.TweepError(content)

            #logger.info("json decoded successfully: "+str(info))
        except ValueError as err:
            logger.error(err.message)
            #logger.error(content)
            info = self.decode_multiple_json(content)
            #logger.error(str(info))
        return info
Пример #20
0
def tweet_image(url, message):
    if SETTINGS.nopost is True:
        return
    auth = tweepy.OAuthHandler(
        consumer_key=SETTINGS.TWITTER_TOKEN["consumer_key"],
        consumer_secret=SETTINGS.TWITTER_TOKEN["consumer_secret"])
    auth.set_access_token(key=SETTINGS.TWITTER_TOKEN["access_token_key"],
                          secret=SETTINGS.TWITTER_TOKEN["access_token_secret"])
    client = tweepy.API(auth)
    try:
        auth.get_username()
    except:
        raise KeyError("INVALID TWITTER KEYS")
    request = requests.get(url, stream=True)
    if request.status_code == 200:
        with open("image.png", 'wb') as image:
            for chunk in request:
                image.write(chunk)
    else:
        return print("Unable to download image")
    try:
        if SETTINGS.nopost is False:
            client.update_with_media("image.png", status=message)
        else:
            # pass
            raise tweepy.TweepError("!!!SIMULATION!!!")
    except tweepy.TweepError as ex:
        print(ex)
        for tint in range(2, 10):
            try:
                temp = Image.open("image.png")
            except Exception as ex:
                return print(ex)
            x = int(round(temp.size[0] / tint))
            if x <= 0:
                x = 1900
            y = int(round(temp.size[1] / tint))
            if y <= 0:
                y = 1080
            temp = temp.resize((x, y), Image.ANTIALIAS)
            temp.save("image.png",
                      optimize=True,
                      quality=int(round(100 / tint)))
            temp.save(io.BytesIO(), format="PNG")
            try:
                client.update_with_media("image.png", status=message)
                break
            except tweepy.TweepError as ex:
                print(ex)
Пример #21
0
  def test_delete_start_redirect_url_error(self):
    self.mox.StubOutWithMock(testutil.OAuthStartHandler, 'redirect_url')
    testutil.OAuthStartHandler.redirect_url(state=mox.IgnoreArg()
      ).AndRaise(tweepy.TweepError('Connection closed unexpectedly...'))
    self.mox.ReplayAll()

    resp = app.application.get_response(
      '/delete/start', method='POST', body=native_str(urllib.parse.urlencode({
        'feature': 'listen',
        'key': self.sources[0].key.urlsafe(),
      })))
    self.assertEquals(302, resp.status_int)
    location = urllib.parse.urlparse(resp.headers['Location'])
    self.assertEquals('/fake/0123456789', location.path)
    self.assertEquals('!FakeSource API error 504: Connection closed unexpectedly...',
                      urllib.parse.unquote(location.fragment))
Пример #22
0
 def get_user(self, uid=None, account=None, resolve=None, reject=None):
     if not self.api:
         raise tweepy.TweepError('Twitter api NOT ready!')
     try:
         user = self.api.get_user(user_id=uid, screen_name=account)
         self.user = user
         if callable(resolve):
             resolve(user)
     except tweepy.TweepError as e:
         logging.error('Uid ({0}) and account ({1}) has error: {2}'.format(uid, account, e))
         if callable(reject):
             reject(uid, account, e)
         if e.api_code in self._IGNORE_ERROR_CODES:
             return None
         raise e
     return self
Пример #23
0
def load_access_token() -> Any:
    auth = make_auth()
    if os.path.isfile(TOKEN_FILE):
        with open(TOKEN_FILE, "r") as f:
            token = json.load(f)
    else:
        verifier = authenicate_oauth(auth)
        try:
            key, secret = auth.get_access_token(verifier)
        except Exception as e:
            raise tweepy.TweepError(e)

        token = {"key": key, "secret": secret}
        with open(TOKEN_FILE, mode="w") as f:
            json.dump(token, f)
    auth.set_access_token(**token)
    return auth
Пример #24
0
 def thread_from_queue(index, twitter, resolve=None, reject=None):
     """
     :param index: thread index to start and sleep
     :param twitter: authentic instance of Twitter
     :param resolve: func of operate which receive two param (twitter, uid)
     :param reject: func of error operate which receive two param (twitter, uid)
     """
     if not callable(resolve):
         raise tweepy.TweepError('resolve is need in twitter crawl callback!')
     time.sleep(index)
     logging.info('Twitter thead-%d : tasks started!' % index)
     while not uids_queue.empty():
         uid = uids_queue.get_nowait()
         if twitter.get_user(uid=uid) is None:
             if callable(reject):
                 reject(twitter, uid=uid)
             continue
         resolve(twitter, uid=uid)
     logging.info('Twitter thead-%d : task complete!' % index)
Пример #25
0
        def update_status(self, status, lat=None, long=None):
            """
            Update status
            :return: Status
            """
            class Status:
                """
                Class mocking a tweepy Status
                """
                def __init__(self, text):
                    """
                    :param text: str
                    """
                    self.text = text

            if status == 'error':
                raise tweepy.TweepError('error on purpose')
            else:
                return Status(status)
Пример #26
0
 def get_access_token(self, verifier=None):
     """
     After user has authorized the request token, get access token
     with user supplied verifier.
     """
     try:
         url = self._get_oauth_url('access_token')
         self.oauth = OAuth1Session(
             self.consumer_key,
             client_secret=self.consumer_secret,
             resource_owner_key=self.request_token['oauth_token'],
             resource_owner_secret=self.request_token['oauth_token_secret'],
             verifier=verifier,
             callback_uri=self.callback)
         resp = self.oauth.fetch_access_token(url, proxies=self.proxies)
         self.access_token = resp['oauth_token']
         self.access_token_secret = resp['oauth_token_secret']
         return self.access_token, self.access_token_secret
     except Exception as e:
         raise tweepy.TweepError(e)
Пример #27
0
def twitter_post(message):
    """ Post a message on Twitter (uses the Tweepy module)
    
    Args:
        message: a string containing the message to be posted
    """
    message = message[:270]
    print(message + "\n")

    if test is False:
        if not check_duplicates(message):
            auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
            auth.set_access_token(access_token, access_token_secret)
            api = tweepy.API(auth)

            try:
                api.update_status(message)
            except tweepy.TweepError as error:
                raise tweepy.TweepError(str(error.reason))
        else:
            print(
                "::warning file=tweet.py:: Tweet NOT posted because it was a duplicate."
            )
Пример #28
0
def curate_tweet_list(self):

    curated_list = []

    while len(
            curated_list
    ) < self.target_tweet_actions_per_session and check_application_limit(
            self):
        search_terms = get_search_data(self)  #get new tweet search terms
        if len(search_terms.split()
               ) == 2:  # make sure we have two search terms

            try:
                raw_list_search = search_tweets(
                    self, search_terms)  #search to get raw list of tweets
                deduped_list = remove_duplicates(
                    self, raw_list_search)  #get rid of duplicates
                clean_list = remove_profane_tweets(
                    self, deduped_list)  #remove any profane tweets
                excluded_list = remove_excludedterms_from_tweets(
                    self, clean_list
                )  #remove any tweets that contain terms on the excluded list
                relevant_list = check_relevancy(
                    self, search_terms, excluded_list
                )  #remove tweets that don't meet relevancy test

                for tweet in relevant_list:
                    curated_list.append(tweet)

            except tweepy.TweepError as e:
                log_tweepy_error_message(self, 'Could not process list', e)
                raise tweepy.TweepError("curate_tweet_list error")

        self.lgr.info('Curated List length:, ' + str(len(curated_list)))

    return curated_list
Пример #29
0
def check_shop():
    try:
        with open('Cache/shop.json', 'r') as file:
            Cached = json.load(file)
        data = requests.get('https://api.peely.de/v1/shop')
        new = data.json()
        if data.status_code != 200:
            return
    except:
        return
    if new != Cached:
        url = new["discordurl"]
        if SETTINGS.shopimageurl or SETTINGS.shopimagetext != "":
            lang = "en"
            if SETTINGS.lang:
                lang = SETTINGS.lang
            url = f"https://api.peely.de/v1/shop/custom?background={SETTINGS.shopimageurl}&text={SETTINGS.shopimagetext}&lang={lang}&featured={SETTINGS.shopfeaturedstring}&daily={SETTINGS.shopdailystring}"
        try:
            print("NEW Shop")
            MODULES.tweet_image(url=url, message=get_text("shop"))
        except Exception as ex:
            raise tweepy.TweepError(ex)
        with open('Cache/shop.json', 'w') as file:
            json.dump(new, file, indent=3)
Пример #30
0
 def on_error(self, status):
     logger.error("Error status is %s " % status)
     raise tweepy.TweepError(status)