Skip to content

Instantly share code, notes, and snippets.

@marsimaria
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save marsimaria/bb9f71a0338c038fa272 to your computer and use it in GitHub Desktop.

Select an option

Save marsimaria/bb9f71a0338c038fa272 to your computer and use it in GitHub Desktop.
Britney Spears feat. New York Times
import sys
import re
import json
import random
s = []
from nytimesarticle import articleAPI
for i in range(0, 100):
api = articleAPI('74548056debe1698254a3a07babc9f84:3:60371965')
res = api.search(q = 'Britney Spears', fq = {'lead_paragraph':'Britney Spears'}, begin_date = 20000101, end_date = 20001231, page = str(i))
for m in res['response']['docs']:
s.append(m['lead_paragraph'])
with open("britney.txt", "w") as text_file:
for item in s:
text_file.write(item)
britSen = list()
search = r'\b(?:Spears|SPEARS)\b [A-Za-z ]+'
for sentence in open('britney.txt'):
lines = sentence.split('.')
for line in lines:
results = re.findall(search, line)
if len(results) > 0:
britSen.append(results)
# print results
fuckYouOldBritSen = []
for listbitches in britSen:
listbabes = britSen[0:]
for listbabes in listbitches:
fuckYouOldBritSen.append(listbabes)
# print fuckYouOldBritSen
first = []
oops = open('oops.txt', 'r')
for sentence in oops:
line = sentence.split('\n')
for eachline in line:
firstword = eachline.split(' ')[0]
firstwords = firstword.strip(',')
if len(firstwords) > 0:
first.append(firstwords)
# print firstwords
for i in range(len(fuckYouOldBritSen)):
r1 = random.choice(first)
r2 = random.choice(fuckYouOldBritSen)
line = r1 + ' '+ r2
print line
@inteledyne
Copy link
Copy Markdown

when I try to run a basic api.search command in python 3.4 like you've done here I get the following stack trace. What do you think is causing this?

C:\Python34\python.exe C:/Users/robin.tanner/OneDrive/Documents/GitHub-Projects/news_streaming/berkeley.py
Traceback (most recent call last):
  File "C:/Users/robin.tanner/OneDrive/Documents/GitHub-Projects/news_streaming/berkeley.py", line 6, in <module>
    articles = api.search(q = 'Obama')
  File "C:\Python34\lib\site-packages\nytimesarticle.py", line 115, in search
    return r.json()
  File "C:\Python34\lib\site-packages\requests-2.7.0-py3.4.egg\requests\models.py", line 819, in json
    return json.loads(self.text, **kwargs)
  File "C:\Python34\lib\json\__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment