Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get followers_count and following_count in the json response? #55

Open
Immortality-IMT opened this issue Nov 4, 2024 · 1 comment
Labels
question Further information is requested

Comments

@Immortality-IMT
Copy link

Immortality-IMT commented Nov 4, 2024

How to get followers_count and following_count in the json response?

JSON response
{
"id":
........
"followers_count": 922,
"following_count": null, <----------------------------------------------------------------- need this info
.....
"following": false,
"followed_by": null,
},

Here are my code alterations but still returns null

      const userIndexPaths = [
...
        "legacy.followers_count",
        "legacy.following_count",  <-------------------------- add this assign the variable
        "legacy.statuses_count",
        "legacy.favourites_count",
....
      ];

    async upsertUsers(users) {
      return this.db.transaction("rw", this.users(), () => {
        const data = users.map((user) => ({
          ...user,
          twe_private_fields: {
            created_at: +parseTwitterDateTime(user.legacy.created_at),
            updated_at: Date.now(),
            // Ensure following_count is included
            following_count: user.legacy.following_count, // <----------------------- add this
            followers_count: user.legacy.followers_count   
          }
        }));
        return this.users().bulkPut(data);
      }).catch(this.logError);
    }

....

    columnHelper.accessor("legacy.followers_count", {
      meta: { exportKey: "followers_count", exportHeader: "Followers" },
      header: () => /* @__PURE__ */ u(Trans, { i18nKey: "Followers" }),
      cell: (info) => /* @__PURE__ */ u("p", { children: info.getValue() })
    }),
    // add this function
    columnHelper.accessor("legacy.following_count", {
      meta: { exportKey: "following_count", exportHeader: "Following" },
      header: () => /* @__PURE__ */ u(Trans, { i18nKey: "Following" }),
      cell: (info) => /* @__PURE__ */ u("p", { children: info.getValue() })
    }),

---
//altered this function to grab following as well

const FollowersInterceptor = (req, res, ext) => {
  // Identify if the request is for "Followers" or "Following"
  const isFollowers = /\/graphql\/.+\/(BlueVerified)*Followers/.test(req.url);
  const isFollowing = /\/graphql\/.+\/Following/.test(req.url);

  if (!isFollowers && !isFollowing) {
    return;  // Exit if neither pattern matches
  }

  try {
    const newData = extractDataFromResponse(
      res,
      (json) => json.data.user.result.timeline.timeline.instructions,
      (entry) => {
        const user = extractTimelineUser(entry.content.itemContent);

        if (user && user.legacy) {
          // Set counts based on the type of request
          user.followers_count = isFollowers ? user.legacy.followers_count : user.followers_count;
          user.following_count = isFollowing ? user.legacy.following_count : user.following_count;
          return user;
        }
        return null;
      }
    );

    databaseManager.extAddUsers(ext.name, newData);

    // Log based on the type of data processed
    const logType = isFollowers ? "Followers" : "Following";
    logger.info(`${logType}: ${newData.length} items received`);
  } catch (err) {
    logger.debug(req.method, req.url, res.status, res.responseText);
    logger.errorWithBanner("UserInterceptor: Failed to parse API response", err);
  }
};

of interest are
const FollowingInterceptor = (req, res, ext) => {
and
const FollowersInterceptor = (req, res, ext) => {

I chose to conbine to grab the info into 1 function.
Returns null, cannot get it to populate the value.

@prinsss prinsss added the question Further information is requested label Nov 21, 2024
@prinsss
Copy link
Owner

prinsss commented Nov 21, 2024

There is no user.legacy.following_count property. The field of user's following count in Twitter API is actually called friends_count. And you don't need to change twe_private_fields.

I've modified the script and build a combined version for you. Give it a try:

twitter-web-exporter@1.2.1-beta.4+build.1.user.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants