Fetch comments data on posts
(
browser, owner, poster, amount, post_link, ignore_users, randomize, logger
)
| 238 | |
| 239 | |
| 240 | def get_comments_on_post( |
| 241 | browser, owner, poster, amount, post_link, ignore_users, randomize, logger |
| 242 | ): |
| 243 | """Fetch comments data on posts""" |
| 244 | web_address_navigator(browser, post_link) |
| 245 | |
| 246 | comments = [] |
| 247 | commenters = [] |
| 248 | |
| 249 | if randomize is True: |
| 250 | amount = amount * 3 |
| 251 | |
| 252 | # check if commenting on the post is enabled |
| 253 | (commenting_approved, disapproval_reason,) = verify_commenting( |
| 254 | browser, |
| 255 | None, |
| 256 | None, |
| 257 | logger, |
| 258 | ) |
| 259 | if not commenting_approved: |
| 260 | logger.info(disapproval_reason) |
| 261 | return None |
| 262 | |
| 263 | # get comments & commenters information path |
| 264 | like_button_full_XPath = read_xpath( |
| 265 | get_comments_on_post.__name__, "like_button_full_XPath" |
| 266 | ) |
| 267 | unlike_button_full_XPath = read_xpath( |
| 268 | get_comments_on_post.__name__, "unlike_button_full_XPath" |
| 269 | ) |
| 270 | |
| 271 | # wait for page fully load [IMPORTANT!] |
| 272 | explicit_wait(browser, "PFL", [], logger, 10) |
| 273 | |
| 274 | try: |
| 275 | all_comment_like_buttons = browser.find_elements( |
| 276 | By.XPATH, like_button_full_XPath |
| 277 | ) |
| 278 | |
| 279 | if all_comment_like_buttons: |
| 280 | commenter = None |
| 281 | comment = None |
| 282 | |
| 283 | data = getMediaData("edge_media_to_parent_comment", browser) |
| 284 | for value in data["edges"]: |
| 285 | commenter = value["node"]["owner"]["username"] |
| 286 | comment = value["node"]["text"] |
| 287 | |
| 288 | if ( |
| 289 | commenter |
| 290 | and commenter not in commenters |
| 291 | and commenter not in [owner, poster, ignore_users] |
| 292 | and comment |
| 293 | ): |
| 294 | commenters.append(commenter) |
| 295 | comments.append(comment) |
| 296 | else: |
| 297 | logger.info("Could not grab any commenter from this post") |
no test coverage detected