!!Con West 2020: Jessica Garson - How I solved my NYC parking problem with Python!

Mar 20, 2020 18:51 · 1651 words · 8 minute read github happening two functions alright

Hey, how’s it going? Can you all hear me okay? All right. Cool. Hi. So my name is Jess, and I’m a developer advocate at Twitter. And I’m here to talk with you about how I solved a parking problem that I had in my life with code. And let me know if this inspires you to create anything. You can reach me @JessicaGarson on Twitter, and code for the talk can be found at github.com/twitterdev/parking.

00:59 - Today we’re actually gonna be using a repository in there called Recent Search. So let’s talk a little bit about what was happening in my life to inspire me to create a solution with code. So I live in Brooklyn, New York. And if you notice, the car has an orange… envelope, of sorts, on it. That is a parking ticket. How many people have gotten a parking ticket in New York? So a couple of hands are raised. How many people have ever gotten a parking ticket? Alright. The whole room has raised their hands. So in New York, that is a $45 to $160 endeavor.

01:39 - Depending on where you park and the situation. And that’s really, really a lot of money. So I had a problem. I had a car. I no longer have a car. But I did have a car. And I was moving my car a lot. Every night. I actually looked at traffic data in my neighborhood and I would move my car every night at 8 o’clock. And I was really good about this. I had an alarm to remind me to do so, I would move my social schedule, so I would move my car right on time. And I was doing this every night. You might be wondering… Jess, why were you doing this? What are alternate side of the street parking restrictions anyway in New York?! What is this? So. Yeah. On Mondays and Thursdays, in my neighborhood, you can park on the right side of the street until 8 am, where there is street cleaning that comes through the neighborhood.

02:32 - And on Tuesdays and Fridays, on the other side of the street, it’s the same situation. So… I was moving my car a lot. And there was one night where… I was driving around for about 2 hours, 3 hours, listening to a book on tape, and I came back home and my roommate was like… Jess! Where were you? And I was like… Yeah. I was moving my car. And they’re like… Yeah, but after, what did you do? Did you go to a show? Did you go to a party? Did you do something cool? I was like, nope! I was just moving my car. They were like… You know, you didn’t have to do that. I was like… What do you mean I didn’t have to do that? And they showed me this really cool Twitter handle called NYC alternate side of the street parking and it updates every day at 7:30 am and 4 pm. And I was like…

03:20 - Whoa! I’m a developer advocate at Twitter!! I think I can solve this problem! So I built this solution using Twitter data and Twilio to send myself a text message whenever I didn’t have to move my car. So let’s kind of talk about how I solved this. So I needed to get the tweets from the NYC ASP Twitter handle. I needed to see if the words “suspended” and “tomorrow” appeared in the same tweet. I needed to get a text message when the words appeared, and I needed to run this on a server so that this repeated without any effort from me, because I would never remember to run the script.

03:57 - So the process for creating this app is to get the tweets we want from the Twitter API connect to Twilio, and get a text whenever I didn’t have to move my car. If you want to build something like this, you might be wondering: What will I need to get started? You will need to create a Twitter app which allows you to connect to the API. You can apply for access at this address if you need that. The endpoint that I’m gonna be showcasing today uses one of our experimental endpoints called Twitter developer labs. It’s Recent Search, which allows you to get tweets from the past seven days. And you also need a Twilio account.

04:36 - You can check out their documentation on getting started on the subject. Tilde is actually here with Twilio. And they have some really great example code as well and you can talk with them if you need any help. You’ll also need the libraries twilio.rest, which you can pip install, pandas, and requests. So let’s look at the code. I always like looking at the code. So I have this running in a Jupyter notebook right now. So the first thing you have to do is import the libraries that you need which is pandas, yaml, and requests. So you run that. Then we also can create a helper script.

05:13 - So you can actually get code that’s very similar to this on an index card over at the Twilio booth. But from here, you’ll import in os, you can use environment variables, import in twilio.rest and their client to help you connect and you can create a function that I like to think of as a secret handshake to create the authentication that you need, and then you can go and send a message. So this is where you set it up, so you can say… You don’t have to move your car tonight. Enjoy your night. At one point, I had the full tweets going through this. But it wasn’t really needed.

05:48 - So from there, you can actually import these two functions that you’ve created into the script. And then from here, let’s actually run this. From here, we can actually pass in the handle that we’re looking for, into the URL. So we can pass in the NYC ASP handle, and we see the full URL that we’re gonna be connecting to. And then from here, I actually have a yaml file to keep my secrets in it. I make sure I don’t push that anywhere. And I keep it in my.gitignore. But I have my bearer token in there, so I can pass that through. So I’m gonna go through the secret yaml file, and then from there, I can pass in that bearer token, in through the headers, using the requests library for Python and then do a GET request, pass in the URL, and pass in the headers that contain the bearer token. And so the next thing I have to do is figure out if the response encoding is correct. Create an object for the JSON, print out that JSON, and then… Ooh! When I get nervous, I like move my hands in weird ways. Yay cool. And so we can actually scroll down here. We can put it inside of a dataframe.

07:06 - And so from here, what we can see is that we have the data in a dataframe, and we have just the ID and the last 10 tweets. Which is pretty exciting. So we have… We can actually see that the rules are in effect every day, except for on the 8th line and the 9th line. You can see that… Actually, line number 7, the rules will be suspended. So that’s pretty exciting. Actually, no. It’s the 8th line. I did get it right. It’s an older version of the code. And then when you do the client, that’s where you connect to the Twilio API, and then you can put the logic in this one statement. So here we have: If the word “suspended” and “tomorrow” appear in the same text, then it says “text sent”.

07:56 - And since it is not today, it will say “not today, friend.” However, if I move this and change it to the 8th line, then I should get a text message. And I do have a text message. And two more minutes left. Which is pretty exciting. So this is what the text message looks like for me right now. It tells me that I don’t have to move my car. And this is currently deployed to a DigitalOcean server using a cron job.

08:28 - So the conclusion is: You might be wondering -- Jess, why didn’t you make this so you do get a text when you need to move your car instead of when you don’t? That’s something that like everytime I give this talk people ask me about. Why did you build this whole thing instead of tell you when you did have to move your car? That just wasn’t the problem I had in my life. So I challenge all of you to think about the problems that you have in your life and think about how you can create a solution with code. And if you want to see a blog post version of this talk, you can check it out at t.co/parking-post. Let me know if this inspires you to create anything.

09:02 - And you can let us know at @TwitterDev, or on the Twitter community forums if you get stuck or if you just want to tell us what you’ve built, and if you have feedback while you’re using the Twitter API, you can give us feedback at twitterdevfeedback.uservoice.com. And we’re also hiring for a developer advocacy manager. So if that’s something you’re interested in, come talk to me. And if you’re building with the Twitter API and wanna’ talk, come find me. I’d love to have a conversation. Thanks! (applause) .