mahrtayyab / tweety

Twitter Scraper
494 stars 67 forks source link

A Failed None Check causing a Error to attempt to call a function of NoneType Line 269 on twDataTypes #210

Closed TheLiveitup34 closed 1 day ago

TheLiveitup34 commented 2 months ago

In short I looked into the find_objects and seen it returns None when no value is found and the error came up of it attempting to call the function of a NoneType.

def _format_tweet(self):
        self._check_if_protected()
        self._tweet = find_objects(self._raw, "__typename", ["Tweet", "TweetWithVisibilityResults"], recursive=False)
        // code here fails to detect if  self._tweet is a value of None value and will fail with error trace back below
        if self._tweet.get('tweet'):
            self._tweet = self._tweet['tweet']
Traceback (most recent call last):
  File "/home/user/Tweety-archiver/main.py", line 190, in <module>
    modify_tweet(app.tweet_detail(manual))
  File "/home/user/.local/lib/python3.10/site-packages/tweety/bot.py", line 863, in tweet_detail
    tweet = Tweet(self, entry, response)
  File "/home/user/.local/lib/python3.10/site-packages/tweety/types/twDataTypes.py", line 46, in new_init
    init(self, *_args, **_kwargs) # noqa
  File "/home/user/.local/lib/python3.10/site-packages/tweety/types/twDataTypes.py", line 166, in __init__
    self._format_tweet()
  File "/home/user/.local/lib/python3.10/site-packages/tweety/types/twDataTypes.py", line 269, in _format_tweet
    if self._tweet.get('tweet'):
AttributeError: 'NoneType' object has no attribute 'get'
mahrtayyab commented 2 months ago

if you are comfortable , can you please share tweet id which caused this error

TheLiveitup34 commented 2 months ago

Yes I'm more than comfortable i built the program to use the code you wrote to archive club penguin tweets for a friend and it may not be available to you due to it going private after closure but we're using a logged in session that has access to the tweets

Tweet id:993640176751538177
mahrtayyab commented 1 day ago

https://github.com/mahrtayyab/tweety/commit/650981ce9605e6e06f2c5c1fe2db5c01a5270d2e

Tyviebrock commented 1 day ago

image

Can confirm this issue is still occurring on the latest version

mahrtayyab commented 1 day ago

can you please specify version your are using.

Tyviebrock commented 1 day ago

Version 2.0.1

TheLiveitup34 commented 1 day ago

to add to note to what he is saying ` def find_objects(obj, key, value, recursive=True, none_value=None): results = []

def find_matching_objects(_obj, _key, _value):
    if isinstance(_obj, dict):
        if _key in _obj:
            found = False
            if _value is None:
                found = True
                results.append(_obj[_key])
            elif (isinstance(_value, list) and _obj[_key] in _value) or _obj[_key] == _value:
                found = True
                results.append(_obj)

            if not recursive and found:
                return results[0]

        for sub_obj in _obj.values():
            find_matching_objects(sub_obj, _key, _value)
    elif isinstance(_obj, list):
        for item in _obj:
            find_matching_objects(item, _key, _value)

find_matching_objects(obj, key, value)

if len(results) == 1:
    return results[0]

if len(results) == 0:
 // returns None here 
    return none_value

if not recursive:
    return results[0]

return results

` your function called in line 268 of twDataTypes.py

def _format_tweet(self): self._check_if_protected() // This returns None Object self._tweet = find_objects(self._raw, "__typename", ["Tweet", "TweetWithVisibilityResults"], recursive=False) // Runs error to call .get on a None value if self._tweet.get('tweet'): self._tweet = self._tweet['tweet']

mahrtayyab commented 1 day ago

Please do update to latest version

Tyviebrock commented 1 day ago

image Can confirm issue is now fixed in version 2.0.2