0

I have a list of LinkedIn posts IDs. I need to request share statistics for each of those posts with another request.

The request function looks like this:

def ugcp_stats(headers):
    response = requests.get(f'https://api.linkedin.com/v2/organizationalEntityShareStatistics?q=organizationalEntity&organizationalEntity=urn%3Ali%3Aorganization%3A77487&ugcPosts=List(urn%3Ali%3AugcPost%3A{shid},urn%3Ali%3AugcPost%3A{shid2},...,urn%3Ali%3AugcPost%3A{shidx})', headers = headers)
    ugcp_stats = response.json()
    return ugcp_stats

urn%3Ali%3AugcPost%3A{shid},urn%3Ali%3AugcPost%3A{shid2},...,urn%3Ali%3AugcPost%3A{shidx} - these are the share urns. Their number depends on number of elements in my list.

What should I do next? Should I count the number of elements in my list and somehow amend the request URL to include all of them? Or maybe I should loop through the list and make a separate request for each of the elements and then append all the responses in one json file?

I'm struggling and I'm not quite sure how to write this. I don't even know how to parse the element into the request. Although I suspect it could look something like this:

for shid in shids:
    def ugcp_stats(headers):
        response = requests.get(f'https://api.linkedin.com/v2/organizationalEntityShareStatistics?q=organizationalEntity&organizationalEntity=urn%3Ali%3Aorganization%3A77487&ugcPosts=List(urn%3Ali%3AugcPost%3A & {shid})', headers = headers)
        ugcp_stats = response.json()
        return ugcp_stats

UPDATE - following your ansers The code looks like this now:

    link = "https://api.linkedin.com/v2/organizationalEntityShareStatistics?q=organizationalEntity&organizationalEntity=urn%3Ali%3Aorganization%3A77487&ugcPosts=List"
def share_stats(headers, shids):
    # Local variable
    sample = ""
    # Sample the shids in the right pattern
    for shid in shids: sample += "urn%3Ali%3AugcPost%3A & {},".format(shid)
    # Get the execution of the string content
    response = eval(f"requests.get('{link}({sample[:-1]})', headers = {headers})")
    # Return the stats
    return response.json()
    if __name__ == '__main__':
        credentials = 'credentials.json'
        access_token = auth(credentials) # Authenticate the API
        headers = headers(access_token) # Make the headers to attach to the API call.
        share_stats = share_stats(headers) # Get shares
    print(share_stats)

But nothing seems to be happening. It finishes the script, but I don't get anything. What's wrong?

  • If you *do* find the need to make multiple API requests for this use case, I’d suggest using the `concurrent.futures.ThreadPoolExecutor` and maybe the ‘map’ function IIRC that it provides - feel like it would work well for this workflow. – rv.kvetch Oct 15 '21 at 21:36
  • You can use exec or eval. They make same thing, both will execute python code from a string. So will interpret you message as python code. The difference between them is returning. eval will return also the executed value and exec will just execute with out returning the value (if it). This will help you to automate the "shid" feeds in your link string type. Also consider @rv.kvetch suggestion as an optimization strategy to get advantage of multi-threading technology from now days. – ASI Oct 16 '21 at 01:12

1 Answers1

0

This is just a proof of what I told you earlier as comment. Now you will adapt to your needs (even I try it to do it for you) :)

Updated - Base on your feedback.

#// IMPORT
#// I'm assuming your are using "requests" library
#// PyCharm IDE show me like this library is not used, but "eval()" is using it
import requests

#// GLOBAL VARIABLES
link: str = "https://api.linkedin.com/v2/organizationalEntityShareStatistics?q=organizationalEntity&organizationalEntity=urn%3Ali%3Aorganization%3A77487&ugcPosts=List"


#// Your function logic updated
def share_stats(sheds: list, header: dict) -> any:
    # Local variable
    sample = ""

    # Sample the sheds in the right pattern
    for shed in sheds: sample += "urn%3Ali%3AugcPost%3A & {},".format(shed)

    # Get the execution of the string content
    response = eval(f"requests.get('{link}({sample[:-1]})', headers = {header})")

    # Return the stats as JSON file
    return response.json()


#// Run if this is tha main file
if __name__ == '__main__':
    #// An empty sheds list for code validation
    debug_sheds: list = []
    credentials: str = "credentials.json"

    #// For me I get an unresolved reference for "auth", for you shod be fine
    #// I'm assuming is your function for reading the file content and convert it to Python
    access_token = auth(credentials)    # Authenticate the API

    #// Your error was from this script line
    #// Error message: 'TypedDict' object is not callable
    #// Your code: headers = headers(access_token)
    #// When you want to get a dictionary value by a key use square brackets
    headers = headers[access_token]     # Make the headers to attach to the API call.

    #// Here you shood ged an error/warning because you do not provided the sheds first time
    #// Your code: share_stats = share_stats(headers)
    share_stats = share_stats(debug_sheds, headers)  # Get shares

    print(share_stats)

ASI
  • 334
  • 3
  • 15
  • Thanks for your reply! I'm trying to make your function work, but I get a type error. I amended the firs post to show what the code looks like now. ``` headers = headers(access_token) # Make the headers to attach to the API call. TypeError: 'dict' object is not callable ``` – Piotr Chmurzynski Oct 20 '21 at 07:57
  • @PiotrChmurzynski That `if __name__ == '__main__':` need to be out side of the function definition. Short story of that part of the script is to run if the main script file is this one, other ways the hole content of the file can be used as a library for importing classes and functions. For more information's about that you can read [this answer for the forum](https://stackoverflow.com/a/419185/10234009). Try that and let me know is is working. I'm still confused of how your dictionary is look like. – ASI Oct 21 '21 at 22:33
  • @PiotrChmurzynski Or at least this is how I use it. Also I copy-paste your code on **PyCharm** and the IDE sow that script after return state is unreachable. You may want to modify and that one. Also check and [this forum page](https://stackoverflow.com/questions/6634708/typeerror-dict-object-is-not-callable) to see if your dictionary call is used wrong or change the **headers** variable name, maybe is an conflicting typename on `headers = {headers})`. Other way updating and the "headers" value to make and idea how your dictionary look like (e.g.`print(headers(auth('credentials.json')))`). – ASI Oct 21 '21 at 23:19